poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From n...@apache.org
Subject svn commit: r1134945 - in /poi/trunk/src: java/org/apache/poi/poifs/filesystem/NDocumentInputStream.java testcases/org/apache/poi/poifs/filesystem/TestDocumentInputStream.java
Date Sun, 12 Jun 2011 16:51:43 GMT
Author: nick
Date: Sun Jun 12 16:51:43 2011
New Revision: 1134945

URL: http://svn.apache.org/viewvc?rev=1134945&view=rev
Log:
Further tests for NPOIFS document input stream reading, and fix a NPOIFS skip issue

Modified:
    poi/trunk/src/java/org/apache/poi/poifs/filesystem/NDocumentInputStream.java
    poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentInputStream.java

Modified: poi/trunk/src/java/org/apache/poi/poifs/filesystem/NDocumentInputStream.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/poifs/filesystem/NDocumentInputStream.java?rev=1134945&r1=1134944&r2=1134945&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/poifs/filesystem/NDocumentInputStream.java (original)
+++ poi/trunk/src/java/org/apache/poi/poifs/filesystem/NDocumentInputStream.java Sun Jun 12
16:51:43 2011
@@ -184,7 +184,10 @@ public final class NDocumentInputStream 
          _current_block_count++;
          
    		// Skip to the right place in it
-   		_buffer.position(_marked_offset - _current_offset);
+         // (It should be positioned already at the start of the block,
+         //  we need to move further inside the block)
+         int skipBy = _marked_offset - _current_offset;
+   		_buffer.position(_buffer.position() + skipBy);
       }
 
       // All done

Modified: poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentInputStream.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentInputStream.java?rev=1134945&r1=1134944&r2=1134945&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentInputStream.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentInputStream.java Sun
Jun 12 16:51:43 2011
@@ -77,6 +77,14 @@ public final class TestDocumentInputStre
         System.arraycopy(_workbook_data, 0, _workbook_data_only, 0, _workbook_size);
         
         NPOIFSFileSystem npoifs = new NPOIFSFileSystem();
+        // Make it easy when debugging to see what isn't the doc
+        byte[] minus1 = new byte[512];
+        Arrays.fill(minus1, (byte)-1);
+        npoifs.getBlockAt(-1).put(minus1);
+        npoifs.getBlockAt(0).put(minus1);
+        npoifs.getBlockAt(1).put(minus1);
+        
+        // Create the NPOIFS document
         _workbook_n = (DocumentNode)npoifs.createDocument(
               new ByteArrayInputStream(_workbook_data_only),
               "Workbook"
@@ -127,13 +135,15 @@ public final class TestDocumentInputStre
      * test mark/reset/markSupported.
      */
     public void testMarkFunctions() throws IOException {
+        byte[] buffer = new byte[ _workbook_size / 5 ];
+        byte[] small_buffer = new byte[212];
+       
         DocumentInputStream[] streams = new DocumentInputStream[] {
               new DocumentInputStream(_workbook_o),
               new NDocumentInputStream(_workbook_n)
         };
         for(DocumentInputStream stream : streams) {
            // Read a fifth of it, and check all's correct
-           byte[] buffer = new byte[ _workbook_size / 5 ];
            stream.read(buffer);
            for (int j = 0; j < buffer.length; j++) {
               assertEquals(
@@ -148,7 +158,89 @@ public final class TestDocumentInputStre
            stream.reset();
            assertEquals(_workbook_size, stream.available());
            
-           // Read all of it again, check it begain at the start again
+           
+           // Read part of a block
+           stream.read(small_buffer);
+           for (int j = 0; j < small_buffer.length; j++) {
+              assertEquals(
+                    "checking byte " + j, 
+                    _workbook_data[ j ], small_buffer[ j ]
+              );
+           }
+           assertEquals(_workbook_size - small_buffer.length, stream.available());
+           stream.mark(0);
+           
+           // Read the next part
+           stream.read(small_buffer);
+           for (int j = 0; j < small_buffer.length; j++) {
+              assertEquals(
+                    "checking byte " + j, 
+                    _workbook_data[ j+small_buffer.length ], small_buffer[ j ]
+              );
+           }
+           assertEquals(_workbook_size - 2*small_buffer.length, stream.available());
+           
+           // Reset, check it goes back to where it was
+           stream.reset();
+           assertEquals(_workbook_size - small_buffer.length, stream.available());
+           
+           // Read 
+           stream.read(small_buffer);
+           for (int j = 0; j < small_buffer.length; j++) {
+              assertEquals(
+                    "checking byte " + j, 
+                    _workbook_data[ j+small_buffer.length ], small_buffer[ j ]
+              );
+           }
+           assertEquals(_workbook_size - 2*small_buffer.length, stream.available());
+           
+           
+           // Now read at various points
+           Arrays.fill(small_buffer, ( byte ) 0);
+           stream.read(small_buffer, 6, 8);
+           stream.read(small_buffer, 100, 10);
+           stream.read(small_buffer, 150, 12);
+           int pos = small_buffer.length * 2;
+           for (int j = 0; j < small_buffer.length; j++) {
+              byte exp = 0;
+              if(j>= 6 && j<6+8) {
+                 exp = _workbook_data[pos];
+                 pos++;
+              }
+              if(j>= 100 && j<100+10) {
+                 exp = _workbook_data[pos];
+                 pos++;
+              }
+              if(j>= 150 && j<150+12) {
+                 exp = _workbook_data[pos];
+                 pos++;
+              }
+              
+              assertEquals("checking byte " + j, exp, small_buffer[j]);
+           }
+        }
+           
+        // Now repeat it with spanning multiple blocks
+        streams = new DocumentInputStream[] {
+              new DocumentInputStream(_workbook_o),
+              new NDocumentInputStream(_workbook_n)
+        };
+        for(DocumentInputStream stream : streams) {
+           // Read several blocks work
+           buffer = new byte[ _workbook_size / 5 ];
+           stream.read(buffer);
+           for (int j = 0; j < buffer.length; j++) {
+              assertEquals(
+                    "checking byte " + j, 
+                    _workbook_data[ j ], buffer[ j ]
+              );
+           }
+           assertEquals(_workbook_size - buffer.length, stream.available());
+           
+           // Read all of it again, check it began at the start again
+           stream.reset();
+           assertEquals(_workbook_size, stream.available());
+           
            stream.read(buffer);
            for (int j = 0; j < buffer.length; j++) {
               assertEquals(



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


Mime
View raw message