* Perform blocking read with a timeout if desired\r
* @param timeout boolean - set to true if the system will time out\r
* @return boolean - true if data was read, false is EOF is reached\r
- * @throws IOException\r
+ * @throws IOException \r
*/\r
private boolean readSocket(boolean timeout) throws IOException {\r
int nRead = 0;\r
if (!committed) {\r
//Socket.send(socket, Constants.ACK_BYTES, 0, Constants.ACK_BYTES.length) < 0\r
ByteBuffer buf = ByteBuffer.wrap(Constants.ACK_BYTES,0,Constants.ACK_BYTES.length); \r
- writeToSocket(buf);\r
+ writeToSocket(buf,false);\r
}\r
\r
}\r
\r
- private void writeToSocket(ByteBuffer bytebuffer) throws IOException {\r
+ private synchronized void writeToSocket(ByteBuffer bytebuffer, boolean flip) throws IOException {\r
int limit = bytebuffer.position();\r
- bytebuffer.rewind();\r
- bytebuffer.limit(limit);\r
- int remaining = limit;\r
- while ( remaining > 0 ) {\r
+ if ( flip ) bytebuffer.flip();\r
+ while ( bytebuffer.hasRemaining() ) {\r
int written = socket.write(bytebuffer);\r
- remaining -= written;\r
}\r
bbuf.clear();\r
- bbuf.rewind();\r
- bbuf.limit(bbufLimit);\r
-\r
- //System.out.println("Written:"+limit);\r
this.total = 0;\r
} \r
\r
\r
//write to the socket, if there is anything to write\r
if (bbuf.position() > 0) {\r
- writeToSocket(bbuf);\r
+ writeToSocket(bbuf,true);\r
}\r
}\r
\r