![]() |
Quadcap Embeddable Server |
00001 package com.quadcap.util.threads; 00002 00003 /* Copyright 1997 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.util.Vector; 00042 00043 import com.quadcap.util.Debug; 00044 import com.quadcap.util.DList; 00045 import com.quadcap.util.DListItem; 00046 import com.quadcap.util.ListException; 00047 00048 /** 00049 * This class implements a thread-safe stream data structure, where the 00050 * stream has a fixed-size buffer of Objects. 00051 * 00052 * @author Stan Bailes 00053 */ 00054 public class Stream { 00055 int maxSize = 10; 00056 DList queue = new DList(); 00057 boolean full = false; 00058 boolean closed = false; 00059 00060 /** 00061 * Construct a new stream using defaults 00062 */ 00063 public Stream() {} 00064 00065 /** 00066 * Construct a new stream with a specified buffer size. 00067 * 00068 * @param maxSize the maximum number of items to buffer. 00069 */ 00070 public Stream(int maxSize) { 00071 this.maxSize = maxSize; 00072 } 00073 00074 /** 00075 * Return the next item from the stream. Block if the stream's empty. 00076 * 00077 * @return the next stream item. 00078 */ 00079 public Object read() { 00080 Object obj = null; 00081 synchronized (queue) { 00082 while (queue.size() == 0) { 00083 if (closed) { 00084 return new RuntimeException("stream closed"); 00085 } 00086 try { 00087 queue.wait(); 00088 if (closed) throw new RuntimeException("stream closed"); 00089 } catch (InterruptedException e) { 00090 Debug.print(e); 00091 } 00092 } 00093 try { 00094 DListItem d = queue.popFront(); 00095 obj = d.obj; 00096 } catch (ListException e) { 00097 Debug.print(e); 00098 } 00099 if (full) { 00100 full = false; 00101 queue.notifyAll(); 00102 } 00103 } 00104 return obj; 00105 } 00106 00107 /** 00108 * Write an item to the stream. Block if the stream buffer is full. 00109 * 00110 * @param obj the object to write to the stream. 00111 */ 00112 public void write(Object obj) { 00113 synchronized (queue) { 00114 while (queue.size() >= maxSize) { 00115 if (closed) { 00116 throw new RuntimeException("stream closed"); 00117 } 00118 full = true; 00119 try { 00120 queue.wait(); 00121 } catch (InterruptedException e) { 00122 Debug.print(e); 00123 } 00124 } 00125 queue.addBack(obj); 00126 if (queue.size() == 1) queue.notifyAll(); 00127 } 00128 } 00129 00130 /** 00131 * Close this stream. 00132 */ 00133 public void close() { 00134 synchronized (queue) { 00135 closed = true; 00136 queue.notifyAll(); 00137 } 00138 } 00139 }