1 10 11 package org.mule.providers.tcp.protocols; 12 13 import org.mule.providers.tcp.TcpProtocol; 14 15 import java.io.DataInputStream ; 16 import java.io.DataOutputStream ; 17 import java.io.IOException ; 18 import java.io.InputStream ; 19 import java.io.OutputStream ; 20 21 30 public class LengthProtocol implements TcpProtocol 31 { 32 33 public byte[] read(InputStream is) throws IOException 34 { 35 DataInputStream dis = new DataInputStream (is); 42 byte[] buffer = new byte[32]; 43 int length; 44 dis.mark(32); 45 while ((length = dis.read(buffer)) == 0) 46 { 47 } 49 if (length == -1) 50 { 51 return null; 52 } 53 dis.reset(); 54 length = dis.readInt(); 55 buffer = new byte[length]; 56 dis.readFully(buffer); 57 return buffer; 58 } 59 60 public void write(OutputStream os, byte[] data) throws IOException 61 { 62 DataOutputStream dos = new DataOutputStream (os); 64 dos.writeInt(data.length); 65 dos.write(data); 66 dos.flush(); 67 } 68 69 } 70 | Popular Tags |