From c6afcd087b79dabb47db611881c4d0f96a980d15 Mon Sep 17 00:00:00 2001 From: Henning Baldersheim Date: Thu, 18 Jun 2020 11:22:25 +0000 Subject: Add suppport for mark/reset. --- .../jdisc/handler/UnsafeContentInputStream.java | 37 +++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) (limited to 'jdisc_core/src/main') 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; /** *

Constructs a new ContentInputStream that reads from the given {@link ReadableContentChannel}.

@@ -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; + } } -- cgit v1.2.3