KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > net > server > WorkerOutputStream


1 package com.quadcap.net.server;
2
3 /* Copyright 1999 - 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.io.FileOutputStream JavaDoc;
42 import java.io.IOException JavaDoc;
43 import java.io.OutputStream JavaDoc;
44
45 import com.quadcap.util.Debug;
46
47 /**
48  * An efficient worker output stream, which is <B>NOT</B> destroyed after
49  * each session, to reduce allocation overhead. This class performs
50  * all necessary buffering of the Socket OutputStream, so no extra
51  * "buffer stream" classes are needed.
52  *
53  * @author Stan Bailes
54  */

55 public final class WorkerOutputStream extends OutputStream JavaDoc {
56     static final int MAX = 4096;
57     byte[] buf = new byte[MAX];
58     byte[] temp = new byte[16];
59     int pos = 0;
60     OutputStream JavaDoc out;
61
62     //#ifdef DEBUG
63
static boolean doTrace = false;
64     FileOutputStream JavaDoc log;
65     //#endif
66

67     public WorkerOutputStream(FileOutputStream JavaDoc log) {
68         //#ifdef DEBUG
69
doTrace = (log != null);
70         this.log = log;
71         //#endif
72
}
73
74     public final void reset(OutputStream JavaDoc out) throws IOException JavaDoc {
75         this.out = out;
76         this.pos = 0;
77         //#ifdef DEBUG
78
if (doTrace) {
79             log.write(("RESET " + Thread.currentThread().getName() +
80                        "\r\n").getBytes());
81         }
82         //#endif
83
}
84
85     public final void write(int c) throws IOException JavaDoc {
86         if (pos >= MAX) {
87             owrite(buf, 0, MAX);
88         }
89         buf[pos++] = (byte)c;
90     }
91
92     final void owrite(byte[] b, int off, int len) throws IOException JavaDoc {
93         out.write(b, off, len);
94         pos = 0;
95         //#ifdef DEBUG
96
if (doTrace) {
97             log.write("WRITE\r\n".getBytes());
98             log.write(b, off, len);
99         }
100         //#endif
101
}
102
103     public final void write(String JavaDoc s) throws IOException JavaDoc {
104         int len = s.length();
105         int off = 0;
106         while (len + pos >= MAX) {
107             int slen = MAX - pos;
108             s.getBytes(off, off + slen, buf, pos);
109             owrite(buf, 0, MAX);
110             off += slen;
111             len -= slen;
112             pos = 0;
113         }
114         if (len > 0) {
115             s.getBytes(off, len, buf, pos);
116             pos += len;
117         }
118     }
119     
120     public final void write(byte[] b, int off, int len) throws IOException JavaDoc {
121         final int npos = pos + len;
122         if (npos >= MAX) {
123             if (pos == 0) {
124                 owrite(b, off, len);
125             } else {
126                 final int slen = MAX - pos;
127                 System.arraycopy(b, off, buf, pos, slen);
128                 owrite(buf, 0, MAX);
129                 len -= slen;
130                 if (len < MAX) {
131                     System.arraycopy(b, off + slen, buf, 0, len);
132                     pos = len;
133                 } else {
134                     pos = 0;
135                     owrite(b, off + slen, len);
136                 }
137             }
138         } else {
139             System.arraycopy(b, off, buf, pos, len);
140             pos = npos;
141         }
142     }
143
144     public final void write(byte[] b) throws IOException JavaDoc {
145         write(b, 0, b.length);
146     }
147
148     public final void flush() throws IOException JavaDoc {
149         if (pos > 0) {
150             owrite(buf, 0, pos);
151             pos = 0;
152         }
153         out.flush();
154     }
155
156     static final byte[] digits = "0123456789".getBytes();
157
158     public final void writeInt(int x) throws IOException JavaDoc {
159         int p = 0;
160         while (x > 0) {
161             temp[p++] = digits[x % 10];
162             x /= 10;
163         }
164         if (p == 0) temp[p++] = (byte)'0';
165         if (pos + p < MAX) {
166             while (p > 0) buf[pos++] = temp[--p];
167         } else {
168             while (p > 0) write(temp[--p]);
169         }
170     }
171
172     public final void close() throws IOException JavaDoc {
173         flush();
174         out.close();
175         //#ifdef DEBUG
176
if (doTrace) log.write("CLOSE\r\n".getBytes());
177         //#endif
178
}
179 }
180
Popular Tags