1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.io.input;
  17. import java.io.DataInput;
  18. import java.io.EOFException;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import org.apache.commons.io.EndianUtils;
  22. /**
  23. * DataInput for systems relying on little endian data formats.
  24. * When read, values will be changed from little endian to big
  25. * endian formats for internal usage.
  26. *
  27. * <p><b>Origin of code: </b>Avalon Excalibur (IO)</p>
  28. *
  29. * @author <a href="mailto:peter@apache.org">Peter Donald</a>
  30. * @version CVS $Revision: 1.8 $ $Date: 2004/02/23 04:56:59 $
  31. */
  32. public class SwappedDataInputStream extends ProxyInputStream
  33. implements DataInput
  34. {
  35. /**
  36. * Constructs a SwappedDataInputStream.
  37. *
  38. * @param input InputStream to read from
  39. */
  40. public SwappedDataInputStream( InputStream input )
  41. {
  42. super( input );
  43. }
  44. /** @see java.io.DataInput#readBoolean() */
  45. public boolean readBoolean()
  46. throws IOException, EOFException
  47. {
  48. return ( 0 == readByte() );
  49. }
  50. /** @see java.io.DataInput#readByte() */
  51. public byte readByte()
  52. throws IOException, EOFException
  53. {
  54. return (byte)in.read();
  55. }
  56. /** @see java.io.DataInput#readChar() */
  57. public char readChar()
  58. throws IOException, EOFException
  59. {
  60. return (char)readShort();
  61. }
  62. /** @see java.io.DataInput#readDouble() */
  63. public double readDouble()
  64. throws IOException, EOFException
  65. {
  66. return EndianUtils.readSwappedDouble( in );
  67. }
  68. /** @see java.io.DataInput#readFloat() */
  69. public float readFloat()
  70. throws IOException, EOFException
  71. {
  72. return EndianUtils.readSwappedFloat( in );
  73. }
  74. /** @see java.io.DataInput#readFully(byte[]) */
  75. public void readFully( byte[] data )
  76. throws IOException, EOFException
  77. {
  78. readFully( data, 0, data.length );
  79. }
  80. /** @see java.io.DataInput#readFully(byte[], int, int) */
  81. public void readFully( byte[] data, int offset, int length )
  82. throws IOException, EOFException
  83. {
  84. int remaining = length;
  85. while( remaining > 0 )
  86. {
  87. int location = offset + ( length - remaining );
  88. int count = read( data, location, remaining );
  89. if( -1 == count )
  90. {
  91. throw new EOFException();
  92. }
  93. remaining -= count;
  94. }
  95. }
  96. /** @see java.io.DataInput#readInt() */
  97. public int readInt()
  98. throws IOException, EOFException
  99. {
  100. return EndianUtils.readSwappedInteger( in );
  101. }
  102. /**
  103. * Not currently supported.
  104. *
  105. * @see java.io.DataInput#readLine()
  106. */
  107. public String readLine()
  108. throws IOException, EOFException
  109. {
  110. throw new UnsupportedOperationException(
  111. "Operation not supported: readLine()" );
  112. }
  113. /** @see java.io.DataInput#readLong() */
  114. public long readLong()
  115. throws IOException, EOFException
  116. {
  117. return EndianUtils.readSwappedLong( in );
  118. }
  119. /** @see java.io.DataInput#readShort() */
  120. public short readShort()
  121. throws IOException, EOFException
  122. {
  123. return EndianUtils.readSwappedShort( in );
  124. }
  125. /** @see java.io.DataInput#readUnsignedByte() */
  126. public int readUnsignedByte()
  127. throws IOException, EOFException
  128. {
  129. return in.read();
  130. }
  131. /** @see java.io.DataInput#readUnsignedShort() */
  132. public int readUnsignedShort()
  133. throws IOException, EOFException
  134. {
  135. return EndianUtils.readSwappedUnsignedShort( in );
  136. }
  137. /**
  138. * Not currently supported.
  139. *
  140. * @see java.io.DataInput#readUTF()
  141. */
  142. public String readUTF()
  143. throws IOException, EOFException
  144. {
  145. throw new UnsupportedOperationException(
  146. "Operation not supported: readUTF()" );
  147. }
  148. /** @see java.io.DataInput#skipBytes(int) */
  149. public int skipBytes( int count )
  150. throws IOException, EOFException
  151. {
  152. return (int)in.skip( count );
  153. }
  154. }