Java Code Examples for java.util.Arrays#checkOffsetAndCount()
The following examples show how to use
java.util.Arrays#checkOffsetAndCount() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LimitedLengthInputStream.java From android_9.0.0_r45 with Apache License 2.0 | 6 votes |
@Override public int read(byte[] buffer, int offset, int byteCount) throws IOException { if (mOffset >= mEnd) { return -1; } final int arrayLength = buffer.length; Arrays.checkOffsetAndCount(arrayLength, offset, byteCount); if (mOffset > Long.MAX_VALUE - byteCount) { throw new IOException("offset out of bounds: " + mOffset + " + " + byteCount); } if (mOffset + byteCount > mEnd) { byteCount = (int) (mEnd - mOffset); } final int numRead = super.read(buffer, offset, byteCount); mOffset += numRead; return numRead; }
Example 2
Source File: Streams.java From jtransc with Apache License 2.0 | 6 votes |
/** * Reads exactly 'byteCount' bytes from 'in' (into 'dst' at offset 'offset'), and throws * EOFException if insufficient bytes are available. * * Used to implement {@link java.io.DataInputStream#readFully(byte[], int, int)}. */ public static void readFully(InputStream in, byte[] dst, int offset, int byteCount) throws IOException { if (byteCount == 0) { return; } if (in == null) { throw new NullPointerException("in == null"); } if (dst == null) { throw new NullPointerException("dst == null"); } Arrays.checkOffsetAndCount(dst.length, offset, byteCount); while (byteCount > 0) { int bytesRead = in.read(dst, offset, byteCount); if (bytesRead < 0) { throw new EOFException(); } offset += bytesRead; byteCount -= bytesRead; } }
Example 3
Source File: Parcel.java From android_9.0.0_r45 with Apache License 2.0 | 5 votes |
/** * Write a byte array into the parcel at the current {@link #dataPosition}, * growing {@link #dataCapacity} if needed. * @param b Bytes to place into the parcel. * @param offset Index of first byte to be written. * @param len Number of bytes to write. */ public final void writeByteArray(byte[] b, int offset, int len) { if (b == null) { writeInt(-1); return; } Arrays.checkOffsetAndCount(b.length, offset, len); nativeWriteByteArray(mNativePtr, b, offset, len); }
Example 4
Source File: FileBridge.java From android_9.0.0_r45 with Apache License 2.0 | 5 votes |
@Override public void write(byte[] buffer, int byteOffset, int byteCount) throws IOException { Arrays.checkOffsetAndCount(buffer.length, byteOffset, byteCount); Memory.pokeInt(mTemp, 0, CMD_WRITE, ByteOrder.BIG_ENDIAN); Memory.pokeInt(mTemp, 4, byteCount, ByteOrder.BIG_ENDIAN); IoBridge.write(mClient, mTemp, 0, MSG_LENGTH); IoBridge.write(mClient, buffer, byteOffset, byteCount); }
Example 5
Source File: ZipOutputStream.java From jtransc with Apache License 2.0 | 5 votes |
/** * Writes data for the current entry to the underlying stream. * * @exception IOException * If an error occurs writing to the stream */ @Override public void write(byte[] buffer, int offset, int byteCount) throws IOException { Arrays.checkOffsetAndCount(buffer.length, offset, byteCount); if (currentEntry == null) { throw new ZipException("No active entry"); } if (currentEntry.getMethod() == STORED) { out.write(buffer, offset, byteCount); } else { super.write(buffer, offset, byteCount); } crc.update(buffer, offset, byteCount); }
Example 6
Source File: DeflaterOutputStream.java From jtransc with Apache License 2.0 | 5 votes |
/** * Compresses {@code byteCount} bytes of data from {@code buf} starting at * {@code offset} and writes it to the underlying stream. * @throws IOException * If an error occurs during writing. */ @Override public void write(byte[] buffer, int offset, int byteCount) throws IOException { if (done) { throw new IOException("attempt to write after finish"); } Arrays.checkOffsetAndCount(buffer.length, offset, byteCount); if (!def.needsInput()) { throw new IOException(); } def.setInput(buffer, offset, byteCount); deflate(); }
Example 7
Source File: DeflaterInputStream.java From jtransc with Apache License 2.0 | 5 votes |
/** * Reads up to {@code byteCount} bytes of compressed data into a byte buffer. The result will be bytes of compressed * data corresponding to an uncompressed byte or bytes read from the underlying stream. * Returns the number of bytes read or -1 if the end of the compressed input * stream has been reached. */ @Override public int read(byte[] buffer, int byteOffset, int byteCount) throws IOException { checkClosed(); Arrays.checkOffsetAndCount(buffer.length, byteOffset, byteCount); if (byteCount == 0) { return 0; } if (!available) { return -1; } int count = 0; while (count < byteCount && !def.finished()) { if (def.needsInput()) { // read data from input stream int bytesRead = in.read(buf); if (bytesRead == -1) { def.finish(); } else { def.setInput(buf, 0, bytesRead); } } int bytesDeflated = def.deflate(buf, 0, Math.min(buf.length, byteCount - count)); if (bytesDeflated == -1) { break; } System.arraycopy(buf, 0, buffer, byteOffset + count, bytesDeflated); count += bytesDeflated; } if (count == 0) { count = -1; available = false; } return count; }
Example 8
Source File: GZIPInputStream.java From jtransc with Apache License 2.0 | 5 votes |
@Override public int read(byte[] buffer, int byteOffset, int byteCount) throws IOException { if (closed) { throw new IOException("Stream is closed"); } if (eos) { return -1; } Arrays.checkOffsetAndCount(buffer.length, byteOffset, byteCount); int bytesRead; try { bytesRead = super.read(buffer, byteOffset, byteCount); } finally { eos = eof; // update eos after every read(), even when it throws } if (bytesRead != -1) { crc.update(buffer, byteOffset, bytesRead); } if (eos) { verifyCrc(); } return bytesRead; }
Example 9
Source File: PushbackInputStream.java From CodenameOne with GNU General Public License v2.0 | 5 votes |
/** * Reads at most {@code length} bytes from this stream and stores them in * the byte array {@code buffer} starting at {@code offset}. Bytes are read * from the pushback buffer first, then from the source stream if more bytes * are required. Blocks until {@code count} bytes have been read, the end of * the source stream is detected or an exception is thrown. * * @param buffer * the array in which to store the bytes read from this stream. * @param offset * the initial position in {@code buffer} to store the bytes read * from this stream. * @param length * the maximum number of bytes to store in {@code buffer}. * @return the number of bytes read or -1 if the end of the source stream * has been reached. * @throws IndexOutOfBoundsException * if {@code offset < 0} or {@code length < 0}, or if * {@code offset + length} is greater than the length of * {@code buffer}. * @throws IOException * if this stream is closed or another I/O error occurs while * reading from this stream. * @throws NullPointerException * if {@code buffer} is {@code null}. */ @Override public int read(byte[] buffer, int offset, int length) throws IOException { if (buf == null) { throw streamClosed(); } Arrays.checkOffsetAndCount(buffer.length, offset, length); int copiedBytes = 0, copyLength = 0, newOffset = offset; // Are there pushback bytes available? if (pos < buf.length) { copyLength = (buf.length - pos >= length) ? length : buf.length - pos; System.arraycopy(buf, pos, buffer, newOffset, copyLength); newOffset += copyLength; copiedBytes += copyLength; // Use up the bytes in the local buffer pos += copyLength; } // Have we copied enough? if (copyLength == length) { return length; } int inCopied = in.read(buffer, newOffset, length - copiedBytes); if (inCopied > 0) { return inCopied + copiedBytes; } if (copiedBytes == 0) { return inCopied; } return copiedBytes; }
Example 10
Source File: ByteArrayInputStream.java From CodenameOne with GNU General Public License v2.0 | 4 votes |
/** * Reads at most {@code len} bytes from this stream and stores * them in byte array {@code b} starting at {@code offset}. This * implementation reads bytes from the source byte array. * * @param buffer * the byte array in which to store the bytes read. * @param offset * the initial position in {@code b} to store the bytes read from * this stream. * @param length * the maximum number of bytes to store in {@code b}. * @return the number of bytes actually read or -1 if no bytes were read and * the end of the stream was encountered. * @throws IndexOutOfBoundsException * if {@code offset < 0} or {@code length < 0}, or if * {@code offset + length} is greater than the size of * {@code b}. * @throws NullPointerException * if {@code b} is {@code null}. */ @Override public synchronized int read(byte[] buffer, int offset, int length) { Arrays.checkOffsetAndCount(buffer.length, offset, length); // Are there any bytes available? if (this.pos >= this.count) { return -1; } if (length == 0) { return 0; } int copylen = this.count - pos < length ? this.count - pos : length; System.arraycopy(this.buf, pos, buffer, offset, copylen); pos += copylen; return copylen; }
Example 11
Source File: Parcel.java From android_9.0.0_r45 with Apache License 2.0 | 3 votes |
/** * Write a blob of data into the parcel at the current {@link #dataPosition}, * growing {@link #dataCapacity} if needed. * @param b Bytes to place into the parcel. * @param offset Index of first byte to be written. * @param len Number of bytes to write. * {@hide} * {@SystemApi} */ public final void writeBlob(byte[] b, int offset, int len) { if (b == null) { writeInt(-1); return; } Arrays.checkOffsetAndCount(b.length, offset, len); nativeWriteBlob(mNativePtr, b, offset, len); }
Example 12
Source File: InflaterOutputStream.java From jtransc with Apache License 2.0 | 3 votes |
/** * Writes to the decompressing output stream. The {@code bytes} array should contain * compressed input. The corresponding uncompressed data will be written to the underlying * stream. * * @throws IOException if an I/O error occurs, or the stream has been closed * @throws ZipException if a zip exception occurs. * @throws NullPointerException if {@code b == null}. * @throws IndexOutOfBoundsException if {@code off < 0 || len < 0 || off + len > b.length} */ @Override public void write(byte[] bytes, int offset, int byteCount) throws IOException, ZipException { checkClosed(); Arrays.checkOffsetAndCount(bytes.length, offset, byteCount); inf.setInput(bytes, offset, byteCount); write(); }
Example 13
Source File: PushbackInputStream.java From CodenameOne with GNU General Public License v2.0 | 3 votes |
/** * Pushes a subset of the bytes in {@code buffer} back to this stream. The * subset is defined by the start position {@code offset} within * {@code buffer} and the number of bytes specified by {@code length}. The * bytes are pushed back in such a way that the next byte read from this * stream is {@code buffer[offset]}, then {@code buffer[1]} and so on. * <p> * If this stream's internal pushback buffer cannot store the selected * subset of {@code buffer}, an {@code IOException} is thrown. Parts of * {@code buffer} may have already been copied to the pushback buffer when * the exception is thrown. * * @param buffer * the buffer containing the bytes to push back to this stream. * @param offset * the index of the first byte in {@code buffer} to push back. * @param length * the number of bytes to push back. * @throws IndexOutOfBoundsException * if {@code offset < 0} or {@code length < 0}, or if * {@code offset + length} is greater than the length of * {@code buffer}. * @throws IOException * if the free space in the internal pushback buffer is not * sufficient to store the selected contents of {@code buffer}. */ public void unread(byte[] buffer, int offset, int length) throws IOException { if (length > pos) { throw new IOException("Pushback buffer full"); } Arrays.checkOffsetAndCount(buffer.length, offset, length); if (buf == null) { throw streamClosed(); } System.arraycopy(buffer, offset, buf, pos - length, length); pos = pos - length; }
Example 14
Source File: ByteArrayOutputStream.java From CodenameOne with GNU General Public License v2.0 | 3 votes |
/** * Writes {@code count} bytes from the byte array {@code buffer} starting at * offset {@code index} to this stream. * * @param buffer * the buffer to be written. * @param offset * the initial position in {@code buffer} to retrieve bytes. * @param len * the number of bytes of {@code buffer} to write. * @throws NullPointerException * if {@code buffer} is {@code null}. * @throws IndexOutOfBoundsException * if {@code offset < 0} or {@code len < 0}, or if * {@code offset + len} is greater than the length of * {@code buffer}. */ @Override public synchronized void write(byte[] buffer, int offset, int len) { Arrays.checkOffsetAndCount(buffer.length, offset, len); if (len == 0) { return; } expand(len); System.arraycopy(buffer, offset, buf, this.count, len); this.count += len; }
Example 15
Source File: FilterOutputStream.java From CodenameOne with GNU General Public License v2.0 | 3 votes |
/** * Writes {@code count} bytes from the byte array {@code buffer} starting at * {@code offset} to the target stream. * * @param buffer * the buffer to write. * @param offset * the index of the first byte in {@code buffer} to write. * @param length * the number of bytes in {@code buffer} to write. * @throws IndexOutOfBoundsException * if {@code offset < 0} or {@code count < 0}, or if * {@code offset + count} is bigger than the length of * {@code buffer}. * @throws IOException * if an I/O error occurs while writing to this stream. */ @Override public void write(byte[] buffer, int offset, int length) throws IOException { Arrays.checkOffsetAndCount(buffer.length, offset, length); for (int i = 0; i < length; i++) { // Call write() instead of out.write() since subclasses could // override the write() method. write(buffer[offset + i]); } }