下面列出了怎么用org.apache.commons.io.EndianUtils的API类实例代码及写法,或者点击链接到github查看源代码。
private void readObjects(InputStream in) throws IOException {
final int MAX_X = width * DT1.Tile.SUBTILE_SIZE;
final int MAX_Y = height * DT1.Tile.SUBTILE_SIZE;
numObjects = version < 2 ? 0 : EndianUtils.readSwappedInteger(in);
objects = numObjects == 0 ? Object.EMPTY_OBJECT_ARRAY : new Object[numObjects];
for (int i = 0; i < numObjects; i++) {
try {
Object object = objects[i] = new Object().read(version, in);
if (DEBUG_OBJECTS) Gdx.app.debug(TAG, object.toString());
if (object.x < 0 || MAX_X <= object.x
|| object.y < 0 || MAX_Y <= object.y) {
Gdx.app.error(TAG, "Object out of DS1 bounds: " + object);
}
} catch (Throwable t) {
// Don't care, invalid object, skip it. Log it for posterity.
Gdx.app.error(TAG, t.getMessage(), t);
}
}
}
/**
* Queries the device for available sample rates. Will always provide at
* least the default 10 MHz sample rate.
*/
private void determineAvailableSampleRates()
throws LibUsbException, DeviceException
{
mSampleRates.clear();
mSampleRates.add( DEFAULT_SAMPLE_RATE );
//Get a count of available sample rates. If we get an exception, then
//we're using an older firmware revision and only the default 10 MHz
//rate is supported
try
{
byte[] rawCount = readArray( Command.GET_SAMPLE_RATES, 0, 0, 4 );
if( rawCount != null )
{
int count = EndianUtils.readSwappedInteger( rawCount, 0 );
byte[] rawRates = readArray( Command.GET_SAMPLE_RATES, 0,
count, ( count * 4 ) );
for( int x = 0; x < count; x++ )
{
int rate = EndianUtils.readSwappedInteger( rawRates, ( x * 4 ) );
if( rate != DEFAULT_SAMPLE_RATE.getRate() )
{
mSampleRates.add( new AirspySampleRate( x, rate,
formatSampleRate( rate ) ) );
}
}
}
}
catch( LibUsbException e )
{
//Press on, nothing else to do here ..
}
}
private int readTags(InputStream in, int offset) throws IOException {
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
tags[offset] = EndianUtils.readSwappedInteger(in);
offset += numTags;
}
}
return offset;
}
private void readGroups(InputStream in) throws IOException {
if (version >= 12 && (tagType == 1 || tagType == 2)) {
if (version >= 18) IOUtils.skip(in, Ints.BYTES);
numGroups = EndianUtils.readSwappedInteger(in);
groups = numGroups == 0 ? Group.EMPTY_GROUP_ARRAY : new Group[numGroups];
for (int i = 0; i < numGroups; i++) {
groups[i] = new Group().read(version, in);
if (DEBUG_GROUPS) Gdx.app.debug(TAG, groups[i].toString());
}
} else {
numGroups = 0;
groups = Group.EMPTY_GROUP_ARRAY;
}
}
private void readPaths(InputStream in) throws IOException {
if (version >= 14 && in.available() >= Ints.BYTES) {
numPaths = EndianUtils.readSwappedInteger(in);
paths = numPaths == 0 ? Path.EMPTY_PATH_ARRAY : new Path[numPaths];
for (int i = 0; i < numPaths; i++) {
Path path = paths[i] = new Path().read(version, objects, in);
if (DEBUG_PATHS) Gdx.app.debug(TAG, path.toString());
}
} else {
numPaths = 0;
paths = Path.EMPTY_PATH_ARRAY;
}
}
Cell read(InputStream in, int orient) throws IOException {
value = EndianUtils.readSwappedInteger(in);
mainIndex = (short) ((value >>> MAIN_INDEX_OFFSET) & MAIN_INDEX_BITS);
subIndex = (short) ((value >>> SUB_INDEX_OFFSET) & SUB_INDEX_BITS);
orientation = (short) orient;
return updateIndex();
}
Object read(int version, InputStream in) throws IOException {
type = EndianUtils.readSwappedInteger(in);
id = EndianUtils.readSwappedInteger(in);
x = EndianUtils.readSwappedInteger(in);
y = EndianUtils.readSwappedInteger(in);
flags = version < 6 ? 0 : EndianUtils.readSwappedInteger(in);
path = null;
return this;
}
Group read(int version, InputStream in) throws IOException {
if (in.available() >= Ints.BYTES) x = EndianUtils.readSwappedInteger(in);
if (in.available() >= Ints.BYTES) y = EndianUtils.readSwappedInteger(in);
if (in.available() >= Ints.BYTES) width = EndianUtils.readSwappedInteger(in);
if (in.available() >= Ints.BYTES) height = EndianUtils.readSwappedInteger(in);
if (version >= 13) {
if (in.available() >= Ints.BYTES) unk = EndianUtils.readSwappedInteger(in);
}
return this;
}
Path read(int version, Object[] objects, InputStream in) throws IOException {
numPoints = EndianUtils.readSwappedInteger(in);
points = new Point[numPoints];
x = EndianUtils.readSwappedInteger(in);
y = EndianUtils.readSwappedInteger(in);
Object object = null;
for (int i = 0; i < objects.length; i++) {
Object tmp = objects[i];
if (tmp == null) continue;
if (tmp.x != x || tmp.y != y) continue;
if (object != null) Gdx.app.error(TAG, "More than one object is located at path position: " + this);
object = tmp;
}
if (object == null) {
Gdx.app.error(TAG, "No object associated with path: " + this);
int skip = (version >= 15 ? 3 : 2) * Ints.BYTES;
for (int p = 0; p < numPoints; p++) {
in.skip(skip);
}
return this;
}
for (int p = 0; p < numPoints; p++) points[p] = new Point().read(version, in);
object.path = this;
return this;
}
Block(InputStream in) throws IOException {
numEntries = EndianUtils.readSwappedInteger(in);
entries = new Entry[numEntries];
for (int i = 0; i < numEntries; i++) {
entries[i] = new Entry(in);
if (DEBUG_ENTRIES) Gdx.app.debug(TAG, entries[i].toString());
}
}
private VorbisCommentHeader readCommentHeader(InputStream input)
throws IOException, VorbisCommentReaderException {
try {
long vendorLength = EndianUtils.readSwappedUnsignedInteger(input);
String vendorName = readUTF8String(input, vendorLength);
long userCommentLength = EndianUtils
.readSwappedUnsignedInteger(input);
return new VorbisCommentHeader(vendorName, userCommentLength);
} catch (UnsupportedEncodingException e) {
throw new VorbisCommentReaderException(e);
}
}
public long readUnsignedInt() throws IOException {
return EndianUtils.readSwappedUnsignedInteger(this);
}
public short readShort() throws IOException {
return EndianUtils.readSwappedShort(this);
}
public int readUnsignedShort() throws IOException {
return EndianUtils.readSwappedUnsignedShort(this);
}
public void writeUnsignedShort(int num) throws IOException {
EndianUtils.writeSwappedShort(stream, (short) num);
}
public void writeUnsignedInt(int num) throws IOException {
EndianUtils.writeSwappedInteger(stream, num);
}
public void writeShort(int v) throws IOException {
EndianUtils.writeSwappedShort(stream, (short) v);
}
public void writeInt(int v) throws IOException {
EndianUtils.writeSwappedInteger(stream, v);
}
public void writeLong(long v) throws IOException {
EndianUtils.writeSwappedLong(stream, v);
}
public void writeFloat(float v) throws IOException {
EndianUtils.writeSwappedFloat(stream, v);
}
Point read(int version, InputStream in) throws IOException {
x = EndianUtils.readSwappedInteger(in);
y = EndianUtils.readSwappedInteger(in);
action = version < 15 ? 1 : EndianUtils.readSwappedInteger(in);
return this;
}
/**
* Delegates to {@link EndianUtils#readSwappedDouble(InputStream)}.
* @return the read long
* @throws IOException if an I/O error occurs
* @throws EOFException if an end of file is reached unexpectedly
*/
public double readDouble()
throws IOException, EOFException
{
return EndianUtils.readSwappedDouble( in );
}
/**
* Delegates to {@link EndianUtils#readSwappedFloat(InputStream)}.
* @return the read long
* @throws IOException if an I/O error occurs
* @throws EOFException if an end of file is reached unexpectedly
*/
public float readFloat()
throws IOException, EOFException
{
return EndianUtils.readSwappedFloat( in );
}
/**
* Delegates to {@link EndianUtils#readSwappedInteger(InputStream)}.
* @return the read long
* @throws EOFException if an end of file is reached unexpectedly
* @throws IOException if an I/O error occurs
*/
public int readInt()
throws IOException, EOFException
{
return EndianUtils.readSwappedInteger( in );
}
/**
* Delegates to {@link EndianUtils#readSwappedLong(InputStream)}.
* @return the read long
* @throws EOFException if an end of file is reached unexpectedly
* @throws IOException if an I/O error occurs
*/
public long readLong()
throws IOException, EOFException
{
return EndianUtils.readSwappedLong( in );
}
/**
* Delegates to {@link EndianUtils#readSwappedShort(InputStream)}.
* @return the read long
* @throws EOFException if an end of file is reached unexpectedly
* @throws IOException if an I/O error occurs
*/
public short readShort()
throws IOException, EOFException
{
return EndianUtils.readSwappedShort( in );
}
/**
* Delegates to {@link EndianUtils#readSwappedUnsignedShort(InputStream)}.
* @return the read long
* @throws EOFException if an end of file is reached unexpectedly
* @throws IOException if an I/O error occurs
*/
public int readUnsignedShort()
throws IOException, EOFException
{
return EndianUtils.readSwappedUnsignedShort( in );
}
/**
* Delegates to {@link EndianUtils#readSwappedDouble(InputStream)}.
* @return the read long
* @throws IOException if an I/O error occurs
* @throws EOFException if an end of file is reached unexpectedly
*/
public double readDouble()
throws IOException, EOFException
{
return EndianUtils.readSwappedDouble( in );
}
/**
* Delegates to {@link EndianUtils#readSwappedFloat(InputStream)}.
* @return the read long
* @throws IOException if an I/O error occurs
* @throws EOFException if an end of file is reached unexpectedly
*/
public float readFloat()
throws IOException, EOFException
{
return EndianUtils.readSwappedFloat( in );
}
/**
* Delegates to {@link EndianUtils#readSwappedInteger(InputStream)}.
* @return the read long
* @throws EOFException if an end of file is reached unexpectedly
* @throws IOException if an I/O error occurs
*/
public int readInt()
throws IOException, EOFException
{
return EndianUtils.readSwappedInteger( in );
}