You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FileBackedDataSource.java 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.poifs.nio;
  16. import java.io.Closeable;
  17. import java.io.File;
  18. import java.io.FileNotFoundException;
  19. import java.io.IOException;
  20. import java.io.OutputStream;
  21. import java.io.RandomAccessFile;
  22. import java.nio.ByteBuffer;
  23. import java.nio.channels.Channels;
  24. import java.nio.channels.FileChannel;
  25. import java.nio.channels.WritableByteChannel;
  26. import java.util.IdentityHashMap;
  27. import org.apache.logging.log4j.LogManager;
  28. import org.apache.logging.log4j.Logger;
  29. import org.apache.poi.util.IOUtils;
  30. /**
  31. * A POIFS {@link DataSource} backed by a File
  32. */
  33. public class FileBackedDataSource extends DataSource implements Closeable {
  34. private static final Logger LOG = LogManager.getLogger(FileBackedDataSource.class);
  35. private final FileChannel channel;
  36. private Long channelSize;
  37. private final boolean writable;
  38. private final boolean closeChannelOnClose;
  39. // remember file base, which needs to be closed too
  40. private final RandomAccessFile srcFile;
  41. // Buffers which map to a file-portion are not closed automatically when the Channel is closed
  42. // therefore we need to keep the list of mapped buffers and do some ugly reflection to try to
  43. // clean the buffer during close().
  44. // See https://bz.apache.org/bugzilla/show_bug.cgi?id=58480,
  45. private final IdentityHashMap<ByteBuffer,ByteBuffer> buffersToClean = new IdentityHashMap<>();
  46. public FileBackedDataSource(File file) throws FileNotFoundException {
  47. this(newSrcFile(file, "r"), true);
  48. }
  49. public FileBackedDataSource(File file, boolean readOnly) throws FileNotFoundException {
  50. this(newSrcFile(file, readOnly ? "r" : "rw"), readOnly);
  51. }
  52. public FileBackedDataSource(RandomAccessFile srcFile, boolean readOnly) {
  53. this(srcFile, srcFile.getChannel(), readOnly, false);
  54. }
  55. public FileBackedDataSource(FileChannel channel, boolean readOnly) {
  56. this(channel, readOnly, true);
  57. }
  58. /**
  59. * @since POI 5.1.0
  60. */
  61. public FileBackedDataSource(FileChannel channel, boolean readOnly, boolean closeChannelOnClose) {
  62. this(null, channel, readOnly, closeChannelOnClose);
  63. }
  64. private FileBackedDataSource(RandomAccessFile srcFile, FileChannel channel, boolean readOnly, boolean closeChannelOnClose) {
  65. this.srcFile = srcFile;
  66. this.channel = channel;
  67. this.writable = !readOnly;
  68. this.closeChannelOnClose = closeChannelOnClose;
  69. }
  70. public boolean isWriteable() {
  71. return this.writable;
  72. }
  73. public FileChannel getChannel() {
  74. return this.channel;
  75. }
  76. @Override
  77. public ByteBuffer read(int length, long position) throws IOException {
  78. if (position >= size()) {
  79. throw new IndexOutOfBoundsException("Position " + position + " past the end of the file");
  80. }
  81. // TODO Could we do the read-only case with MapMode.PRIVATE instead?
  82. // See https://docs.oracle.com/javase/7/docs/api/java/nio/channels/FileChannel.MapMode.html#PRIVATE
  83. // Or should we have 3 modes instead of the current boolean -
  84. // read-write, read-only, read-to-write-elsewhere?
  85. // Do we read or map (for read/write)?
  86. ByteBuffer dst;
  87. if (writable) {
  88. dst = channel.map(FileChannel.MapMode.READ_WRITE, position, length);
  89. // remember this buffer for cleanup
  90. buffersToClean.put(dst,dst);
  91. } else {
  92. channel.position(position);
  93. // allocate the buffer on the heap if we cannot map the data in directly
  94. dst = ByteBuffer.allocate(length);
  95. // Read the contents and check that we could read some data
  96. int worked = IOUtils.readFully(channel, dst);
  97. if (worked == -1) {
  98. throw new IndexOutOfBoundsException("Position " + position + " past the end of the file");
  99. }
  100. }
  101. // make it ready for reading
  102. dst.position(0);
  103. // All done
  104. return dst;
  105. }
  106. @Override
  107. public void write(ByteBuffer src, long position) throws IOException {
  108. channel.write(src, position);
  109. // we have to re-read size if we write "after" the recorded one
  110. if(channelSize != null && position >= channelSize) {
  111. channelSize = null;
  112. }
  113. }
  114. @Override
  115. public void copyTo(OutputStream stream) throws IOException {
  116. // Wrap the OutputSteam as a channel
  117. try (WritableByteChannel out = Channels.newChannel(stream)) {
  118. // Now do the transfer
  119. channel.transferTo(0, channel.size(), out);
  120. }
  121. }
  122. @Override
  123. public long size() throws IOException {
  124. // this is called often and profiling showed that channel.size()
  125. // was taking a large part of processing-time, so we only read it
  126. // once
  127. if(channelSize == null) {
  128. channelSize = channel.size();
  129. }
  130. return channelSize;
  131. }
  132. public void releaseBuffer(ByteBuffer buffer) {
  133. ByteBuffer previous = buffersToClean.remove(buffer);
  134. if (previous != null) {
  135. unmap(previous);
  136. }
  137. }
  138. @Override
  139. public void close() throws IOException {
  140. // also ensure that all buffers are unmapped so we do not keep files locked on Windows
  141. // We consider it a bug if a Buffer is still in use now!
  142. buffersToClean.forEach((k,v) -> unmap(v));
  143. buffersToClean.clear();
  144. if (srcFile != null) {
  145. // see http://bugs.java.com/bugdatabase/view_bug.do?bug_id=4796385
  146. srcFile.close();
  147. } else if (closeChannelOnClose) {
  148. channel.close();
  149. }
  150. }
  151. private static RandomAccessFile newSrcFile(File file, String mode) throws FileNotFoundException {
  152. if (!file.exists()) {
  153. throw new FileNotFoundException(file.toString());
  154. }
  155. return new RandomAccessFile(file, mode);
  156. }
  157. // need to use reflection to avoid depending on the sun.nio internal API
  158. // unfortunately this might break silently with newer/other Java implementations,
  159. // but we at least have unit-tests which will indicate this when run on Windows
  160. private static void unmap(final ByteBuffer buffer) {
  161. // not necessary for HeapByteBuffer, avoid lots of log-output on this class
  162. if (buffer.getClass().getName().endsWith("HeapByteBuffer")) {
  163. return;
  164. }
  165. if (CleanerUtil.UNMAP_SUPPORTED) {
  166. try {
  167. CleanerUtil.getCleaner().freeBuffer(buffer);
  168. } catch (IOException e) {
  169. LOG.atWarn().withThrowable(e).log("Failed to unmap the buffer");
  170. }
  171. } else {
  172. LOG.atDebug().log(CleanerUtil.UNMAP_NOT_SUPPORTED_REASON);
  173. }
  174. }
  175. }