/* * Copyright 2002-2019 Drew Noakes and contributors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * More information about this project is available at: * * https://drewnoakes.com/code/exif/ * https://github.com/drewnoakes/metadata-extractor */ package com.drew.lang; import com.drew.lang.annotations.NotNull; import com.drew.lang.annotations.Nullable; import com.drew.metadata.StringValue; import java.io.EOFException; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.nio.charset.Charset; /** * @author Drew Noakes https://drewnoakes.com */ @SuppressWarnings("WeakerAccess") public abstract class SequentialReader { // TODO review whether the masks are needed (in both this and RandomAccessReader) private boolean _isMotorolaByteOrder = true; public abstract long getPosition() throws IOException; /** * Gets the next byte in the sequence. * * @return The read byte value */ public abstract byte getByte() throws IOException; /** * Returns the required number of bytes from the sequence. * * @param count The number of bytes to be returned * @return The requested bytes */ @NotNull public abstract byte[] getBytes(int count) throws IOException; /** * Retrieves bytes, writing them into a caller-provided buffer. * @param buffer The array to write bytes to. * @param offset The starting position within buffer to write to. * @param count The number of bytes to be written. */ public abstract void getBytes(@NotNull byte[] buffer, int offset, int count) throws IOException; /** * Skips forward in the sequence. If the sequence ends, an {@link EOFException} is thrown. * * @param n the number of byte to skip. Must be zero or greater. * @throws EOFException the end of the sequence is reached. * @throws IOException an error occurred reading from the underlying source. */ public abstract void skip(long n) throws IOException; /** * Skips forward in the sequence, returning a boolean indicating whether the skip succeeded, or whether the sequence ended. * * @param n the number of byte to skip. Must be zero or greater. * @return a boolean indicating whether the skip succeeded, or whether the sequence ended. * @throws IOException an error occurred reading from the underlying source. */ public abstract boolean trySkip(long n) throws IOException; /** * Returns an estimate of the number of bytes that can be read (or skipped * over) from this {@link SequentialReader} without blocking by the next * invocation of a method for this input stream. A single read or skip of * this many bytes will not block, but may read or skip fewer bytes. *
* Note that while some implementations of {@link SequentialReader} like * {@link SequentialByteArrayReader} will return the total remaining number * of bytes in the stream, others will not. It is never correct to use the * return value of this method to allocate a buffer intended to hold all * data in this stream. * * @return an estimate of the number of bytes that can be read (or skipped * over) from this {@link SequentialReader} without blocking or * {@code 0} when it reaches the end of the input stream. */ public abstract int available(); /** * Sets the endianness of this reader. *
true
for Motorola (or big) endianness (also known as network byte order), with MSB before LSB.false
for Intel (or little) endianness, with LSB before MSB.true
for Motorola/big endian, false
for Intel/little endian
*/
public void setMotorolaByteOrder(boolean motorolaByteOrder)
{
_isMotorolaByteOrder = motorolaByteOrder;
}
/**
* Gets the endianness of this reader.
* true
for Motorola (or big) endianness (also known as network byte order), with MSB before LSB.false
for Intel (or little) endianness, with LSB before MSB.
* This particular fixed point encoding has one sign bit, 15 numerator bits and 16 denominator bits.
*
* @return the floating point value
* @throws IOException the buffer does not contain enough bytes to service the request
*/
public float getS15Fixed16() throws IOException
{
if (_isMotorolaByteOrder) {
float res = (getByte() & 0xFF) << 8 |
(getByte() & 0xFF);
int d = (getByte() & 0xFF) << 8 |
(getByte() & 0xFF);
return (float)(res + d/65536.0);
} else {
// this particular branch is untested
int d = (getByte() & 0xFF) |
(getByte() & 0xFF) << 8;
float res = (getByte() & 0xFF) |
(getByte() & 0xFF) << 8;
return (float)(res + d/65536.0);
}
}
public float getFloat32() throws IOException
{
return Float.intBitsToFloat(getInt32());
}
public double getDouble64() throws IOException
{
return Double.longBitsToDouble(getInt64());
}
@NotNull
public String getString(int bytesRequested) throws IOException
{
return new String(getBytes(bytesRequested));
}
@NotNull
public String getString(int bytesRequested, String charset) throws IOException
{
byte[] bytes = getBytes(bytesRequested);
try {
return new String(bytes, charset);
} catch (UnsupportedEncodingException e) {
return new String(bytes);
}
}
@NotNull
public String getString(int bytesRequested, @NotNull Charset charset) throws IOException
{
byte[] bytes = getBytes(bytesRequested);
return new String(bytes, charset);
}
@NotNull
public StringValue getStringValue(int bytesRequested, @Nullable Charset charset) throws IOException
{
return new StringValue(getBytes(bytesRequested), charset);
}
/**
* Creates a String from the stream, ending where byte=='\0'
or where length==maxLength
.
*
* @param maxLengthBytes The maximum number of bytes to read. If a zero-byte is not reached within this limit,
* reading will stop and the string will be truncated to this length.
* @return The read string.
* @throws IOException The buffer does not contain enough bytes to satisfy this request.
*/
@NotNull
public String getNullTerminatedString(int maxLengthBytes, Charset charset) throws IOException
{
return getNullTerminatedStringValue(maxLengthBytes, charset).toString();
}
/**
* Creates a String from the stream, ending where byte=='\0'
or where length==maxLength
.
*
* @param maxLengthBytes The maximum number of bytes to read. If a \0
byte is not reached within this limit,
* reading will stop and the string will be truncated to this length.
* @param charset The Charset
to register with the returned StringValue
, or null
if the encoding
* is unknown
* @return The read string.
* @throws IOException The buffer does not contain enough bytes to satisfy this request.
*/
@NotNull
public StringValue getNullTerminatedStringValue(int maxLengthBytes, Charset charset) throws IOException
{
byte[] bytes = getNullTerminatedBytes(maxLengthBytes);
return new StringValue(bytes, charset);
}
/**
* Returns the sequence of bytes punctuated by a \0
value.
*
* @param maxLengthBytes The maximum number of bytes to read. If a \0
byte is not reached within this limit,
* the returned array will be maxLengthBytes
long.
* @return The read byte array, excluding the null terminator.
* @throws IOException The buffer does not contain enough bytes to satisfy this request.
*/
@NotNull
public byte[] getNullTerminatedBytes(int maxLengthBytes) throws IOException
{
byte[] buffer = new byte[maxLengthBytes];
// Count the number of non-null bytes
int length = 0;
while (length < buffer.length && (buffer[length] = getByte()) != 0)
length++;
if (length == maxLengthBytes)
return buffer;
byte[] bytes = new byte[length];
if (length > 0)
System.arraycopy(buffer, 0, bytes, 0, length);
return bytes;
}
}