You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by GitBox <gi...@apache.org> on 2021/03/05 14:24:54 UTC

[GitHub] [camel] ppalaga commented on a change in pull request #5175: CAMEL-16307 azure-storage-datalake:...?operation=getFile appends newline

ppalaga commented on a change in pull request #5175:
URL: https://github.com/apache/camel/pull/5175#discussion_r588332285



##########
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:
       I think this is safer, because if some subclass overrides our `markSupported()` or callers will ignore our `markSupported()` then they should fail badly instead of being ignored silently.




----------------------------------------------------------------
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:
users@infra.apache.org