mirror of https://github.com/apache/poi.git
Get NPOIFS in-place-write working!
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1590556 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
59a1590048
commit
4e984df1c9
|
@ -164,6 +164,7 @@ public class NPOIFSFileSystem extends BlockStore
|
||||||
{
|
{
|
||||||
this(
|
this(
|
||||||
(new RandomAccessFile(file, readOnly? "r" : "rw")).getChannel(),
|
(new RandomAccessFile(file, readOnly? "r" : "rw")).getChannel(),
|
||||||
|
readOnly,
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -176,17 +177,17 @@ public class NPOIFSFileSystem extends BlockStore
|
||||||
* when you're done to have the underlying Channel closed, as the channel is
|
* 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>
|
* 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
|
* @exception IOException on errors reading, or on invalid data
|
||||||
*/
|
*/
|
||||||
public NPOIFSFileSystem(FileChannel channel)
|
public NPOIFSFileSystem(FileChannel channel)
|
||||||
throws IOException
|
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
|
throws IOException
|
||||||
{
|
{
|
||||||
this(false);
|
this(false);
|
||||||
|
@ -200,7 +201,7 @@ public class NPOIFSFileSystem extends BlockStore
|
||||||
_header = new HeaderBlock(headerBuffer);
|
_header = new HeaderBlock(headerBuffer);
|
||||||
|
|
||||||
// Now process the various entries
|
// Now process the various entries
|
||||||
_data = new FileBackedDataSource(channel);
|
_data = new FileBackedDataSource(channel, readOnly);
|
||||||
readCoreContents();
|
readCoreContents();
|
||||||
} catch(IOException e) {
|
} catch(IOException e) {
|
||||||
if(closeChannelOnError) {
|
if(closeChannelOnError) {
|
||||||
|
|
|
@ -31,13 +31,10 @@ import org.apache.poi.util.IOUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A POIFS {@link DataSource} backed by a File
|
* 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 {
|
public class FileBackedDataSource extends DataSource {
|
||||||
private FileChannel channel;
|
private FileChannel channel;
|
||||||
|
private boolean writable;
|
||||||
|
|
||||||
@SuppressWarnings("resource")
|
@SuppressWarnings("resource")
|
||||||
public FileBackedDataSource(File file) throws FileNotFoundException {
|
public FileBackedDataSource(File file) throws FileNotFoundException {
|
||||||
|
@ -45,10 +42,12 @@ public class FileBackedDataSource extends DataSource {
|
||||||
throw new FileNotFoundException(file.toString());
|
throw new FileNotFoundException(file.toString());
|
||||||
}
|
}
|
||||||
this.channel = (new RandomAccessFile(file, "r")).getChannel();
|
this.channel = (new RandomAccessFile(file, "r")).getChannel();
|
||||||
|
this.writable = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public FileBackedDataSource(FileChannel channel) {
|
public FileBackedDataSource(FileChannel channel, boolean readOnly) {
|
||||||
this.channel = channel;
|
this.channel = channel;
|
||||||
|
this.writable = !readOnly;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -56,11 +55,19 @@ public class FileBackedDataSource extends DataSource {
|
||||||
if(position >= size()) {
|
if(position >= size()) {
|
||||||
throw new IllegalArgumentException("Position " + position + " past the end of the file");
|
throw new IllegalArgumentException("Position " + position + " past the end of the file");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read
|
// Do we read or map (for read/write?
|
||||||
channel.position(position);
|
ByteBuffer dst;
|
||||||
ByteBuffer dst = ByteBuffer.allocate(length);
|
int worked = -1;
|
||||||
int worked = IOUtils.readFully(channel, dst);
|
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
|
// Check
|
||||||
if(worked == -1) {
|
if(worked == -1) {
|
||||||
|
|
|
@ -21,8 +21,6 @@ import org.junit.runner.RunWith;
|
||||||
import org.junit.runners.Suite;
|
import org.junit.runners.Suite;
|
||||||
/**
|
/**
|
||||||
* Test suite for org.apache.poi.hpsf.basic
|
* Test suite for org.apache.poi.hpsf.basic
|
||||||
*
|
|
||||||
* @author Josh Micich
|
|
||||||
*/
|
*/
|
||||||
@RunWith(Suite.class)
|
@RunWith(Suite.class)
|
||||||
@Suite.SuiteClasses({
|
@Suite.SuiteClasses({
|
||||||
|
|
|
@ -75,7 +75,6 @@ import org.apache.poi.util.IOUtils;
|
||||||
import org.apache.poi.util.LittleEndian;
|
import org.apache.poi.util.LittleEndian;
|
||||||
import org.apache.poi.util.TempFile;
|
import org.apache.poi.util.TempFile;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Ignore;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -811,7 +810,6 @@ public class TestWrite
|
||||||
* without needing to stream in + out the whole kitchen sink
|
* without needing to stream in + out the whole kitchen sink
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
@Ignore
|
|
||||||
public void inPlaceNPOIFSWrite() throws Exception {
|
public void inPlaceNPOIFSWrite() throws Exception {
|
||||||
NPOIFSFileSystem fs = null;
|
NPOIFSFileSystem fs = null;
|
||||||
DirectoryEntry root = null;
|
DirectoryEntry root = null;
|
||||||
|
|
Loading…
Reference in New Issue