1 10 11 package org.mule.providers.tcp.protocols; 12 13 import org.apache.commons.logging.Log; 14 import org.apache.commons.logging.LogFactory; 15 import org.mule.providers.tcp.TcpProtocol; 16 17 import org.apache.commons.io.output.ByteArrayOutputStream; 18 import java.io.IOException ; 19 import java.io.InputStream ; 20 import java.io.OutputStream ; 21 import java.net.SocketException ; 22 import java.net.SocketTimeoutException ; 23 24 31 public class EOFProtocol implements TcpProtocol 32 { 33 34 private static final int BUFFER_SIZE = 8192; 35 36 private static final Log logger = LogFactory.getLog(EOFProtocol.class); 37 38 public byte[] read(InputStream is) throws IOException 39 { 40 ByteArrayOutputStream baos = new ByteArrayOutputStream(EOFProtocol.BUFFER_SIZE); 41 42 byte[] buffer = new byte[EOFProtocol.BUFFER_SIZE]; 43 int len = 0; 44 try 45 { 46 while ((len = is.read(buffer)) == 0) 47 { 48 } 50 } 51 catch (SocketException e) 52 { 53 EOFProtocol.logger.debug("Socket exception occured: " + e.getMessage()); 55 return null; 56 } 57 catch (SocketTimeoutException e) 58 { 59 EOFProtocol.logger.debug("Socket timeout, returning null."); 60 return null; 61 } 62 if (len == -1) 63 { 64 return null; 65 } 66 else 67 { 68 do 69 { 70 baos.write(buffer, 0, len); 71 } 72 while ((len = is.read(buffer)) >= 0); 73 74 baos.flush(); 75 baos.close(); 76 return baos.toByteArray(); 77 } 78 } 79 80 public void write(OutputStream os, byte[] data) throws IOException 81 { 82 os.write(data); 83 } 84 85 } 86 | Popular Tags |