[BAEL-7441] Fixing EOFException in Java
This commit is contained in:
		
							parent
							
								
									7223f8b498
								
							
						
					
					
						commit
						a2a1068b13
					
				| @ -10,4 +10,5 @@ This module contains articles about core java exceptions | |||||||
| - [Get the Current Stack Trace in Java](https://www.baeldung.com/java-get-current-stack-trace) | - [Get the Current Stack Trace in Java](https://www.baeldung.com/java-get-current-stack-trace) | ||||||
| - [Errors and Exceptions in Java](https://www.baeldung.com/java-errors-vs-exceptions) | - [Errors and Exceptions in Java](https://www.baeldung.com/java-errors-vs-exceptions) | ||||||
| - [Fix the IllegalArgumentException: No enum const class](https://www.baeldung.com/java-fix-no-enum-const-class) | - [Fix the IllegalArgumentException: No enum const class](https://www.baeldung.com/java-fix-no-enum-const-class) | ||||||
|  | - [How to Fix EOFException in Java](https://www.baeldung.com/java-fix-eofexception) | ||||||
| - [[<-- Prev]](../core-java-exceptions-3) | - [[<-- Prev]](../core-java-exceptions-3) | ||||||
|  | |||||||
| @ -0,0 +1,16 @@ | |||||||
|  | package com.baeldung.exception.eof; | ||||||
|  | 
 | ||||||
|  | import java.io.DataInputStream; | ||||||
|  | import java.io.InputStream; | ||||||
|  | import java.io.ByteArrayInputStream; | ||||||
|  | 
 | ||||||
|  | public class EOFExceptionDemo { | ||||||
|  |     public static void readInput() throws Exception { | ||||||
|  |       InputStream is = new ByteArrayInputStream("123".getBytes()); | ||||||
|  |       DataInputStream in = new DataInputStream(is); | ||||||
|  |       while (true) { | ||||||
|  |         char value = (char)in.readByte(); | ||||||
|  |         System.out.println("Input value: " + value); | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,22 @@ | |||||||
|  | package com.baeldung.exception.eof; | ||||||
|  | 
 | ||||||
|  | import java.io.DataInputStream; | ||||||
|  | import java.io.InputStream; | ||||||
|  | import java.io.ByteArrayInputStream; | ||||||
|  | import java.io.EOFException; | ||||||
|  | 
 | ||||||
|  | public class EOFExceptionDemo2 { | ||||||
|  |     public static void readInput() throws Exception { | ||||||
|  |       InputStream is = new ByteArrayInputStream("123".getBytes()); | ||||||
|  |       DataInputStream in = new DataInputStream(is); | ||||||
|  |       while (true) { | ||||||
|  |         try { | ||||||
|  |           char value = (char)in.readByte(); | ||||||
|  |           System.out.println("Input value: " + value); | ||||||
|  |         } catch (EOFException e) { | ||||||
|  |           System.out.println("End of file"); | ||||||
|  |           break; | ||||||
|  |         } | ||||||
|  |       }       | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,19 @@ | |||||||
|  | package com.baeldung.exception.eof; | ||||||
|  | 
 | ||||||
|  | import java.io.DataInputStream; | ||||||
|  | import java.io.InputStream; | ||||||
|  | import java.util.Scanner; | ||||||
|  | import java.io.ByteArrayInputStream; | ||||||
|  | 
 | ||||||
|  | public class EOFExceptionDemo3 { | ||||||
|  |     public static void readInput() { | ||||||
|  |         InputStream is = new ByteArrayInputStream("1 2 3".getBytes()); | ||||||
|  |         Scanner sc = new Scanner(is); | ||||||
|  |         while (sc.hasNextInt()) { | ||||||
|  |             int value = sc.nextInt(); | ||||||
|  |             System.out.println("Input value: " + value); | ||||||
|  |         } | ||||||
|  |         System.out.println("End of file"); | ||||||
|  |         sc.close(); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,35 @@ | |||||||
|  | package com.baeldung.exception.eof; | ||||||
|  | 
 | ||||||
|  | import org.junit.jupiter.api.Test; | ||||||
|  | import org.junit.jupiter.api.BeforeEach; | ||||||
|  | import org.junit.jupiter.api.AfterEach; | ||||||
|  | import java.io.PrintStream; | ||||||
|  | import java.io.ByteArrayOutputStream; | ||||||
|  | import static org.junit.jupiter.api.Assertions.assertEquals; | ||||||
|  | 
 | ||||||
|  | public class EOFExceptionDemo2UnitTest { | ||||||
|  | 
 | ||||||
|  |     private final PrintStream standardOut = System.out; | ||||||
|  |     private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream(); | ||||||
|  | 
 | ||||||
|  |     @BeforeEach | ||||||
|  |     public void setUp() { | ||||||
|  |         System.setOut(new PrintStream(outputStreamCaptor)); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @AfterEach | ||||||
|  |     public void tearDown() { | ||||||
|  |         System.setOut(standardOut); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     void readInput()throws Exception { | ||||||
|  |         EOFExceptionDemo2.readInput(); | ||||||
|  |         String expectedOuput = "Input value: 1"; | ||||||
|  |         expectedOuput += "\n" + "Input value: 2"; | ||||||
|  |         expectedOuput += "\n" + "Input value: 3"; | ||||||
|  |         expectedOuput += "\n" + "End of file"; | ||||||
|  |         assertEquals(expectedOuput, outputStreamCaptor.toString() | ||||||
|  |                 .trim()); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,35 @@ | |||||||
|  | package com.baeldung.exception.eof; | ||||||
|  | 
 | ||||||
|  | import org.junit.jupiter.api.Test; | ||||||
|  | import org.junit.jupiter.api.BeforeEach; | ||||||
|  | import org.junit.jupiter.api.AfterEach; | ||||||
|  | import java.io.PrintStream; | ||||||
|  | import java.io.ByteArrayOutputStream; | ||||||
|  | import static org.junit.jupiter.api.Assertions.assertEquals; | ||||||
|  | 
 | ||||||
|  | public class EOFExceptionDemo3UnitTest { | ||||||
|  | 
 | ||||||
|  |     private final PrintStream standardOut = System.out; | ||||||
|  |     private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream(); | ||||||
|  | 
 | ||||||
|  |     @BeforeEach | ||||||
|  |     public void setUp() { | ||||||
|  |         System.setOut(new PrintStream(outputStreamCaptor)); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @AfterEach | ||||||
|  |     public void tearDown() { | ||||||
|  |         System.setOut(standardOut); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     void readInput() { | ||||||
|  |         EOFExceptionDemo3.readInput(); | ||||||
|  |         String expectedOuput = "Input value: 1"; | ||||||
|  |         expectedOuput += "\n" + "Input value: 2"; | ||||||
|  |         expectedOuput += "\n" + "Input value: 3"; | ||||||
|  |         expectedOuput += "\n" + "End of file"; | ||||||
|  |         assertEquals(expectedOuput, outputStreamCaptor.toString() | ||||||
|  |                 .trim()); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,36 @@ | |||||||
|  | package com.baeldung.exception.eof; | ||||||
|  | 
 | ||||||
|  | import org.junit.jupiter.api.Test; | ||||||
|  | import org.junit.jupiter.api.BeforeEach; | ||||||
|  | import org.junit.jupiter.api.AfterEach; | ||||||
|  | import java.io.EOFException; | ||||||
|  | import java.io.PrintStream; | ||||||
|  | import java.io.ByteArrayOutputStream; | ||||||
|  | import static org.junit.jupiter.api.Assertions.assertThrows; | ||||||
|  | import static org.junit.jupiter.api.Assertions.assertEquals; | ||||||
|  | 
 | ||||||
|  | public class EOFExceptionDemoUnitTest { | ||||||
|  | 
 | ||||||
|  |     private final PrintStream standardOut = System.out; | ||||||
|  |     private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream(); | ||||||
|  | 
 | ||||||
|  |     @BeforeEach | ||||||
|  |     public void setUp() { | ||||||
|  |         System.setOut(new PrintStream(outputStreamCaptor)); | ||||||
|  |     } | ||||||
|  |      | ||||||
|  |     @AfterEach | ||||||
|  |     public void tearDown() { | ||||||
|  |         System.setOut(standardOut); | ||||||
|  |     }     | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     void readInput_throwsEOFException() { | ||||||
|  |         assertThrows(EOFException.class, () -> EOFExceptionDemo.readInput());             | ||||||
|  |         String expectedOuput = "Input value: 1"; | ||||||
|  |         expectedOuput += "\n" + "Input value: 2"; | ||||||
|  |         expectedOuput += "\n" + "Input value: 3";         | ||||||
|  |         assertEquals(expectedOuput, outputStreamCaptor.toString() | ||||||
|  |             .trim());      | ||||||
|  |     } | ||||||
|  | } | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user