summaryrefslogtreecommitdiff
path: root/core/src/main/java/net/oauth/client/ExcerptInputStream.java
diff options
context:
space:
mode:
Diffstat (limited to 'core/src/main/java/net/oauth/client/ExcerptInputStream.java')
-rwxr-xr-xcore/src/main/java/net/oauth/client/ExcerptInputStream.java45
1 files changed, 45 insertions, 0 deletions
diff --git a/core/src/main/java/net/oauth/client/ExcerptInputStream.java b/core/src/main/java/net/oauth/client/ExcerptInputStream.java
new file mode 100755
index 0000000..0dae3c3
--- /dev/null
+++ b/core/src/main/java/net/oauth/client/ExcerptInputStream.java
@@ -0,0 +1,45 @@
+package net.oauth.client;
+
+import java.io.BufferedInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * A decorator that retains a copy of the first few bytes of data.
+ * @hide
+ */
+public class ExcerptInputStream extends BufferedInputStream
+{
+ /**
+ * A marker that's appended to the excerpt if it's less than the complete
+ * stream.
+ */
+ public static final byte[] ELLIPSIS = " ...".getBytes();
+
+ public ExcerptInputStream(InputStream in) throws IOException {
+ super(in);
+ mark(LIMIT);
+ int total = 0;
+ int read;
+ while ((read = read(excerpt, total, LIMIT - total)) != -1 && ((total += read) < LIMIT));
+ if (total == LIMIT) {
+ // Only add the ellipsis if there are at least LIMIT bytes
+ System.arraycopy(ELLIPSIS, 0, excerpt, total, ELLIPSIS.length);
+ } else {
+ byte[] tmp = new byte[total];
+ System.arraycopy(excerpt, 0, tmp, 0, total);
+ excerpt = tmp;
+ }
+ reset();
+ }
+
+ private static final int LIMIT = 1024;
+ private byte[] excerpt = new byte[LIMIT + ELLIPSIS.length];
+
+ /** The first few bytes of data, plus ELLIPSIS if there are more bytes. */
+ public byte[] getExcerpt()
+ {
+ return excerpt;
+ }
+
+}