reuse code from LittleEndian class

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1187549 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sergey Vladimirov 2011-10-21 21:19:39 +00:00
parent 4ef78d40fc
commit a35cf979d2

View File

@ -39,18 +39,13 @@ public class TypeWriter
* @return The number of bytes that have been written. * @return The number of bytes that have been written.
* @exception IOException if an I/O error occurs * @exception IOException if an I/O error occurs
*/ */
public static int writeToStream(final OutputStream out, final short n) public static int writeToStream( final OutputStream out, final short n )
throws IOException throws IOException
{ {
final int length = LittleEndian.SHORT_SIZE; LittleEndian.putShort( out, n ); // FIXME: unsigned
byte[] buffer = new byte[length]; return LittleEndian.SHORT_SIZE;
LittleEndian.putShort(buffer, 0, n); // FIXME: unsigned
out.write(buffer, 0, length);
return length;
} }
/** /**
* <p>Writes a four-byte value to an output stream.</p> * <p>Writes a four-byte value to an output stream.</p>
* *
@ -59,40 +54,28 @@ public class TypeWriter
* @exception IOException if an I/O error occurs * @exception IOException if an I/O error occurs
* @return The number of bytes written to the output stream. * @return The number of bytes written to the output stream.
*/ */
public static int writeToStream(final OutputStream out, final int n) public static int writeToStream( final OutputStream out, final int n )
throws IOException throws IOException
{ {
final int l = LittleEndian.INT_SIZE; LittleEndian.putInt( n, out );
final byte[] buffer = new byte[l]; return LittleEndian.INT_SIZE;
LittleEndian.putInt(buffer, 0, n);
out.write(buffer, 0, l);
return l;
} }
/** /**
* <p>Writes a eight-byte value to an output stream.</p> * <p>Writes a eight-byte value to an output stream.</p>
* *
* @param out The stream to write to. * @param out The stream to write to.
* @param n The value to write. * @param n The value to write.
* @exception IOException if an I/O error occurs * @exception IOException if an I/O error occurs
* @return The number of bytes written to the output stream. * @return The number of bytes written to the output stream.
*/ */
public static int writeToStream(final OutputStream out, final long n) public static int writeToStream( final OutputStream out, final long n )
throws IOException throws IOException
{ {
final int l = LittleEndian.LONG_SIZE; LittleEndian.putLong( n, out );
final byte[] buffer = new byte[l]; return LittleEndian.LONG_SIZE;
LittleEndian.putLong(buffer, 0, n);
out.write(buffer, 0, l);
return l;
} }
/** /**
* <p>Writes an unsigned two-byte value to an output stream.</p> * <p>Writes an unsigned two-byte value to an output stream.</p>
* *
@ -100,18 +83,16 @@ public class TypeWriter
* @param n The value to write * @param n The value to write
* @exception IOException if an I/O error occurs * @exception IOException if an I/O error occurs
*/ */
public static void writeUShortToStream(final OutputStream out, final int n) public static void writeUShortToStream( final OutputStream out, final int n )
throws IOException throws IOException
{ {
int high = n & 0xFFFF0000; int high = n & 0xFFFF0000;
if (high != 0) if ( high != 0 )
throw new IllegalPropertySetDataException throw new IllegalPropertySetDataException( "Value " + n
("Value " + n + " cannot be represented by 2 bytes."); + " cannot be represented by 2 bytes." );
writeToStream(out, (short) n); LittleEndian.putUShort( n, out );
} }
/** /**
* <p>Writes an unsigned four-byte value to an output stream.</p> * <p>Writes an unsigned four-byte value to an output stream.</p>
* *
@ -120,18 +101,17 @@ public class TypeWriter
* @return The number of bytes that have been written to the output stream. * @return The number of bytes that have been written to the output stream.
* @exception IOException if an I/O error occurs * @exception IOException if an I/O error occurs
*/ */
public static int writeUIntToStream(final OutputStream out, final long n) public static int writeUIntToStream( final OutputStream out, final long n )
throws IOException throws IOException
{ {
long high = n & 0xFFFFFFFF00000000L; long high = n & 0xFFFFFFFF00000000L;
if (high != 0 && high != 0xFFFFFFFF00000000L) if ( high != 0 && high != 0xFFFFFFFF00000000L )
throw new IllegalPropertySetDataException throw new IllegalPropertySetDataException( "Value " + n
("Value " + n + " cannot be represented by 4 bytes."); + " cannot be represented by 4 bytes." );
return writeToStream(out, (int) n); LittleEndian.putUInt( n, out );
return LittleEndian.INT_SIZE;
} }
/** /**
* <p>Writes a 16-byte {@link ClassID} to an output stream.</p> * <p>Writes a 16-byte {@link ClassID} to an output stream.</p>
* *
@ -200,14 +180,11 @@ public class TypeWriter
* @exception IOException if an I/O error occurs * @exception IOException if an I/O error occurs
* @return The number of bytes written to the output stream. * @return The number of bytes written to the output stream.
*/ */
public static int writeToStream(final OutputStream out, final double n) public static int writeToStream( final OutputStream out, final double n )
throws IOException throws IOException
{ {
final int l = LittleEndian.DOUBLE_SIZE; LittleEndian.putDouble( n, out );
final byte[] buffer = new byte[l]; return LittleEndian.DOUBLE_SIZE;
LittleEndian.putDouble(buffer, 0, n);
out.write(buffer, 0, l);
return l;
} }
} }