poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From n...@apache.org
Subject svn commit: r1590556 - in /poi/trunk/src: java/org/apache/poi/poifs/filesystem/ java/org/apache/poi/poifs/nio/ testcases/org/apache/poi/hpsf/basic/
Date Mon, 28 Apr 2014 06:35:39 GMT
Author: nick
Date: Mon Apr 28 06:35:39 2014
New Revision: 1590556

URL: http://svn.apache.org/r1590556
Log:
Get NPOIFS in-place-write working!

Modified:
    poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java
    poi/trunk/src/java/org/apache/poi/poifs/nio/FileBackedDataSource.java
    poi/trunk/src/testcases/org/apache/poi/hpsf/basic/AllPOIHPSFBasicTests.java
    poi/trunk/src/testcases/org/apache/poi/hpsf/basic/TestWrite.java

Modified: poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java?rev=1590556&r1=1590555&r2=1590556&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java (original)
+++ poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java Mon Apr 28 06:35:39
2014
@@ -164,6 +164,7 @@ public class NPOIFSFileSystem extends Bl
     {
        this(
            (new RandomAccessFile(file, readOnly? "r" : "rw")).getChannel(),
+           readOnly,
            true
        );
     }
@@ -176,17 +177,17 @@ public class NPOIFSFileSystem extends Bl
      *  when you're done to have the underlying Channel closed, as the channel is
      *  kept open during normal operation to read the data out.</p> 
      *  
-     * @param channel the FileChannel from which to read the data
+     * @param channel the FileChannel from which to read and write the data
      *
      * @exception IOException on errors reading, or on invalid data
      */
     public NPOIFSFileSystem(FileChannel channel)
          throws IOException
     {
-       this(channel, false);
+       this(channel, false, false);
     }
     
-    private NPOIFSFileSystem(FileChannel channel, boolean closeChannelOnError)
+    private NPOIFSFileSystem(FileChannel channel, boolean readOnly, boolean closeChannelOnError)
          throws IOException
     {
        this(false);
@@ -200,7 +201,7 @@ public class NPOIFSFileSystem extends Bl
           _header = new HeaderBlock(headerBuffer);
           
           // Now process the various entries
-          _data = new FileBackedDataSource(channel);
+          _data = new FileBackedDataSource(channel, readOnly);
           readCoreContents();
        } catch(IOException e) {
           if(closeChannelOnError) {

Modified: poi/trunk/src/java/org/apache/poi/poifs/nio/FileBackedDataSource.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/poifs/nio/FileBackedDataSource.java?rev=1590556&r1=1590555&r2=1590556&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/poifs/nio/FileBackedDataSource.java (original)
+++ poi/trunk/src/java/org/apache/poi/poifs/nio/FileBackedDataSource.java Mon Apr 28 06:35:39
2014
@@ -31,13 +31,10 @@ import org.apache.poi.util.IOUtils;
 
 /**
  * A POIFS {@link DataSource} backed by a File
- * 
- * TODO - Return the ByteBuffers in such a way that in RW mode,
- *  changes to the buffer end up on the disk (will fix the HPSF TestWrite
- *  currently failing unit test when done)
  */
 public class FileBackedDataSource extends DataSource {
    private FileChannel channel;
+   private boolean writable;
 
    @SuppressWarnings("resource")
    public FileBackedDataSource(File file) throws FileNotFoundException {
@@ -45,10 +42,12 @@ public class FileBackedDataSource extend
          throw new FileNotFoundException(file.toString());
       }
       this.channel = (new RandomAccessFile(file, "r")).getChannel();
+      this.writable = false;
    }
 
-   public FileBackedDataSource(FileChannel channel) {
+   public FileBackedDataSource(FileChannel channel, boolean readOnly) {
       this.channel = channel;
+      this.writable = !readOnly;
    }
 
    @Override
@@ -56,11 +55,19 @@ public class FileBackedDataSource extend
       if(position >= size()) {
          throw new IllegalArgumentException("Position " + position + " past the end of the
file");
       }
-
-      // Read
-      channel.position(position);
-      ByteBuffer dst = ByteBuffer.allocate(length);
-      int worked = IOUtils.readFully(channel, dst);
+      
+      // Do we read or map (for read/write?
+      ByteBuffer dst;
+      int worked = -1;
+      if (writable) {
+          dst = channel.map(FileChannel.MapMode.READ_WRITE, position, length);
+          worked = 0;
+      } else {
+          // Read
+          channel.position(position);
+          dst = ByteBuffer.allocate(length);
+          worked = IOUtils.readFully(channel, dst);
+      }
 
       // Check
       if(worked == -1) {

Modified: poi/trunk/src/testcases/org/apache/poi/hpsf/basic/AllPOIHPSFBasicTests.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hpsf/basic/AllPOIHPSFBasicTests.java?rev=1590556&r1=1590555&r2=1590556&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hpsf/basic/AllPOIHPSFBasicTests.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/hpsf/basic/AllPOIHPSFBasicTests.java Mon Apr 28
06:35:39 2014
@@ -21,8 +21,6 @@ import org.junit.runner.RunWith;
 import org.junit.runners.Suite;
 /**
  * Test suite for org.apache.poi.hpsf.basic
- * 
- * @author Josh Micich
  */
 @RunWith(Suite.class)
 @Suite.SuiteClasses({

Modified: poi/trunk/src/testcases/org/apache/poi/hpsf/basic/TestWrite.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hpsf/basic/TestWrite.java?rev=1590556&r1=1590555&r2=1590556&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hpsf/basic/TestWrite.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/hpsf/basic/TestWrite.java Mon Apr 28 06:35:39 2014
@@ -75,7 +75,6 @@ import org.apache.poi.util.IOUtils;
 import org.apache.poi.util.LittleEndian;
 import org.apache.poi.util.TempFile;
 import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
 
 /**
@@ -811,7 +810,6 @@ public class TestWrite
      *  without needing to stream in + out the whole kitchen sink
      */
     @Test
-    @Ignore
     public void inPlaceNPOIFSWrite() throws Exception {
         NPOIFSFileSystem fs = null;
         DirectoryEntry root = null;



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


Mime
View raw message