Tuesday, May 17, 2011

Read partial byte array using ByteArrayInputStream Example

We can write byte array completely as well as partially, i.e. we can write byte array from one index to other. We have already written byte array completely here. We can read byte array partially like this:
/* uses ByteArrayInputStream(byte[] b, int offset, int length)
*/
public static void printPartialByteArray(byte[] bytes, offset, 
length)
ByteArrayInputStream bis = new ByteArrayInputStream(bytes, 
offset, length);
int ch;

while ((ch = bis.read()) != -1)
{
System.out.print((char)ch);
}

}



Using the above function:

String str = "Partial Byte Array InputStream Example";

//get bytes from string
byte[] bytes = str.getBytes();
printPartialByteArray(bytes,5,5);

No comments:

Post a Comment

Chitika