1 48 49 package com.caucho.hessian.mux; 50 51 import java.io.IOException ; 52 import java.io.OutputStream ; 53 54 57 public class MuxOutputStream extends OutputStream { 58 private MuxServer server; 59 private int channel; 60 private OutputStream os; 61 62 65 public MuxOutputStream() 66 { 67 } 68 69 72 protected void init(MuxServer server, int channel) 73 throws IOException 74 { 75 this.server = server; 76 this.channel = channel; 77 this.os = null; 78 } 79 80 84 protected OutputStream getOutputStream() 85 throws IOException 86 { 87 if (os == null && server != null) 88 os = server.writeChannel(channel); 89 90 return os; 91 } 92 93 96 public int getChannel() 97 { 98 return channel; 99 } 100 101 104 public void writeURL(String url) 105 throws IOException 106 { 107 writeUTF('U', url); 108 } 109 110 113 public void write(int ch) 114 throws IOException 115 { 116 OutputStream os = getOutputStream(); 117 118 os.write('D'); 119 os.write(0); 120 os.write(1); 121 os.write(ch); 122 } 123 124 127 public void write(byte []buffer, int offset, int length) 128 throws IOException 129 { 130 OutputStream os = getOutputStream(); 131 132 for (; length > 0x8000; length -= 0x8000) { 133 os.write('D'); 134 os.write(0x80); 135 os.write(0x00); 136 os.write(buffer, offset, 0x8000); 137 138 offset += 0x8000; 139 } 140 141 os.write('D'); 142 os.write(length >> 8); 143 os.write(length); 144 os.write(buffer, offset, length); 145 } 146 147 150 public void yield() 151 throws IOException 152 { 153 OutputStream os = this.os; 154 this.os = null; 155 156 if (os != null) 157 server.yield(channel); 158 } 159 160 163 public void flush() 164 throws IOException 165 { 166 OutputStream os = this.os; 167 this.os = null; 168 169 if (os != null) 170 server.flush(channel); 171 } 172 173 176 public void close() 177 throws IOException 178 { 179 if (server != null) { 180 OutputStream os = getOutputStream(); 181 this.os = null; 182 183 MuxServer server = this.server; 184 this.server = null; 185 186 server.close(channel); 187 } 188 } 189 190 196 protected void writeUTF(int code, String string) 197 throws IOException 198 { 199 OutputStream os = getOutputStream(); 200 201 os.write(code); 202 203 int charLength = string.length(); 204 205 int length = 0; 206 for (int i = 0; i < charLength; i++) { 207 char ch = string.charAt(i); 208 209 if (ch < 0x80) 210 length++; 211 else if (ch < 0x800) 212 length += 2; 213 else 214 length += 3; 215 } 216 217 os.write(length >> 8); 218 os.write(length); 219 220 for (int i = 0; i < length; i++) { 221 char ch = string.charAt(i); 222 223 if (ch < 0x80) 224 os.write(ch); 225 else if (ch < 0x800) { 226 os.write(0xc0 + (ch >> 6) & 0x1f); 227 os.write(0x80 + (ch & 0x3f)); 228 } 229 else { 230 os.write(0xe0 + (ch >> 12) & 0xf); 231 os.write(0x80 + ((ch >> 6) & 0x3f)); 232 os.write(0x80 + (ch & 0x3f)); 233 } 234 } 235 } 236 } 237 | Popular Tags |