summaryrefslogtreecommitdiffstats
path: root/jdisc_core/src/main/java/com/yahoo/jdisc/handler/UnsafeContentInputStream.java
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2020-06-18 11:22:25 +0000
committerHenning Baldersheim <balder@yahoo-inc.com>2020-06-18 11:22:25 +0000
commitc6afcd087b79dabb47db611881c4d0f96a980d15 (patch)
tree23747c556a89a28c147abd77325411e516866da9 /jdisc_core/src/main/java/com/yahoo/jdisc/handler/UnsafeContentInputStream.java
parentb04f090f8c55eb2906e890ca8260d77e27e6ccb3 (diff)
Add suppport for mark/reset.
Diffstat (limited to 'jdisc_core/src/main/java/com/yahoo/jdisc/handler/UnsafeContentInputStream.java')
-rw-r--r--jdisc_core/src/main/java/com/yahoo/jdisc/handler/UnsafeContentInputStream.java37
1 files changed, 36 insertions, 1 deletions
diff --git a/jdisc_core/src/main/java/com/yahoo/jdisc/handler/UnsafeContentInputStream.java b/jdisc_core/src/main/java/com/yahoo/jdisc/handler/UnsafeContentInputStream.java
index 748c2951a6a..1662ed5b46a 100644
--- a/jdisc_core/src/main/java/com/yahoo/jdisc/handler/UnsafeContentInputStream.java
+++ b/jdisc_core/src/main/java/com/yahoo/jdisc/handler/UnsafeContentInputStream.java
@@ -1,6 +1,7 @@
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.handler;
+import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.Objects;
@@ -19,6 +20,8 @@ public class UnsafeContentInputStream extends InputStream {
private final ReadableContentChannel content;
private ByteBuffer buf = ByteBuffer.allocate(0);
+ private byte [] marked;
+ private int readSinceMarked;
/**
* <p>Constructs a new ContentInputStream that reads from the given {@link ReadableContentChannel}.</p>
@@ -37,7 +40,15 @@ public class UnsafeContentInputStream extends InputStream {
if (buf == null) {
return -1;
}
- return ((int)buf.get()) & 0xFF;
+ byte b = buf.get();
+ if (marked != null) {
+ if (readSinceMarked < marked.length) {
+ marked[readSinceMarked++] = b;
+ } else {
+ marked = null;
+ }
+ }
+ return ((int)b) & 0xFF;
}
@Override
@@ -79,4 +90,28 @@ public class UnsafeContentInputStream extends InputStream {
}
}
+
+ @Override
+ public synchronized void mark(int readlimit) {
+ marked = new byte[readlimit];
+ readSinceMarked = 0;
+ }
+
+ @Override
+ public synchronized void reset() throws IOException {
+ if (marked == null) {
+ throw new IOException("mark has not been called, or too much has been read since marked.");
+ }
+ ByteBuffer newBuf = ByteBuffer.allocate(readSinceMarked + buf.remaining());
+ newBuf.put(marked, 0, readSinceMarked);
+ newBuf.put(buf);
+ newBuf.flip();
+ buf = newBuf;
+ marked = null;
+ }
+
+ @Override
+ public boolean markSupported() {
+ return true;
+ }
}