KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > stream > NotifyingBufferedInputStream


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.util.stream;
23
24 import java.io.BufferedInputStream JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27
28 /**
29  * A buffered input stream that notifies every "chunk"
30  *
31  * @version <tt>$Revision: 1958 $</tt>
32  * @author <a HREF="mailto:Adrian@jboss.org">Adrian Brock</a>
33  */

34 public class NotifyingBufferedInputStream
35    extends BufferedInputStream JavaDoc
36 {
37    /**
38     * The number of bytes between notifications
39     */

40    int chunkSize;
41
42    /**
43     * The number of bytes read in the current chunk
44     */

45    int chunk = 0;
46
47    /**
48     * The listener notified every chunk
49     */

50    StreamListener listener;
51
52    /**
53     * Construct a notifying buffered inputstream.
54     * The listener is notified once every chunk.
55     *
56     * @param is the input stream to be buffered
57     * @param size the buffer size
58     * @param chunkSize the chunk size
59     * @param listener the listener to notify
60     * @exception IllegalArgumentException for a size <= 0 or chunkSize <= size
61     */

62    public NotifyingBufferedInputStream(InputStream JavaDoc is, int size, int chunkSize, StreamListener listener)
63    {
64       super(is, size);
65       if (chunkSize <= size)
66          throw new IllegalArgumentException JavaDoc("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 JavaDoc
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 JavaDoc
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    /**
97     * Checks whether a notification is required and
98     * notifies as appropriate
99     *
100     * @param result the number of bytes read
101     */

102    public void checkNotification(int result)
103    {
104       // Is a notification required?
105
chunk += result;
106       if (chunk >= chunkSize)
107       {
108          if (listener != null)
109             listener.onStreamNotification(this, chunk);
110
111          // Start a new chunk
112
chunk = 0;
113       }
114    }
115 }
116
Popular Tags