AndroidAsync icon indicating copy to clipboard operation
AndroidAsync copied to clipboard

Problem caused by too long byte[]

Open siyujie opened this issue 6 years ago • 0 comments

It is possible to support reading byte[] of a specified length. Reading a very long byte[] now requires writing a lot of unpacking logic.Like this:

public static ByteString read(InputStream in, int byteCount) throws IOException {

if (in == null) throw new IllegalArgumentException("in == null");

if (byteCount < 0) throw new IllegalArgumentException("byteCount < 0: " + byteCount);

byte[] result = new byte[byteCount];

for (int offset = 0, read; offset < byteCount; offset += read) {

read = in.read(result, offset, byteCount - offset);

if (read == -1) throw new EOFException();

}

return new ByteString(result);

}

siyujie avatar Aug 22 '19 10:08 siyujie