[ 
https://issues.apache.org/jira/browse/TIKA-4878?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18117028#comment-18117028
 ] 

ASF GitHub Bot commented on TIKA-4878:
--------------------------------------

Copilot commented on code in PR #3209:
URL: https://github.com/apache/tika/pull/3209#discussion_r4053079332


##########
tika-core/src/main/java/org/apache/tika/io/ReopenableSource.java:
##########
@@ -363,16 +368,36 @@ public void close() throws IOException {
         }
     }
 
+    /**
+     * A mark is kept in the open stream's buffer, so a reset within {@code 
readlimit} costs
+     * nothing. Re-opening on every reset is what a reader that marks and 
resets per record (POI
+     * reading a metafile's bitmaps) turned into inflating a zip entry 
thousands of times.
+     */
     @Override
     public synchronized void mark(int readlimit) {
         markPosition = position;
+        markInStream = false;
+        if (currentStream != null && retainedBuffer == null) {
+            // the buffer grows to honour a mark; past the cap a reset 
re-opens instead
+            currentStream.mark(Math.min(readlimit, MAX_BUFFERED_MARK));
+            markInStream = true;
+        }
     }
 
     @Override
     public synchronized void reset() throws IOException {
         if (markPosition < 0) {
             throw new IOException("Mark not set");
         }
+        if (markInStream && currentStream != null) {
+            try {
+                currentStream.reset();
+                position = markPosition;

Review Comment:
   `markInStream` can be stale here: `tryRetainInMemory()` closes and replaces 
`currentStream` with a `ByteArrayInputStream` without clearing this flag. If a 
caller marks, retains/opens a channel, and then resets, this reset succeeds at 
the replacement stream's own initial mark (the current position), while 
`position` is set to the old `markPosition`, so subsequent reads start at the 
wrong offset. Invalidate the flag whenever the stream is replaced, or only take 
this fast path for the originally marked stream.





> Re-open embedded content instead of caching it when the parser can re-read 
> the source
> -------------------------------------------------------------------------------------
>
>                 Key: TIKA-4878
>                 URL: https://issues.apache.org/jira/browse/TIKA-4878
>             Project: Tika
>          Issue Type: Task
>            Reporter: Tim Allison
>            Priority: Minor
>             Fix For: 4.1.0
>
>
> For embedded streams that can point to locations on an existing file, don't 
> spool separate tmp files.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to