public int read(final byte[] b) throws IOException { final int result = drain(b); /* As drain(byte[] dst) never returns a negative result, * we need to perform some additional checks to honor * the contract of InputStream.read(byte[]). Citing from its JavaDoc: * * If the length of <code>b</code> is zero, then no bytes are read and * <code>0</code> is returned; otherwise, there is an attempt to read at * least one byte. If no byte is available because the stream is at the * end of the file, the value <code>-1</code> is returned; otherwise, at * least one byte is read and stored into <code>b</code>. */ return result == 0 ? (b.length == 0 ? 0 : -1) : result; }
/** * Drains up to {@code dst.length} bytes from this iterator into the given {@code dst} array. * An attempt is made to drain as many as {@code dst.length} bytes, but a smaller number may * be drained. * <p> * The number of bytes actually drained is returned as an integer. Unlike * {@link InputStream#read(byte[], int, int)}, this method never returns a negative result. * * @param dst the buffer into which the data is drained * @return the total number of bytes drained into {@code dst}, always greater or equal to {@code 0} */ public int drain(byte[] dst) { return drain(dst, 0, dst.length); }
public int read(final byte[] b, final int off, final int len) throws IOException { final int result = drain(b, off, len); /* As drain(byte[] dst, int offs, int len) never returns a negative result, * we need to perform some additional checks to honor * the contract of InputStream.read(byte[], int, int). Citing from its JavaDoc: * * <p> If <code>len</code> is zero, then no bytes are read and * <code>0</code> is returned; otherwise, there is an attempt to read at * least one byte. If no byte is available because the stream is at end of * file, the value <code>-1</code> is returned; otherwise, at least one * byte is read and stored into <code>b</code>. */ return result == 0 ? (len == 0 ? 0 : -1) : result; } };
/** * Convenience method to directly drain a certain number of bytes to a UTF-8 string. If fewer than {@code count} * bytes are available, only the available bytes will be used to construct the string. * * @param count the maximum number of bytes to consume * @return the UTF-8 string */ public String drainToUtf8(int count) { return new String(drain(count), StandardCharsets.UTF_8); }
/** * Drain up to {@code count} bytes from this iterator, returning the result. * * @param count the number of bytes to read * @return the array of consumed bytes (may be smaller than {@code count}) */ public byte[] drain(int count) { if (count == 0) return NO_BYTES; final byte[] b = new byte[count]; final int cnt = drain(b); return cnt == 0 ? NO_BYTES : cnt < b.length ? Arrays.copyOf(b, cnt) : b; }
/** * Convenience method to directly drain a certain number of bytes to a Latin-1 string. If fewer than {@code count} * bytes are available, only the available bytes will be used to construct the string. * * @param count the maximum number of bytes to consume * @return the Latin-1 string */ public String drainToLatin1(int count) { return new String(drain(count), StandardCharsets.ISO_8859_1); }
public void update(Signature signature) throws IllegalStateException { final byte[] buffer = OP_BUFFER.get(); try { int cnt = drain(buffer); while (cnt > 0) { signature.update(buffer, 0, cnt); cnt = drain(buffer); } signature.update(drain()); } catch (SignatureException e) { throw new IllegalStateException(e); } }
public int drain(final byte[] dst, final int offs, final int len) { return super.drain(dst, offs, Math.min(len, size - offset)); }
/** * Drain exactly {@code count} bytes from this iterator, returning the result. * * @param count the number of bytes to read * @return the array of consumed bytes * @throws NoSuchElementException if there are not enough bytes to fill the array */ public byte[] drainAll(int count) throws NoSuchElementException { if (count == 0) return NO_BYTES; final byte[] b = new byte[count]; final int cnt = drain(b); if (cnt < b.length) { throw new NoSuchElementException(); } return b; }
public boolean verify(Signature signature) throws IllegalStateException { final byte[] buffer = OP_BUFFER.get(); try { int cnt = drain(buffer); while (cnt > 0) { signature.update(buffer, 0, cnt); cnt = drain(buffer); } return signature.verify(NO_BYTES); } catch (SignatureException e) { throw new IllegalStateException(e); } }
public ByteIterator doFinal(Mac mac) throws IllegalStateException { return ByteIterator.ofBytes(mac.doFinal(drain())); }
public int read(final byte[] b) throws IOException { final int result = drain(b); /* As drain(byte[] dst) never returns a negative result, * we need to perform some additional checks to honor * the contract of InputStream.read(byte[]). Citing from its JavaDoc: * * If the length of <code>b</code> is zero, then no bytes are read and * <code>0</code> is returned; otherwise, there is an attempt to read at * least one byte. If no byte is available because the stream is at the * end of the file, the value <code>-1</code> is returned; otherwise, at * least one byte is read and stored into <code>b</code>. */ return result == 0 ? (b.length == 0 ? 0 : -1) : result; }
/** * Drain up to {@code count} bytes from this iterator, returning the result. * * @param count the number of bytes to read * @return the array of consumed bytes (may be smaller than {@code count}) */ public byte[] drain(int count) { if (count == 0) return NO_BYTES; final byte[] b = new byte[count]; final int cnt = drain(b); return cnt == 0 ? NO_BYTES : cnt < b.length ? Arrays.copyOf(b, cnt) : b; }
/** * Drain up to {@code count} bytes from this iterator, returning the result. * * @param count the number of bytes to read * @return the array of consumed bytes (may be smaller than {@code count}) */ public byte[] drain(int count) { if (count == 0) return NO_BYTES; final byte[] b = new byte[count]; final int cnt = drain(b); return cnt == 0 ? NO_BYTES : cnt < b.length ? Arrays.copyOf(b, cnt) : b; }