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