2010-12-18 10:18:43 +00:00
|
|
|
/* ====================================================================
|
|
|
|
|
Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
|
contributor license agreements. See the NOTICE file distributed with
|
|
|
|
|
this work for additional information regarding copyright ownership.
|
|
|
|
|
The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
|
(the "License"); you may not use this file except in compliance with
|
|
|
|
|
the License. You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
==================================================================== */
|
|
|
|
|
|
|
|
|
|
package org.apache.poi.poifs.nio;
|
|
|
|
|
|
2010-12-19 04:59:49 +00:00
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.FileNotFoundException;
|
2010-12-18 10:18:43 +00:00
|
|
|
import java.io.IOException;
|
2010-12-28 05:31:32 +00:00
|
|
|
import java.io.OutputStream;
|
2010-12-19 04:59:49 +00:00
|
|
|
import java.io.RandomAccessFile;
|
2015-10-14 14:53:41 +00:00
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
|
|
import java.lang.reflect.Method;
|
2010-12-18 10:18:43 +00:00
|
|
|
import java.nio.ByteBuffer;
|
2010-12-28 05:31:32 +00:00
|
|
|
import java.nio.channels.Channels;
|
2010-12-18 10:18:43 +00:00
|
|
|
import java.nio.channels.FileChannel;
|
2010-12-28 05:31:32 +00:00
|
|
|
import java.nio.channels.WritableByteChannel;
|
2015-10-14 14:53:41 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
2010-12-18 10:18:43 +00:00
|
|
|
|
2010-12-19 04:59:49 +00:00
|
|
|
import org.apache.poi.util.IOUtils;
|
|
|
|
|
|
2010-12-18 10:18:43 +00:00
|
|
|
/**
|
|
|
|
|
* A POIFS {@link DataSource} backed by a File
|
|
|
|
|
*/
|
|
|
|
|
public class FileBackedDataSource extends DataSource {
|
2010-12-19 04:59:49 +00:00
|
|
|
private FileChannel channel;
|
2014-04-28 06:35:39 +00:00
|
|
|
private boolean writable;
|
2014-05-04 20:58:42 +00:00
|
|
|
// remember file base, which needs to be closed too
|
|
|
|
|
private RandomAccessFile srcFile;
|
2015-10-14 14:53:41 +00:00
|
|
|
|
|
|
|
|
// Buffers which map to a file-portion are not closed automatically when the Channel is closed
|
|
|
|
|
// therefore we need to keep the list of mapped buffers and do some ugly reflection to try to
|
|
|
|
|
// clean the buffer during close().
|
|
|
|
|
// See https://bz.apache.org/bugzilla/show_bug.cgi?id=58480,
|
|
|
|
|
// http://stackoverflow.com/questions/3602783/file-access-synchronized-on-java-object and
|
|
|
|
|
// http://bugs.java.com/view_bug.do?bug_id=4724038 for related discussions
|
|
|
|
|
private List<ByteBuffer> buffersToClean = new ArrayList<ByteBuffer>();
|
2013-12-26 09:38:48 +00:00
|
|
|
|
2010-12-19 04:59:49 +00:00
|
|
|
public FileBackedDataSource(File file) throws FileNotFoundException {
|
2014-05-04 20:58:42 +00:00
|
|
|
this(newSrcFile(file, "r"), true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public FileBackedDataSource(File file, boolean readOnly) throws FileNotFoundException {
|
|
|
|
|
this(newSrcFile(file, readOnly ? "r" : "rw"), readOnly);
|
2010-12-19 04:59:49 +00:00
|
|
|
}
|
2013-12-26 09:38:48 +00:00
|
|
|
|
2014-05-04 20:58:42 +00:00
|
|
|
public FileBackedDataSource(RandomAccessFile srcFile, boolean readOnly) {
|
|
|
|
|
this(srcFile.getChannel(), readOnly);
|
|
|
|
|
this.srcFile = srcFile;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-28 06:35:39 +00:00
|
|
|
public FileBackedDataSource(FileChannel channel, boolean readOnly) {
|
2010-12-19 04:59:49 +00:00
|
|
|
this.channel = channel;
|
2014-04-28 06:35:39 +00:00
|
|
|
this.writable = !readOnly;
|
2010-12-18 10:18:43 +00:00
|
|
|
}
|
2014-04-28 09:22:58 +00:00
|
|
|
|
|
|
|
|
public boolean isWriteable() {
|
|
|
|
|
return this.writable;
|
|
|
|
|
}
|
2014-05-04 20:58:42 +00:00
|
|
|
|
|
|
|
|
public FileChannel getChannel() {
|
|
|
|
|
return this.channel;
|
|
|
|
|
}
|
2013-12-26 09:38:48 +00:00
|
|
|
|
|
|
|
|
@Override
|
2010-12-19 08:53:36 +00:00
|
|
|
public ByteBuffer read(int length, long position) throws IOException {
|
2010-12-19 04:59:49 +00:00
|
|
|
if(position >= size()) {
|
|
|
|
|
throw new IllegalArgumentException("Position " + position + " past the end of the file");
|
|
|
|
|
}
|
2014-04-28 06:35:39 +00:00
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
}
|
2013-12-26 09:38:48 +00:00
|
|
|
|
2010-12-19 08:53:36 +00:00
|
|
|
// Check
|
2010-12-19 04:59:49 +00:00
|
|
|
if(worked == -1) {
|
|
|
|
|
throw new IllegalArgumentException("Position " + position + " past the end of the file");
|
|
|
|
|
}
|
2013-12-26 09:38:48 +00:00
|
|
|
|
2010-12-19 08:53:36 +00:00
|
|
|
// Ready it for reading
|
|
|
|
|
dst.position(0);
|
2013-12-26 09:38:48 +00:00
|
|
|
|
2015-10-14 14:53:41 +00:00
|
|
|
// remember the buffer for cleanup if necessary
|
|
|
|
|
buffersToClean.add(dst);
|
|
|
|
|
|
2010-12-19 08:53:36 +00:00
|
|
|
// All done
|
|
|
|
|
return dst;
|
2010-12-18 10:18:43 +00:00
|
|
|
}
|
2013-12-26 09:38:48 +00:00
|
|
|
|
|
|
|
|
@Override
|
2010-12-18 10:18:43 +00:00
|
|
|
public void write(ByteBuffer src, long position) throws IOException {
|
2010-12-19 04:59:49 +00:00
|
|
|
channel.write(src, position);
|
2010-12-18 10:18:43 +00:00
|
|
|
}
|
2013-12-26 09:38:48 +00:00
|
|
|
|
|
|
|
|
@Override
|
2010-12-28 05:31:32 +00:00
|
|
|
public void copyTo(OutputStream stream) throws IOException {
|
|
|
|
|
// Wrap the OutputSteam as a channel
|
|
|
|
|
WritableByteChannel out = Channels.newChannel(stream);
|
|
|
|
|
// Now do the transfer
|
|
|
|
|
channel.transferTo(0, channel.size(), out);
|
|
|
|
|
}
|
2013-12-26 09:38:48 +00:00
|
|
|
|
|
|
|
|
@Override
|
2010-12-18 10:18:43 +00:00
|
|
|
public long size() throws IOException {
|
2010-12-19 04:59:49 +00:00
|
|
|
return channel.size();
|
2010-12-18 10:18:43 +00:00
|
|
|
}
|
2013-12-26 09:38:48 +00:00
|
|
|
|
|
|
|
|
@Override
|
2010-12-18 10:18:43 +00:00
|
|
|
public void close() throws IOException {
|
2015-10-14 14:53:41 +00:00
|
|
|
// also ensure that all buffers are unmapped so we do not keep files locked on Windows
|
|
|
|
|
// We consider it a bug if a Buffer is still in use now!
|
|
|
|
|
for(ByteBuffer buffer : buffersToClean) {
|
|
|
|
|
unmap(buffer);
|
|
|
|
|
}
|
|
|
|
|
buffersToClean.clear();
|
|
|
|
|
|
|
|
|
|
if (srcFile != null) {
|
2014-05-04 20:58:42 +00:00
|
|
|
// see http://bugs.java.com/bugdatabase/view_bug.do?bug_id=4796385
|
|
|
|
|
srcFile.close();
|
|
|
|
|
} else {
|
|
|
|
|
channel.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static RandomAccessFile newSrcFile(File file, String mode) throws FileNotFoundException {
|
|
|
|
|
if(!file.exists()) {
|
|
|
|
|
throw new FileNotFoundException(file.toString());
|
|
|
|
|
}
|
|
|
|
|
return new RandomAccessFile(file, mode);
|
2010-12-18 10:18:43 +00:00
|
|
|
}
|
2015-10-14 14:53:41 +00:00
|
|
|
|
|
|
|
|
// need to use reflection to avoid depending on the sun.nio internal API
|
|
|
|
|
// unfortunately this might break silently with newer/other Java implementations,
|
|
|
|
|
// but we at least have unit-tests which will indicate this when run on Windows
|
|
|
|
|
private static void unmap(ByteBuffer bb) {
|
|
|
|
|
Class<?> fcClass = bb.getClass();
|
|
|
|
|
try {
|
|
|
|
|
// invoke bb.cleaner().clean(), but do not depend on sun.nio
|
|
|
|
|
// interfaces
|
|
|
|
|
Method cleanerMethod = fcClass.getDeclaredMethod("cleaner");
|
|
|
|
|
cleanerMethod.setAccessible(true);
|
|
|
|
|
Object cleaner = cleanerMethod.invoke(bb);
|
|
|
|
|
Method cleanMethod = cleaner.getClass().getDeclaredMethod("clean");
|
|
|
|
|
cleanMethod.invoke(cleaner);
|
|
|
|
|
} catch (NoSuchMethodException e) {
|
|
|
|
|
// e.printStackTrace();
|
|
|
|
|
} catch (SecurityException e) {
|
|
|
|
|
// e.printStackTrace();
|
|
|
|
|
} catch (IllegalAccessException e) {
|
|
|
|
|
// e.printStackTrace();
|
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
|
// e.printStackTrace();
|
|
|
|
|
} catch (InvocationTargetException e) {
|
|
|
|
|
// e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-12-18 10:18:43 +00:00
|
|
|
}
|