Permalink
Browse files

Use readFully() instead of read() for reading in DefaultPDUReader.rea…

…dPDU(). Under some network conditions, it is possible for the code to receive a partial package over the socket and erronously throw an IOException for the socket. The specs for read() don't guarantee that the full requested length is read in one call. Using readFully() fixes that problem.
  • Loading branch information...
1 parent d16eb67 commit 82960633058da197b8d1f9203ba750d63638bd56 @stefanth committed Feb 14, 2012
@@ -16,7 +16,6 @@
import java.io.DataInputStream;
import java.io.IOException;
-import java.io.InputStream;
import org.jsmpp.bean.Command;
import org.jsmpp.util.OctetUtil;
@@ -57,7 +56,7 @@ public Command readPDUHeader(DataInputStream in)
/* (non-Javadoc)
* @see org.jsmpp.PDUReader#readPDU(java.io.InputStream, org.jsmpp.bean.Command)
*/
- public byte[] readPDU(InputStream in, Command pduHeader) throws IOException {
+ public byte[] readPDU(DataInputStream in, Command pduHeader) throws IOException {
return readPDU(in, pduHeader.getCommandLength(), pduHeader
.getCommandId(), pduHeader.getCommandStatus(), pduHeader
.getSequenceNumber());
@@ -66,7 +65,7 @@ public Command readPDUHeader(DataInputStream in)
/* (non-Javadoc)
* @see org.jsmpp.PDUReader#readPDU(java.io.InputStream, int, int, int, int)
*/
- public byte[] readPDU(InputStream in, int commandLength, int commandId,
+ public byte[] readPDU(DataInputStream in, int commandLength, int commandId,
int commandStatus, int sequenceNumber) throws IOException {
byte[] b = new byte[commandLength];
@@ -76,15 +75,8 @@ public Command readPDUHeader(DataInputStream in)
System.arraycopy(OctetUtil.intToBytes(sequenceNumber), 0, b, 12, 4);
if (commandLength > 16) {
- int len = commandLength - 16;
- int totalReaded = -1;
synchronized (in) {
- totalReaded = in.read(b, 16, commandLength - 16);
- }
- if (totalReaded != len) {
- throw new IOException(
- "Unexpected length of byte readed. Expecting " + len
- + " but only read " + totalReaded);
+ in.readFully(b, 16, commandLength - 16);
}
}
return b;
@@ -52,7 +52,7 @@ public Command readPDUHeader(DataInputStream in)
* @return the complete byte of smpp command.
* @throws IOException if an I/O error occurs.
*/
- public byte[] readPDU(InputStream in, Command pduHeader) throws IOException;
+ public byte[] readPDU(DataInputStream in, Command pduHeader) throws IOException;
/**
* Read all smpp pdu (excluding the command header) with specified pdu
@@ -66,7 +66,7 @@ public Command readPDUHeader(DataInputStream in)
* @return the complete byte of smpp command.
* @throws IOException if an I/O error occurs.
*/
- public byte[] readPDU(InputStream in, int commandLength, int commandId,
+ public byte[] readPDU(DataInputStream in, int commandLength, int commandId,
int commandStatus, int sequenceNumber) throws IOException;
}
@@ -56,7 +56,7 @@ public SynchronizedPDUReader(PDUReader pduReader) {
* @see org.jsmpp.PDUReader#readPDU(java.io.InputStream,
* org.jsmpp.bean.Command)
*/
- public byte[] readPDU(InputStream in, Command pduHeader) throws IOException {
+ public byte[] readPDU(DataInputStream in, Command pduHeader) throws IOException {
synchronized (in) {
return pduReader.readPDU(in, pduHeader);
}
@@ -68,7 +68,7 @@ public SynchronizedPDUReader(PDUReader pduReader) {
* @see org.jsmpp.PDUReader#readPDU(java.io.InputStream, int, int,
* int, int)
*/
- public byte[] readPDU(InputStream in, int commandLength, int commandId,
+ public byte[] readPDU(DataInputStream in, int commandLength, int commandId,
int commandStatus, int sequenceNumber) throws IOException {
synchronized (in) {
return pduReader.readPDU(in, commandLength, commandId,

0 comments on commit 8296063

Please sign in to comment.