/** * Reads the next float value (32 bits) from the stream. This method ensures that there is at * least 4 bytes remaining in the buffer, reading new bytes from the channel if necessary, * then delegates to {@link ByteBuffer#getFloat()}. * * @return the value of the next float from the stream. * @throws IOException if an error (including EOF) occurred while reading the stream. */ public final float readFloat() throws IOException { ensureBufferContains(Float.BYTES); return buffer.getFloat(); }
/** * Reads the next byte value (8 bits) from the stream. This method ensures that there is at * least 1 byte remaining in the buffer, reading new bytes from the channel if necessary, * then delegates to {@link ByteBuffer#get()}. * * @return the value of the next byte from the stream. * @throws IOException if an error (including EOF) occurred while reading the stream. */ public final byte readByte() throws IOException { ensureBufferContains(Byte.BYTES); return buffer.get(); }
/** * Reads the next short value (16 bits) from the stream. This method ensures that there is at * least 2 bytes remaining in the buffer, reading new bytes from the channel if necessary, * then delegates to {@link ByteBuffer#getShort()}. * * @return the value of the next short from the stream. * @throws IOException if an error (including EOF) occurred while reading the stream. */ public final short readShort() throws IOException { ensureBufferContains(Short.BYTES); return buffer.getShort(); }
/** * Reads the next character (16 bits) from the stream. This method ensures that there is at * least 2 bytes remaining in the buffer, reading new bytes from the channel if necessary, * then delegates to {@link ByteBuffer#getChar()}. * * @return the value of the next character from the stream. * @throws IOException if an error (including EOF) occurred while reading the stream. */ public final char readChar() throws IOException { ensureBufferContains(Character.BYTES); return buffer.getChar(); }
/** * Reads the next short value (16 bits) from the stream. This method ensures that there is at * least 2 bytes remaining in the buffer, reading new bytes from the channel if necessary, * then delegates to {@link ByteBuffer#getShort()}. * * @return the value of the next short from the stream. * @throws IOException if an error (including EOF) occurred while reading the stream. */ public final short readShort() throws IOException { ensureBufferContains(Short.SIZE / Byte.SIZE); return buffer.getShort(); }
/** * Reads the next long value (64 bits) from the stream. This method ensures that there is at * least 8 bytes remaining in the buffer, reading new bytes from the channel if necessary, * then delegates to {@link ByteBuffer#getLong()}. * * @return the value of the next integer from the stream. * @throws IOException if an error (including EOF) occurred while reading the stream. */ public final long readLong() throws IOException { ensureBufferContains(Long.SIZE / Byte.SIZE); return buffer.getLong(); }
/** * Reads the next float value (32 bits) from the stream. This method ensures that there is at * least 4 bytes remaining in the buffer, reading new bytes from the channel if necessary, * then delegates to {@link ByteBuffer#getFloat()}. * * @return the value of the next float from the stream. * @throws IOException if an error (including EOF) occurred while reading the stream. */ public final float readFloat() throws IOException { ensureBufferContains(Float.SIZE / Byte.SIZE); return buffer.getFloat(); }
/** * Reads the next double value (64 bits) from the stream. This method ensures that there is at * least 8 bytes remaining in the buffer, reading new bytes from the channel if necessary, * then delegates to {@link ByteBuffer#getDouble()}. * * @return the value of the next double from the stream. * @throws IOException if an error (including EOF) occurred while reading the stream. */ public final double readDouble() throws IOException { ensureBufferContains(Double.SIZE / Byte.SIZE); return buffer.getDouble(); }
/** * Reads the next byte value (8 bits) from the stream. This method ensures that there is at * least 1 byte remaining in the buffer, reading new bytes from the channel if necessary, * then delegates to {@link ByteBuffer#get()}. * * @return the value of the next byte from the stream. * @throws IOException if an error (including EOF) occurred while reading the stream. */ public final byte readByte() throws IOException { ensureBufferContains(Byte.SIZE / Byte.SIZE); return buffer.get(); }
/** * Reads the next character (16 bits) from the stream. This method ensures that there is at * least 2 bytes remaining in the buffer, reading new bytes from the channel if necessary, * then delegates to {@link ByteBuffer#getChar()}. * * @return the value of the next character from the stream. * @throws IOException if an error (including EOF) occurred while reading the stream. */ public final char readChar() throws IOException { ensureBufferContains(Character.SIZE / Byte.SIZE); return buffer.getChar(); }
/** * Reads the next integer value (32 bits) from the stream. This method ensures that there is at * least 4 bytes remaining in the buffer, reading new bytes from the channel if necessary, then * delegates to {@link ByteBuffer#getInt()}. * * @return the value of the next integer from the stream. * @throws IOException if an error (including EOF) occurred while reading the stream. */ public final int readInt() throws IOException { ensureBufferContains(Integer.SIZE / Byte.SIZE); return buffer.getInt(); }
/** * Reads the next integer value (32 bits) from the stream. This method ensures that there is at * least 4 bytes remaining in the buffer, reading new bytes from the channel if necessary, then * delegates to {@link ByteBuffer#getInt()}. * * @return the value of the next integer from the stream. * @throws IOException if an error (including EOF) occurred while reading the stream. */ public final int readInt() throws IOException { ensureBufferContains(Integer.BYTES); return buffer.getInt(); }
/** * Reads the next long value (64 bits) from the stream. This method ensures that there is at * least 8 bytes remaining in the buffer, reading new bytes from the channel if necessary, * then delegates to {@link ByteBuffer#getLong()}. * * @return the value of the next integer from the stream. * @throws IOException if an error (including EOF) occurred while reading the stream. */ public final long readLong() throws IOException { ensureBufferContains(Long.BYTES); return buffer.getLong(); }
/** * Reads the next double value (64 bits) from the stream. This method ensures that there is at * least 8 bytes remaining in the buffer, reading new bytes from the channel if necessary, * then delegates to {@link ByteBuffer#getDouble()}. * * @return the value of the next double from the stream. * @throws IOException if an error (including EOF) occurred while reading the stream. */ public final double readDouble() throws IOException { ensureBufferContains(Double.BYTES); return buffer.getDouble(); }
/** * Aligns position in the stream after reading the given amount of bytes. * This method should be invoked only for {@link DataType#BYTE} and {@link DataType#CHAR}. * * <p>The netCDF format adds padding after bytes, characters and short integers in order to align the data * on multiple of 4 bytes. This method is used for adding such padding to the number of bytes to read.</p> * * @param length number of byte reads. * @throws IOException if an error occurred while skipping bytes. */ private void align(int length) throws IOException { length &= 3; if (length != 0) { length = 4 - length; input.ensureBufferContains(length); input.buffer.position(input.buffer.position() + length); } }
/** * Aligns position in the stream after reading the given amount of bytes. * This method should be invoked only for {@link DataType#BYTE} and {@link DataType#CHAR}. * * <p>The netCDF format adds padding after bytes, characters and short integers in order to align the data * on multiple of 4 bytes. This method is used for adding such padding to the number of bytes to read.</p> * * @param length number of byte reads. * @throws IOException if an error occurred while skipping bytes. */ private void align(int length) throws IOException { length &= 3; if (length != 0) { length = 4 - length; input.ensureBufferContains(length); input.buffer.position(input.buffer.position() + length); } }
/** * Decodes a string from a sequence of bytes in the given encoding. This method tries to avoid the creation * of a temporary {@code byte[]} array when possible. * * <p>This convenience method shall be used only for relatively small amount of {@link String} instances * to decode, for example attribute values in the file header. For large amount of data, consider using * {@link java.nio.charset.CharsetDecoder} instead.</p> * * @param length number of bytes to read. * @param encoding the character encoding. * @return the string decoded from the {@code length} next bytes. * @throws IOException if an error occurred while reading the bytes, or if the given encoding is invalid. */ public final String readString(final int length, final Charset encoding) throws IOException { if (buffer.hasArray() && length <= buffer.capacity()) { ensureBufferContains(length); final int position = buffer.position(); // Must be after 'ensureBufferContains(int)'. buffer.position(position + length); // Before 'new String' for consistency with the 'else' block in case of UnsupportedEncodingException. return new String(buffer.array(), buffer.arrayOffset() + position, length, encoding); } else { return new String(readBytes(length), encoding); } }
int position; if (buffer.hasArray() && length <= buffer.capacity()) { ensureBufferContains(length);
public void readFully(Buffer view, int offset, int length) throws IOException { final int dataSizeShift = dataSizeShift(); ensureBufferContains(Math.min(length << dataSizeShift, buffer.capacity())); if (view == null) { while ((length -= n) != 0) { offset += n; ensureBufferContains(1 << dataSizeShift); // Actually read as much data as possible. view.rewind().limit(buffer.remaining() >> dataSizeShift); transfer(offset, n = Math.min(view.remaining(), length));
public void readFully(Buffer view, int offset, int length) throws IOException { final int dataSizeShift = dataSizeShift(); ensureBufferContains(Math.min(length << dataSizeShift, buffer.capacity())); if (view == null) { while ((length -= n) != 0) { offset += n; ensureBufferContains(1 << dataSizeShift); // Actually read as much data as possible. view.rewind().limit(buffer.remaining() >> dataSizeShift); transfer(offset, n = Math.min(view.remaining(), length));