Returns the given input stream's contents as a byte array.
If a length is specified (ie. if length != -1), only length bytes are returned. Otherwise all bytes in the stream are returned. Note this doesn't close the stream.
00050 {
00051 byte[] contents;
00052 if (length == -1) {
00053 contents = new byte[0];
00054 int contentsLength = 0;
00055 int bytesRead = -1;
00056 do {
00057 int available = stream.available();
00058
00059
00060 if (contentsLength + available > contents.length) {
00061 System.arraycopy(contents, 0,
00062 contents = new byte[contentsLength + available], 0,
00063 contentsLength);
00064 }
00065
00066
00067 bytesRead = stream.read(contents, contentsLength, available);
00068
00069 if (bytesRead > 0) {
00070
00071 contentsLength += bytesRead;
00072 }
00073 } while (bytesRead > 0);
00074
00075
00076 if (contentsLength < contents.length) {
00077 System.arraycopy(contents, 0,
00078 contents = new byte[contentsLength], 0, contentsLength);
00079 }
00080 } else {
00081 contents = new byte[length];
00082 int len = 0;
00083 int readSize = 0;
00084 while ((readSize != -1) && (len != length)) {
00085
00086
00087
00088 len += readSize;
00089 readSize = stream.read(contents, len, length - len);
00090 }
00091 }
00092
00093 return contents;
00094 }