001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.io.input;
018:
019: import java.io.DataInput;
020: import java.io.EOFException;
021: import java.io.IOException;
022: import java.io.InputStream;
023:
024: import org.apache.commons.io.EndianUtils;
025:
026: /**
027: * DataInput for systems relying on little endian data formats.
028: * When read, values will be changed from little endian to big
029: * endian formats for internal usage.
030: * <p>
031: * <b>Origin of code: </b>Avalon Excalibur (IO)
032: *
033: * @author <a href="mailto:peter@apache.org">Peter Donald</a>
034: * @version CVS $Revision: 437567 $ $Date: 2006-08-28 08:39:07 +0200 (Mo, 28 Aug 2006) $
035: */
036: public class SwappedDataInputStream extends ProxyInputStream implements
037: DataInput {
038:
039: /**
040: * Constructs a SwappedDataInputStream.
041: *
042: * @param input InputStream to read from
043: */
044: public SwappedDataInputStream(InputStream input) {
045: super (input);
046: }
047:
048: /** @see java.io.DataInput#readBoolean() */
049: public boolean readBoolean() throws IOException, EOFException {
050: return (0 == readByte());
051: }
052:
053: /** @see java.io.DataInput#readByte() */
054: public byte readByte() throws IOException, EOFException {
055: return (byte) in.read();
056: }
057:
058: /** @see java.io.DataInput#readChar() */
059: public char readChar() throws IOException, EOFException {
060: return (char) readShort();
061: }
062:
063: /** @see java.io.DataInput#readDouble() */
064: public double readDouble() throws IOException, EOFException {
065: return EndianUtils.readSwappedDouble(in);
066: }
067:
068: /** @see java.io.DataInput#readFloat() */
069: public float readFloat() throws IOException, EOFException {
070: return EndianUtils.readSwappedFloat(in);
071: }
072:
073: /** @see java.io.DataInput#readFully(byte[]) */
074: public void readFully(byte[] data) throws IOException, EOFException {
075: readFully(data, 0, data.length);
076: }
077:
078: /** @see java.io.DataInput#readFully(byte[], int, int) */
079: public void readFully(byte[] data, int offset, int length)
080: throws IOException, EOFException {
081: int remaining = length;
082:
083: while (remaining > 0) {
084: int location = offset + (length - remaining);
085: int count = read(data, location, remaining);
086:
087: if (-1 == count) {
088: throw new EOFException();
089: }
090:
091: remaining -= count;
092: }
093: }
094:
095: /** @see java.io.DataInput#readInt() */
096: public int readInt() throws IOException, EOFException {
097: return EndianUtils.readSwappedInteger(in);
098: }
099:
100: /**
101: * Not currently supported.
102: *
103: * @see java.io.DataInput#readLine()
104: */
105: public String readLine() throws IOException, EOFException {
106: throw new UnsupportedOperationException(
107: "Operation not supported: readLine()");
108: }
109:
110: /** @see java.io.DataInput#readLong() */
111: public long readLong() throws IOException, EOFException {
112: return EndianUtils.readSwappedLong(in);
113: }
114:
115: /** @see java.io.DataInput#readShort() */
116: public short readShort() throws IOException, EOFException {
117: return EndianUtils.readSwappedShort(in);
118: }
119:
120: /** @see java.io.DataInput#readUnsignedByte() */
121: public int readUnsignedByte() throws IOException, EOFException {
122: return in.read();
123: }
124:
125: /** @see java.io.DataInput#readUnsignedShort() */
126: public int readUnsignedShort() throws IOException, EOFException {
127: return EndianUtils.readSwappedUnsignedShort(in);
128: }
129:
130: /**
131: * Not currently supported.
132: *
133: * @see java.io.DataInput#readUTF()
134: */
135: public String readUTF() throws IOException, EOFException {
136: throw new UnsupportedOperationException(
137: "Operation not supported: readUTF()");
138: }
139:
140: /** @see java.io.DataInput#skipBytes(int) */
141: public int skipBytes(int count) throws IOException, EOFException {
142: return (int) in.skip(count);
143: }
144:
145: }
|