You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by jo...@apache.org on 2009/11/15 01:28:00 UTC

svn commit: r836298 - in /poi/trunk/src: java/org/apache/poi/util/LittleEndianByteArrayInputStream.java testcases/org/apache/poi/util/TestLittleEndianStreams.java

Author: josh
Date: Sun Nov 15 00:27:59 2009
New Revision: 836298

URL: http://svn.apache.org/viewvc?rev=836298&view=rev
Log:
fixed bug in LittleEndianByteArrayInputStream.readFully()

Modified:
    poi/trunk/src/java/org/apache/poi/util/LittleEndianByteArrayInputStream.java
    poi/trunk/src/testcases/org/apache/poi/util/TestLittleEndianStreams.java

Modified: poi/trunk/src/java/org/apache/poi/util/LittleEndianByteArrayInputStream.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/util/LittleEndianByteArrayInputStream.java?rev=836298&r1=836297&r2=836298&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/util/LittleEndianByteArrayInputStream.java (original)
+++ poi/trunk/src/java/org/apache/poi/util/LittleEndianByteArrayInputStream.java Sun Nov 15 00:27:59 2009
@@ -18,9 +18,8 @@
 package org.apache.poi.util;
 
 /**
- * Adapts a plain byte array to {@link LittleEndianInput} 
- * 
- * 
+ * Adapts a plain byte array to {@link LittleEndianInput}
+ *
  * @author Josh Micich
  */
 public final class LittleEndianByteArrayInputStream implements LittleEndianInput {
@@ -60,7 +59,7 @@
 	public int readInt() {
 		checkPosition(4);
 		int i = _readIndex;
-		
+
 		int b0 = _buf[i++] & 0xFF;
 		int b1 = _buf[i++] & 0xFF;
 		int b2 = _buf[i++] & 0xFF;
@@ -71,7 +70,7 @@
 	public long readLong() {
 		checkPosition(8);
 		int i = _readIndex;
-		
+
 		int b0 = _buf[i++] & 0xFF;
 		int b1 = _buf[i++] & 0xFF;
 		int b2 = _buf[i++] & 0xFF;
@@ -100,7 +99,7 @@
 	public int readUShort() {
 		checkPosition(2);
 		int i = _readIndex;
-		
+
 		int b0 = _buf[i++] & 0xFF;
 		int b1 = _buf[i++] & 0xFF;
 		_readIndex = i;
@@ -108,7 +107,7 @@
 	}
 	public void readFully(byte[] buf, int off, int len) {
 		checkPosition(len);
-		System.arraycopy(_buf, _readIndex, _buf, off, len);
+		System.arraycopy(_buf, _readIndex, buf, off, len);
 		_readIndex+=len;
 	}
 	public void readFully(byte[] buf) {

Modified: poi/trunk/src/testcases/org/apache/poi/util/TestLittleEndianStreams.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/util/TestLittleEndianStreams.java?rev=836298&r1=836297&r2=836298&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/util/TestLittleEndianStreams.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/util/TestLittleEndianStreams.java Sun Nov 15 00:27:59 2009
@@ -19,7 +19,9 @@
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
+import java.util.Arrays;
 
+import junit.framework.AssertionFailedError;
 import junit.framework.TestCase;
 
 /**
@@ -50,4 +52,29 @@
 		assertEquals(1234567890123456789L, lei.readLong());
 		assertEquals(123.456, lei.readDouble(), 0.0);
 	}
+
+	/**
+	 * Up until svn r836101 {@link LittleEndianByteArrayInputStream#readFully(byte[], int, int)}
+	 * had an error which resulted in the data being read and written back to the source byte
+	 * array.
+	 */
+	public void testReadFully() {
+		byte[] srcBuf = HexRead.readFromString("99 88 77 66 55 44 33");
+		LittleEndianInput lei = new LittleEndianByteArrayInputStream(srcBuf);
+
+		// do initial read to increment the read index beyond zero
+		assertEquals(0x8899, lei.readUShort());
+
+		byte[] actBuf = new byte[4];
+		lei.readFully(actBuf);
+
+		if (actBuf[0] == 0x00 && srcBuf[0] == 0x77 && srcBuf[3] == 0x44) {
+			throw new AssertionFailedError("Identified bug in readFully() - source buffer was modified");
+		}
+
+		byte[] expBuf = HexRead.readFromString("77 66 55 44");
+		assertTrue(Arrays.equals(actBuf, expBuf));
+		assertEquals(0x33, lei.readUByte());
+		assertEquals(0, lei.available());
+	}
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org