Rev 531 |
Blame |
Compare with Previous |
Last modification |
View Log
| RSS feed
/*
* Copyright (C) 2015 Steve Ratcliffe
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 or
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
package test.files;
import java.io.IOException;
import uk.me.parabola.imgfmt.ReadFailedException;
import uk.me.parabola.imgfmt.app.ImgFileReader;
public class ByteImgFileReader
implements ImgFileReader
{
private final byte[] bytes
;
private int offset
;
public ByteImgFileReader
(byte[] bytes
) {
this.
bytes = bytes
;
}
/**
* Get the position. Needed because may not be reflected in the underlying
* file if being buffered.
*
* @return The logical position within the file.
*/
@
Override
public long position
() {
return offset
;
}
/**
* Set the position of the file.
*
* @param pos The new position in the file.
*/
@
Override
public void position
(long pos
) {
offset =
(int) pos
;
}
/**
* Read in a single byte.
*
* @return The byte that was read.
*/
@
Override
public byte get
() throws ReadFailedException
{
return bytes
[offset++
];
}
@
Override
public int get1u
() throws ReadFailedException
{
return bytes
[offset++
] & 0xff
;
}
@
Override
public int get1s
() throws ReadFailedException
{
return bytes
[offset++
];
}
/**
* Read in two bytes. Done in the correct byte order.
*
* @return The 2 byte integer that was read.
*/
@
Override
public int get2u
() throws ReadFailedException
{
byte b1 = bytes
[offset++
];
byte b2 = bytes
[offset++
];
return (char) (((b2
& 0xff
) << 8) +
(b1
& 0xff
));
}
@
Override
public int get2s
() throws ReadFailedException
{
byte b1 = bytes
[offset++
];
byte b2 = bytes
[offset++
];
return (b2
<< 8) |
(b1
& 0xff
);
}
/**
* Get a 3byte signed quantity.
*
* @return The value read.
* @throws ReadFailedException When the file cannot be read.
*/
@
Override
public int get3s
() throws ReadFailedException
{
int val = get3u
();
if ((val
& 0x800000
) !=
0)
val |= 0xff000000
;
return val
;
}
/**
* Get a 3byte unsigned quantity.
*
* @return The value read.
* @throws ReadFailedException When the file cannot be read.
*/
@
Override
public int get3u
() throws ReadFailedException
{
byte b1 = bytes
[offset++
];
byte b2 = bytes
[offset++
];
byte b3 = bytes
[offset++
];
return ((b3
& 0xff
) << 16) +
((b2
& 0xff
) << 8) +
(b1
& 0xff
);
}
/**
* Read in a 4 byte value.
*
* @return A 4 byte integer.
*/
@
Override
public int get4
() throws ReadFailedException
{
throw new UnsupportedOperationException();
}
/**
* Read a variable sized integer. The size is given.
*
* @param n The size of the integer to read. Must be 1 to 4.
* @return The integer which will not be sign extended if it is less
* than 4 bytes long.
*/
@
Override
public int getNu
(int n
) throws ReadFailedException
{
throw new UnsupportedOperationException();
}
/**
* Read in an arbitrary length sequence of bytes.
*
* @param len The number of bytes to read.
*/
@
Override
public byte[] get
(int len
) throws ReadFailedException
{
byte[] res =
new byte[len
];
System.
arraycopy(bytes, offset, res,
0, len
);
offset += len
;
return res
;
}
/**
* Read a zero terminated string from the file.
*
* @return A string
* @throws ReadFailedException For failures.
*/
@
Override
public byte[] getZString
() throws ReadFailedException
{
throw new UnsupportedOperationException();
}
/**
* Read in a string of digits in the compressed base 11 format that is used
* for phone numbers in the POI section.
*
* @param firstChar
* @param delimiter This will replace all digit 11 characters. Usually a
* '-' to separate numbers in a telephone. No doubt there is a different
* standard in each country.
* @return A phone number possibly containing the delimiter character.
*/
@
Override
public String getBase11str
(byte firstChar,
char delimiter
) {
throw new UnsupportedOperationException();
}
/**
* Closes this stream and releases any system resources associated
* with it. If the stream is already closed then invoking this
* method has no effect.
*
* @throws IOException if an I/O error occurs
*/
@
Override
public void close
() throws IOException {
// Nothing to do.
}
}