omarsmak commented on a change in pull request #5175: URL: https://github.com/apache/camel/pull/5175#discussion_r588169648
########## File path: core/camel-util/src/main/java/org/apache/camel/util/SkipLastByteInputStream.java ########## @@ -0,0 +1,92 @@ +package org.apache.camel.util; + +import java.io.BufferedInputStream; +import java.io.IOException; +import java.io.InputStream; + +/** + * An {@link InputStream} that skips the last byte of the underlying delegate {@link InputStream} if the last byte is + * equal to the given {@code matchLast} value. + */ +public class SkipLastByteInputStream extends BufferedInputStream { + + private final byte matchLast; + + public SkipLastByteInputStream(InputStream delegate, byte matchLast) { + super(delegate); + this.matchLast = matchLast; + } + + public SkipLastByteInputStream(InputStream delegate, int size, byte matchLast) { + super(delegate, size); + this.matchLast = matchLast; + } + + @Override + public int read() throws IOException { + int c = super.read(); + if (c < 0) { + return -1; + } else if (c == matchLast) { + /* look ahead */ + super.mark(1); + int nextC = super.read(); + if (nextC < 0) { + /* matchLast is the last byte */ + return -1; + } + super.reset(); + } + return c; + } + + @Override + public void close() throws IOException { + super.close(); + } + + @Override + public int read(byte[] buffer, int off, int len) throws IOException { + final int count = super.read(buffer, off, len); + if (count < 0) { + return -1; + } + final int lastIndex = off + count - 1; + if (lastIndex >= 0) { + byte lastByte = buffer[lastIndex]; + if (lastByte == matchLast) { + /* look ahead */ + super.mark(1); + int nextC = super.read(); + if (nextC < 0) { + /* matchLast is the last byte - cut it away and do not reset */ + return count - 1; + } else { + super.reset(); + } + } + } + return count; + } + + public boolean markSupported() { + /* we do not want callers to mess with mark() and reset() because we use it ourselves */ + return false; + } + + @Override + public synchronized long skip(long n) throws IOException { + throw new UnsupportedOperationException(); Review comment: IMHO I'd throw`IOException` instead of the `UnsupportedOperationException`, WDYT? ########## File path: core/camel-util/src/main/java/org/apache/camel/util/SkipLastByteInputStream.java ########## @@ -0,0 +1,92 @@ +package org.apache.camel.util; + +import java.io.BufferedInputStream; +import java.io.IOException; +import java.io.InputStream; + +/** + * An {@link InputStream} that skips the last byte of the underlying delegate {@link InputStream} if the last byte is + * equal to the given {@code matchLast} value. + */ +public class SkipLastByteInputStream extends BufferedInputStream { + + private final byte matchLast; + + public SkipLastByteInputStream(InputStream delegate, byte matchLast) { + super(delegate); + this.matchLast = matchLast; + } + + public SkipLastByteInputStream(InputStream delegate, int size, byte matchLast) { + super(delegate, size); + this.matchLast = matchLast; + } + + @Override + public int read() throws IOException { + int c = super.read(); + if (c < 0) { + return -1; + } else if (c == matchLast) { + /* look ahead */ + super.mark(1); + int nextC = super.read(); + if (nextC < 0) { + /* matchLast is the last byte */ + return -1; + } + super.reset(); + } + return c; + } + + @Override + public void close() throws IOException { + super.close(); + } + + @Override + public int read(byte[] buffer, int off, int len) throws IOException { + final int count = super.read(buffer, off, len); + if (count < 0) { + return -1; + } + final int lastIndex = off + count - 1; + if (lastIndex >= 0) { + byte lastByte = buffer[lastIndex]; + if (lastByte == matchLast) { + /* look ahead */ + super.mark(1); + int nextC = super.read(); + if (nextC < 0) { + /* matchLast is the last byte - cut it away and do not reset */ + return count - 1; + } else { + super.reset(); + } + } + } + return count; + } + + public boolean markSupported() { + /* we do not want callers to mess with mark() and reset() because we use it ourselves */ + return false; + } + + @Override + public synchronized long skip(long n) throws IOException { + throw new UnsupportedOperationException(); + } + + @Override + public synchronized void mark(int readlimit) { + throw new UnsupportedOperationException(); Review comment: Does mark need to throw an exception here? I was thinking to stick to the `InputStream` API definition and provide an empty implementation instead of throwing an exception though, WDYT? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org