1 48 49 package com.caucho.hessian.mux; 50 51 import java.io.IOException ; 52 import java.io.InputStream ; 53 54 57 public class MuxInputStream extends InputStream { 58 private MuxServer server; 59 protected InputStream is; 60 private int channel; 61 62 private String url; 63 64 private int chunkLength; 65 66 69 public MuxInputStream() 70 { 71 } 72 73 76 protected void init(MuxServer server, int channel) 77 throws IOException 78 { 79 this.server = server; 80 this.channel = channel; 81 82 this.url = null; 83 84 chunkLength = 0; 85 } 86 87 91 protected InputStream getInputStream() 92 throws IOException 93 { 94 if (is == null && server != null) 95 is = server.readChannel(channel); 96 97 return is; 98 } 99 100 void setInputStream(InputStream is) 101 { 102 this.is = is; 103 } 104 105 108 public int getChannel() 109 { 110 return channel; 111 } 112 113 116 public String getURL() 117 { 118 return url; 119 } 120 121 124 public int read() 125 throws IOException 126 { 127 if (chunkLength <= 0) { 128 readToData(false); 129 130 if (chunkLength <= 0) 131 return -1; 132 } 133 134 chunkLength--; 135 return is.read(); 136 } 137 138 141 public void close() 142 throws IOException 143 { 144 skipToEnd(); 145 } 146 147 150 private void skipToEnd() 151 throws IOException 152 { 153 InputStream is = getInputStream(); 154 155 if (is == null) 156 return; 157 158 if (chunkLength > 0) 159 is.skip(chunkLength); 160 161 for (int tag = is.read(); tag >= 0; tag = is.read()) { 162 switch (tag) { 163 case 'Y': 164 server.freeReadLock(); 165 this.is = is = server.readChannel(channel); 166 if (is == null) { 167 this.server = null; 168 return; 169 } 170 break; 171 172 case 'Q': 173 server.freeReadLock(); 174 this.is = null; 175 this.server = null; 176 return; 177 178 case -1: 179 server.freeReadLock(); 180 this.is = null; 181 this.server = null; 182 return; 183 184 default: 185 int length = (is.read() << 8) + is.read(); 186 is.skip(length); 187 break; 188 } 189 } 190 } 191 192 195 void readToData(boolean returnOnYield) 196 throws IOException 197 { 198 InputStream is = getInputStream(); 199 200 if (is == null) 201 return; 202 203 for (int tag = is.read(); tag >= 0; tag = is.read()) { 204 switch (tag) { 205 case 'Y': 206 server.freeReadLock(); 207 if (returnOnYield) 208 return; 209 server.readChannel(channel); 210 break; 211 212 case 'Q': 213 server.freeReadLock(); 214 this.is = null; 215 this.server = null; 216 return; 217 218 case 'U': 219 this.url = readUTF(); 220 break; 221 222 case 'D': 223 chunkLength = (is.read() << 8) + is.read(); 224 return; 225 226 default: 227 readTag(tag); 228 break; 229 } 230 } 231 } 232 233 236 protected void readTag(int tag) 237 throws IOException 238 { 239 int length = (is.read() << 8) + is.read(); 240 is.skip(length); 241 } 242 243 248 protected String readUTF() 249 throws IOException 250 { 251 int len = (is.read() << 8) + is.read(); 252 253 StringBuffer sb = new StringBuffer (); 254 255 while (len > 0) { 256 int d1 = is.read(); 257 258 if (d1 < 0) 259 return sb.toString(); 260 else if (d1 < 0x80) { 261 len--; 262 sb.append((char) d1); 263 } 264 else if ((d1 & 0xe0) == 0xc0) { 265 len -= 2; 266 sb.append(((d1 & 0x1f) << 6) + (is.read() & 0x3f)); 267 } 268 else if ((d1 & 0xf0) == 0xe0) { 269 len -= 3; 270 sb.append(((d1 & 0x0f) << 12) + 271 ((is.read() & 0x3f) << 6) + 272 (is.read() & 0x3f)); 273 } 274 else 275 throw new IOException ("utf-8 encoding error"); 276 } 277 278 return sb.toString(); 279 } 280 } 281 | Popular Tags |