/* * Copyright 2002-2015 Drew Noakes * * 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 java.io.EOFException; import java.io.IOException; import java.io.UnsupportedEncodingException; /** * @author Drew Noakes https://drewnoakes.com */ public abstract class SequentialReader { // TODO review whether the masks are needed (in both this and RandomAccessReader) private boolean _isMotorolaByteOrder = true; /** * Gets the next byte in the sequence. * * @return The read byte value */ protected 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; /** * 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; /** * 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);
}
}
/**
* 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) throws IOException
{
// NOTE currently only really suited to single-byte character strings
byte[] bytes = new byte[maxLengthBytes];
// Count the number of non-null bytes
int length = 0;
while (length < bytes.length && (bytes[length] = getByte()) != '\0')
length++;
return new String(bytes, 0, length);
}
}