public static void copy( @NotNull InputStream inputStream, int maxSize, @NotNull OutputStream outputStream) throws IOException { final byte[] buffer = BUFFER.get(); int toRead = maxSize; while (toRead > 0) { int read = inputStream.read(buffer, 0, Math.min(buffer.length, toRead)); if (read < 0) break; toRead -= read; outputStream.write(buffer, 0, read); } }
public static boolean processFirstBytes( @NotNull InputStream stream, int length, @NotNull Processor<ByteSequence> processor) throws IOException { final byte[] bytes = BUFFER.get(); assert bytes.length >= length : "Cannot process more than " + bytes.length + " in one call, requested:" + length; int n = stream.read(bytes, 0, length); if (n <= 0) return false; return processor.process(new ByteSequence(bytes, 0, n)); }
@NotNull public static byte[] loadFirst(@NotNull InputStream stream, int maxLength) throws IOException { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); final byte[] bytes = BUFFER.get(); while (maxLength > 0) { int n = stream.read(bytes, 0, Math.min(maxLength, bytes.length)); if (n <= 0) break; buffer.write(bytes, 0, n); maxLength -= n; } buffer.close(); return buffer.toByteArray(); }