1 22 package org.jboss.util.stream; 23 24 import java.io.BufferedInputStream ; 25 import java.io.InputStream ; 26 import java.io.IOException ; 27 28 34 public class NotifyingBufferedInputStream 35 extends BufferedInputStream 36 { 37 40 int chunkSize; 41 42 45 int chunk = 0; 46 47 50 StreamListener listener; 51 52 62 public NotifyingBufferedInputStream(InputStream is, int size, int chunkSize, StreamListener listener) 63 { 64 super(is, size); 65 if (chunkSize <= size) 66 throw new IllegalArgumentException ("chunkSize must be bigger than the buffer"); 67 this.chunkSize = chunkSize; 68 this.listener = listener; 69 } 70 71 public void setStreamListener(StreamListener listener) 72 { 73 this.listener = listener; 74 } 75 76 public int read() 77 throws IOException 78 { 79 int result = super.read(); 80 if (result == -1) 81 return result; 82 checkNotification(result); 83 return result; 84 } 85 86 public int read(byte[] b, int off, int len) 87 throws IOException 88 { 89 int result = super.read(b, off, len); 90 if (result == -1) 91 return result; 92 checkNotification(result); 93 return result; 94 } 95 96 102 public void checkNotification(int result) 103 { 104 chunk += result; 106 if (chunk >= chunkSize) 107 { 108 if (listener != null) 109 listener.onStreamNotification(this, chunk); 110 111 chunk = 0; 113 } 114 } 115 } 116 | Popular Tags |