KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > util > threads > Stream


1 package com.quadcap.util.threads;
2
3 /* Copyright 1997 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.util.Vector JavaDoc;
42
43 import com.quadcap.util.Debug;
44 import com.quadcap.util.DList;
45 import com.quadcap.util.DListItem;
46 import com.quadcap.util.ListException;
47
48 /**
49  * This class implements a thread-safe stream data structure, where the
50  * stream has a fixed-size buffer of Objects.
51  *
52  * @author Stan Bailes
53  */

54 public class Stream {
55     int maxSize = 10;
56     DList queue = new DList();
57     boolean full = false;
58     boolean closed = false;
59
60     /**
61      * Construct a new stream using defaults
62      */

63     public Stream() {}
64
65     /**
66      * Construct a new stream with a specified buffer size.
67      *
68      * @param maxSize the maximum number of items to buffer.
69      */

70     public Stream(int maxSize) {
71     this.maxSize = maxSize;
72     }
73
74     /**
75      * Return the next item from the stream. Block if the stream's empty.
76      *
77      * @return the next stream item.
78      */

79     public Object JavaDoc read() {
80     Object JavaDoc obj = null;
81     synchronized (queue) {
82         while (queue.size() == 0) {
83                 if (closed) {
84                     return new RuntimeException JavaDoc("stream closed");
85                 }
86         try {
87             queue.wait();
88             if (closed) throw new RuntimeException JavaDoc("stream closed");
89         } catch (InterruptedException JavaDoc e) {
90             Debug.print(e);
91         }
92         }
93         try {
94         DListItem d = queue.popFront();
95         obj = d.obj;
96         } catch (ListException e) {
97         Debug.print(e);
98         }
99         if (full) {
100         full = false;
101         queue.notifyAll();
102         }
103     }
104     return obj;
105     }
106
107     /**
108      * Write an item to the stream. Block if the stream buffer is full.
109      *
110      * @param obj the object to write to the stream.
111      */

112     public void write(Object JavaDoc obj) {
113     synchronized (queue) {
114         while (queue.size() >= maxSize) {
115                 if (closed) {
116                     throw new RuntimeException JavaDoc("stream closed");
117                 }
118         full = true;
119         try {
120             queue.wait();
121         } catch (InterruptedException JavaDoc e) {
122             Debug.print(e);
123         }
124         }
125         queue.addBack(obj);
126         if (queue.size() == 1) queue.notifyAll();
127     }
128     }
129
130     /**
131      * Close this stream.
132      */

133     public void close() {
134     synchronized (queue) {
135         closed = true;
136         queue.notifyAll();
137     }
138     }
139 }
140
Popular Tags