From caeb586e1db8ce03d928ea99b72964baba17fe06 Mon Sep 17 00:00:00 2001 From: Sergey Vladimirov Date: Fri, 21 Oct 2011 21:19:39 +0000 Subject: [PATCH] reuse code from LittleEndian class git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1187549 13f79535-47bb-0310-9956-ffa450edef68 --- src/java/org/apache/poi/hpsf/TypeWriter.java | 83 +++++++------------- 1 file changed, 30 insertions(+), 53 deletions(-) diff --git a/src/java/org/apache/poi/hpsf/TypeWriter.java b/src/java/org/apache/poi/hpsf/TypeWriter.java index 84269ab333..b1e501cabc 100644 --- a/src/java/org/apache/poi/hpsf/TypeWriter.java +++ b/src/java/org/apache/poi/hpsf/TypeWriter.java @@ -39,18 +39,13 @@ public class TypeWriter * @return The number of bytes that have been written. * @exception IOException if an I/O error occurs */ - public static int writeToStream(final OutputStream out, final short n) - throws IOException + public static int writeToStream( final OutputStream out, final short n ) + throws IOException { - final int length = LittleEndian.SHORT_SIZE; - byte[] buffer = new byte[length]; - LittleEndian.putShort(buffer, 0, n); // FIXME: unsigned - out.write(buffer, 0, length); - return length; + LittleEndian.putShort( out, n ); // FIXME: unsigned + return LittleEndian.SHORT_SIZE; } - - /** *

Writes a four-byte value to an output stream.

* @@ -59,40 +54,28 @@ public class TypeWriter * @exception IOException if an I/O error occurs * @return The number of bytes written to the output stream. */ - public static int writeToStream(final OutputStream out, final int n) - throws IOException + public static int writeToStream( final OutputStream out, final int n ) + throws IOException { - final int l = LittleEndian.INT_SIZE; - final byte[] buffer = new byte[l]; - LittleEndian.putInt(buffer, 0, n); - out.write(buffer, 0, l); - return l; - + LittleEndian.putInt( n, out ); + return LittleEndian.INT_SIZE; } - - /** *

Writes a eight-byte value to an output stream.

* * @param out The stream to write to. * @param n The value to write. * @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) - throws IOException + public static int writeToStream( final OutputStream out, final long n ) + throws IOException { - final int l = LittleEndian.LONG_SIZE; - final byte[] buffer = new byte[l]; - LittleEndian.putLong(buffer, 0, n); - out.write(buffer, 0, l); - return l; - + LittleEndian.putLong( n, out ); + return LittleEndian.LONG_SIZE; } - - /** *

Writes an unsigned two-byte value to an output stream.

* @@ -100,18 +83,16 @@ public class TypeWriter * @param n The value to write * @exception IOException if an I/O error occurs */ - public static void writeUShortToStream(final OutputStream out, final int n) - throws IOException + public static void writeUShortToStream( final OutputStream out, final int n ) + throws IOException { int high = n & 0xFFFF0000; - if (high != 0) - throw new IllegalPropertySetDataException - ("Value " + n + " cannot be represented by 2 bytes."); - writeToStream(out, (short) n); + if ( high != 0 ) + throw new IllegalPropertySetDataException( "Value " + n + + " cannot be represented by 2 bytes." ); + LittleEndian.putUShort( n, out ); } - - /** *

Writes an unsigned four-byte value to an output stream.

* @@ -120,18 +101,17 @@ public class TypeWriter * @return The number of bytes that have been written to the output stream. * @exception IOException if an I/O error occurs */ - public static int writeUIntToStream(final OutputStream out, final long n) - throws IOException + public static int writeUIntToStream( final OutputStream out, final long n ) + throws IOException { long high = n & 0xFFFFFFFF00000000L; - if (high != 0 && high != 0xFFFFFFFF00000000L) - throw new IllegalPropertySetDataException - ("Value " + n + " cannot be represented by 4 bytes."); - return writeToStream(out, (int) n); + if ( high != 0 && high != 0xFFFFFFFF00000000L ) + throw new IllegalPropertySetDataException( "Value " + n + + " cannot be represented by 4 bytes." ); + LittleEndian.putUInt( n, out ); + return LittleEndian.INT_SIZE; } - - /** *

Writes a 16-byte {@link ClassID} to an output stream.

* @@ -200,14 +180,11 @@ public class TypeWriter * @exception IOException if an I/O error occurs * @return The number of bytes written to the output stream. */ - public static int writeToStream(final OutputStream out, final double n) - throws IOException + public static int writeToStream( final OutputStream out, final double n ) + throws IOException { - final int l = LittleEndian.DOUBLE_SIZE; - final byte[] buffer = new byte[l]; - LittleEndian.putDouble(buffer, 0, n); - out.write(buffer, 0, l); - return l; + LittleEndian.putDouble( n, out ); + return LittleEndian.DOUBLE_SIZE; } } -- 2.39.5