001: /*
002: * Copyright (c) 2001-2004 Caucho Technology, Inc. All rights reserved.
003: *
004: * The Apache Software License, Version 1.1
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution, if
019: * any, must include the following acknowlegement:
020: * "This product includes software developed by the
021: * Caucho Technology (http://www.caucho.com/)."
022: * Alternately, this acknowlegement may appear in the software itself,
023: * if and wherever such third-party acknowlegements normally appear.
024: *
025: * 4. The names "Hessian", "Resin", and "Caucho" must not be used to
026: * endorse or promote products derived from this software without prior
027: * written permission. For written permission, please contact
028: * info@caucho.com.
029: *
030: * 5. Products derived from this software may not be called "Resin"
031: * nor may "Resin" appear in their names without prior written
032: * permission of Caucho Technology.
033: *
034: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
035: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
036: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
037: * DISCLAIMED. IN NO EVENT SHALL CAUCHO TECHNOLOGY OR ITS CONTRIBUTORS
038: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
039: * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
040: * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
041: * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
042: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
043: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
044: * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
045: *
046: * @author Scott Ferguson
047: */
048:
049: package com.caucho.hessian.mux;
050:
051: import java.io.IOException;
052: import java.io.OutputStream;
053:
054: /**
055: * Output stream to a specific channel.
056: */
057: public class MuxOutputStream extends OutputStream {
058: private MuxServer server;
059: private int channel;
060: private OutputStream os;
061:
062: /**
063: * Null argument constructor.
064: */
065: public MuxOutputStream() {
066: }
067:
068: /**
069: * Initialize the multiplexor with input and output streams.
070: */
071: protected void init(MuxServer server, int channel)
072: throws IOException {
073: this .server = server;
074: this .channel = channel;
075: this .os = null;
076: }
077:
078: /**
079: * Gets the raw output stream. Clients will normally not call
080: * this.
081: */
082: protected OutputStream getOutputStream() throws IOException {
083: if (os == null && server != null)
084: os = server.writeChannel(channel);
085:
086: return os;
087: }
088:
089: /**
090: * Gets the channel of the connection.
091: */
092: public int getChannel() {
093: return channel;
094: }
095:
096: /**
097: * Writes a URL to the stream.
098: */
099: public void writeURL(String url) throws IOException {
100: writeUTF('U', url);
101: }
102:
103: /**
104: * Writes a data byte to the output stream.
105: */
106: public void write(int ch) throws IOException {
107: OutputStream os = getOutputStream();
108:
109: os.write('D');
110: os.write(0);
111: os.write(1);
112: os.write(ch);
113: }
114:
115: /**
116: * Writes data to the output stream.
117: */
118: public void write(byte[] buffer, int offset, int length)
119: throws IOException {
120: OutputStream os = getOutputStream();
121:
122: for (; length > 0x8000; length -= 0x8000) {
123: os.write('D');
124: os.write(0x80);
125: os.write(0x00);
126: os.write(buffer, offset, 0x8000);
127:
128: offset += 0x8000;
129: }
130:
131: os.write('D');
132: os.write(length >> 8);
133: os.write(length);
134: os.write(buffer, offset, length);
135: }
136:
137: /**
138: * Flush data to the output stream.
139: */
140: public void yield() throws IOException {
141: OutputStream os = this .os;
142: this .os = null;
143:
144: if (os != null)
145: server.yield(channel);
146: }
147:
148: /**
149: * Flush data to the output stream.
150: */
151: public void flush() throws IOException {
152: OutputStream os = this .os;
153: this .os = null;
154:
155: if (os != null)
156: server.flush(channel);
157: }
158:
159: /**
160: * Complete writing to the stream, closing the channel.
161: */
162: public void close() throws IOException {
163: if (server != null) {
164: OutputStream os = getOutputStream();
165: this .os = null;
166:
167: MuxServer server = this .server;
168: this .server = null;
169:
170: server.close(channel);
171: }
172: }
173:
174: /**
175: * Writes a UTF-8 string.
176: *
177: * @param code the HMUX code identifying the string
178: * @param string the string to write
179: */
180: protected void writeUTF(int code, String string) throws IOException {
181: OutputStream os = getOutputStream();
182:
183: os.write(code);
184:
185: int charLength = string.length();
186:
187: int length = 0;
188: for (int i = 0; i < charLength; i++) {
189: char ch = string.charAt(i);
190:
191: if (ch < 0x80)
192: length++;
193: else if (ch < 0x800)
194: length += 2;
195: else
196: length += 3;
197: }
198:
199: os.write(length >> 8);
200: os.write(length);
201:
202: for (int i = 0; i < length; i++) {
203: char ch = string.charAt(i);
204:
205: if (ch < 0x80)
206: os.write(ch);
207: else if (ch < 0x800) {
208: os.write(0xc0 + (ch >> 6) & 0x1f);
209: os.write(0x80 + (ch & 0x3f));
210: } else {
211: os.write(0xe0 + (ch >> 12) & 0xf);
212: os.write(0x80 + ((ch >> 6) & 0x3f));
213: os.write(0x80 + (ch & 0x3f));
214: }
215: }
216: }
217: }
|