Fixed ObjectInputStream header error by reading chunks.
This commit is contained in:
		| @@ -28,11 +28,5 @@ | |||||||
| 			<attribute name="maven.pomderived" value="true"/> | 			<attribute name="maven.pomderived" value="true"/> | ||||||
| 		</attributes> | 		</attributes> | ||||||
| 	</classpathentry> | 	</classpathentry> | ||||||
| 	<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"> |  | ||||||
| 		<attributes> |  | ||||||
| 			<attribute name="maven.pomderived" value="true"/> |  | ||||||
| 			<attribute name="test" value="true"/> |  | ||||||
| 		</attributes> |  | ||||||
| 	</classpathentry> |  | ||||||
| 	<classpathentry kind="output" path="target/classes"/> | 	<classpathentry kind="output" path="target/classes"/> | ||||||
| </classpath> | </classpath> | ||||||
|   | |||||||
| @@ -1,5 +1,6 @@ | |||||||
| package envoy.client; | package envoy.client; | ||||||
|  |  | ||||||
|  | import java.io.ByteArrayInputStream; | ||||||
| import java.io.InputStream; | import java.io.InputStream; | ||||||
| import java.io.ObjectInputStream; | import java.io.ObjectInputStream; | ||||||
| import java.net.SocketException; | import java.net.SocketException; | ||||||
| @@ -10,12 +11,13 @@ import java.util.logging.Level; | |||||||
| import java.util.logging.Logger; | import java.util.logging.Logger; | ||||||
|  |  | ||||||
| import envoy.client.util.EnvoyLog; | import envoy.client.util.EnvoyLog; | ||||||
|  | import envoy.util.SerializationUtils; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * Project: <strong>envoy-client</strong><br> |  * Project: <strong>envoy-client</strong><br> | ||||||
|  * File: <strong>Receiver.java</strong><br> |  * File: <strong>Receiver.java</strong><br> | ||||||
|  * Created: <strong>30.12.2019</strong><br> |  * Created: <strong>30.12.2019</strong><br> | ||||||
|  *  |  * | ||||||
|  * @author Kai S. K. Engelbart |  * @author Kai S. K. Engelbart | ||||||
|  * @since Envoy v0.3-alpha |  * @since Envoy v0.3-alpha | ||||||
|  */ |  */ | ||||||
| @@ -36,29 +38,42 @@ public class Receiver implements Runnable { | |||||||
| 	@SuppressWarnings("unchecked") | 	@SuppressWarnings("unchecked") | ||||||
| 	@Override | 	@Override | ||||||
| 	public void run() { | 	public void run() { | ||||||
| 		try (ObjectInputStream oin = new ObjectInputStream(in)) { |  | ||||||
| 			while (true) { |  | ||||||
| 				Object obj = oin.readObject(); |  | ||||||
| 				logger.finest("Received object " + obj); |  | ||||||
|  |  | ||||||
| 				// Get appropriate processor | 		try { | ||||||
| 				@SuppressWarnings("rawtypes") | 			while (true) { | ||||||
| 				Consumer processor = processors.get(obj.getClass()); | 				// Read object length | ||||||
| 				if (processor == null) | 				byte[] lenBytes = new byte[4]; | ||||||
| 					logger.severe(String.format("The received object has the class %s for which no processor is defined.", obj.getClass())); | 				in.read(lenBytes); | ||||||
| 				else processor.accept(obj); | 				int len = SerializationUtils.bytesToInt(lenBytes, 0); | ||||||
|  |  | ||||||
|  | 				// Read object into byte array | ||||||
|  | 				byte[] objBytes = new byte[len]; | ||||||
|  | 				in.read(objBytes); | ||||||
|  |  | ||||||
|  | 				try (ObjectInputStream oin = new ObjectInputStream(new ByteArrayInputStream(objBytes))) { | ||||||
|  | 					Object obj = oin.readObject(); | ||||||
|  | 					logger.finest("Received object " + obj); | ||||||
|  |  | ||||||
|  | 					// Get appropriate processor | ||||||
|  | 					@SuppressWarnings("rawtypes") | ||||||
|  | 					Consumer processor = processors.get(obj.getClass()); | ||||||
|  | 					if (processor == null) | ||||||
|  | 						logger.severe(String.format("The received object has the class %s for which no processor is defined.", obj.getClass())); | ||||||
|  | 					else processor.accept(obj); | ||||||
|  | 				} | ||||||
| 			} | 			} | ||||||
| 		} catch (SocketException e) { | 		} catch (SocketException e) { | ||||||
| 			logger.info("Connection probably closed by client. Exiting receiver thread..."); | 			logger.info("Connection probably closed by client. Exiting receiver thread..."); | ||||||
| 		} catch (Exception e) { | 		} catch (Exception e) { | ||||||
| 			logger.log(Level.SEVERE, "Error on receiver thread", e); | 			logger.log(Level.SEVERE, "Error on receiver thread", e); | ||||||
|  | 			e.printStackTrace(); | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	/** | 	/** | ||||||
| 	 * Adds an object processor to this {@link Receiver}. It will be called once an | 	 * Adds an object processor to this {@link Receiver}. It will be called once an | ||||||
| 	 * object of the accepted class has been received. | 	 * object of the accepted class has been received. | ||||||
| 	 *  | 	 * | ||||||
| 	 * @param processorClass the object class accepted by the processor | 	 * @param processorClass the object class accepted by the processor | ||||||
| 	 * @param processor      the object processor | 	 * @param processor      the object processor | ||||||
| 	 */ | 	 */ | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user