1
2
3
4
5
6
7
8
9
10
11 package org.mule.transport.tcp.integration;
12
13 import org.mule.transport.tcp.protocols.AbstractByteProtocol;
14
15 import java.io.ByteArrayOutputStream;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.io.OutputStream;
19
20 public class CustomByteProtocol extends AbstractByteProtocol
21 {
22
23
24
25
26 public CustomByteProtocol()
27 {
28 super(false);
29 }
30
31
32
33
34 @Override
35 protected void writeByteArray(OutputStream os, byte[] data) throws IOException
36 {
37 super.writeByteArray(os, data);
38 os.write('>');
39 os.write('>');
40 os.write('>');
41 os.flush();
42 }
43
44
45
46
47 public Object read(InputStream is) throws IOException
48 {
49 ByteArrayOutputStream baos = new ByteArrayOutputStream();
50 int count = 0;
51 byte read[] = new byte[1];
52
53 while (true)
54 {
55
56 if (safeRead(is, read) < 0)
57 {
58
59
60 return null;
61 }
62 byte b = read[0];
63 if (b == '>')
64 {
65 count++;
66 if (count == 3)
67 {
68 return baos.toByteArray();
69 }
70 }
71 else
72 {
73 for (int i = 0; i < count; i++)
74 {
75 baos.write('>');
76 }
77 count = 0;
78 baos.write(b);
79 }
80 }
81 }
82 }