KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > tcp > protocols > DefaultProtocol


1 /*
2  * $Id: DefaultProtocol.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

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 JavaDoc;
19 import java.io.InputStream JavaDoc;
20 import java.io.OutputStream JavaDoc;
21 import java.net.SocketException JavaDoc;
22 import java.net.SocketTimeoutException JavaDoc;
23
24 /**
25  * The DefaultProtocol class is an application level tcp protocol that does nothing.
26  * Reading is performed in reading the socket until no more bytes are available.
27  * Writing simply writes the data to the socket.
28  *
29  * @author <a HREF="mailto:gnt@codehaus.org">Guillaume Nodet</a>
30  * @version $Revision: 3798 $
31  */

32 public class DefaultProtocol implements TcpProtocol
33 {
34
35     private static final int BUFFER_SIZE = 8192;
36
37     private static final Log logger = LogFactory.getLog(DefaultProtocol.class);
38
39     public byte[] read(InputStream JavaDoc is) throws IOException JavaDoc
40     {
41         ByteArrayOutputStream baos = new ByteArrayOutputStream(BUFFER_SIZE);
42
43         byte[] buffer = new byte[BUFFER_SIZE];
44         int len = 0;
45         try
46         {
47             while ((len = is.read(buffer)) == 0)
48             {
49                 // wait
50
}
51         }
52         catch (SocketException JavaDoc e)
53         {
54             // do not pollute the log with a stacktrace, log only the message
55
logger.debug("Socket exception occured: " + e.getMessage());
56             return null;
57         }
58         catch (SocketTimeoutException JavaDoc e)
59         {
60             logger.debug("Socket timeout, returning null.");
61             return null;
62         }
63         if (len == -1)
64         {
65             return null;
66         }
67         else
68         {
69             do
70             {
71                 baos.write(buffer, 0, len);
72                 if (len < buffer.length)
73                 {
74                     break;
75                 }
76                 int av = is.available();
77                 if (av == 0)
78                 {
79                     break;
80                 }
81             }
82             while ((len = is.read(buffer)) > 0);
83
84             baos.flush();
85             baos.close();
86             return baos.toByteArray();
87         }
88     }
89
90     public void write(OutputStream os, byte[] data) throws IOException JavaDoc
91     {
92         os.write(data);
93     }
94
95 }
96
Popular Tags