Skip to content

Instantly share code, notes, and snippets.

@alphazero
Created October 9, 2009 15:17
Show Gist options
  • Save alphazero/206106 to your computer and use it in GitHub Desktop.
Save alphazero/206106 to your computer and use it in GitHub Desktop.
/**
* Will read up expected bulkdata bytes from the input stream. Routine will
* also read in the last two bytes and will check that they are indeed CRLF.
*
* @param in the stream to read from.
* @param length expected bulk data length (NOT including the trailing CRLF).
* @return a byte[] of length.
* @throws IOException
* @throws IllegalArgumentException if could not read the bulk data
*/
public final byte[] readBulkData (InputStream in, int length)
throws IOException, RuntimeException
{
byte[] data = new byte[length]; // TODO: optimize me
// byte[] term = new byte[CRLF.length]; // FIX: http://github.com/alphazero/jredis/issues#issue/5 -- N/A
int readcnt = -1;
int offset = 0;
while(offset < length){
if((readcnt = in.read (data, offset, length-offset)) ==-1 ) throw new ClientRuntimeException("IO - read returned -1 -- problem");
offset += readcnt;
}
// FIX: http://github.com/alphazero/jredis/issues#issue/5 -- BEGIN
for(int i=0; i<CRLF_LEN; i++){
if (in.read() == -1){
throw new RuntimeException ("read got EOF (-1) while consuming the " +(i+1)+ "-th byte of CRLF bytes!");
}
}
// if((readcnt = in.read (term, 0, CRLF.length)) != CRLF.length) {
// throw new RuntimeException ("Only read " + readcnt + " bytes for CRLF!");
// }
// http://github.com/alphazero/jredis/issues#issue/5 -- END
return data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment