1 package net.oauth.client; 2 3 import java.io.BufferedInputStream; 4 import java.io.IOException; 5 import java.io.InputStream; 6 7 /** 8 * A decorator that retains a copy of the first few bytes of data. 9 * @hide 10 */ 11 public class ExcerptInputStream extends BufferedInputStream 12 { 13 /** 14 * A marker that's appended to the excerpt if it's less than the complete 15 * stream. 16 */ 17 public static final byte[] ELLIPSIS = " ...".getBytes(); 18 ExcerptInputStream(InputStream in)19 public ExcerptInputStream(InputStream in) throws IOException { 20 super(in); 21 mark(LIMIT); 22 int total = 0; 23 int read; 24 while ((read = read(excerpt, total, LIMIT - total)) != -1 && ((total += read) < LIMIT)); 25 if (total == LIMIT) { 26 // Only add the ellipsis if there are at least LIMIT bytes 27 System.arraycopy(ELLIPSIS, 0, excerpt, total, ELLIPSIS.length); 28 } else { 29 byte[] tmp = new byte[total]; 30 System.arraycopy(excerpt, 0, tmp, 0, total); 31 excerpt = tmp; 32 } 33 reset(); 34 } 35 36 private static final int LIMIT = 1024; 37 private byte[] excerpt = new byte[LIMIT + ELLIPSIS.length]; 38 39 /** The first few bytes of data, plus ELLIPSIS if there are more bytes. */ getExcerpt()40 public byte[] getExcerpt() 41 { 42 return excerpt; 43 } 44 45 } 46