1 16 package org.mortbay.http.ajp; 17 18 import java.io.IOException ; 19 import java.io.InputStream ; 20 import java.io.OutputStream ; 21 22 public class AJP13InputStream extends InputStream 23 { 24 25 private AJP13RequestPacket _packet; 26 private AJP13RequestPacket _getBodyChunk; 27 private InputStream _in; 28 private OutputStream _out; 29 private boolean _gotFirst=false; 30 private boolean _closed; 31 32 33 AJP13InputStream(InputStream in, OutputStream out, int bufferSize) 34 { 35 _in=in; 36 _out=out; 37 _packet=new AJP13RequestPacket(bufferSize); 38 _getBodyChunk=new AJP13RequestPacket(8); 39 _getBodyChunk.addByte((byte)'A'); 40 _getBodyChunk.addByte((byte)'B'); 41 _getBodyChunk.addInt(3); 42 _getBodyChunk.addByte(AJP13RequestPacket.__GET_BODY_CHUNK); 43 _getBodyChunk.addInt(bufferSize); 44 } 45 46 47 public void resetStream() 48 { 49 _gotFirst=false; 50 _closed=false; 51 _packet.reset(); 52 } 53 54 55 public void destroy() 56 { 57 if (_packet!=null) 58 _packet.destroy(); 59 _packet=null; 60 if (_getBodyChunk!=null) 61 _getBodyChunk.destroy(); 62 _getBodyChunk=null; 63 _in=null; 64 _out=null; 65 } 66 67 68 public int available() throws IOException 69 { 70 if (_closed) 71 return 0; 72 if (_packet.unconsumedData()==0) 73 fillPacket(); 74 return _packet.unconsumedData(); 75 } 76 77 78 public void close() throws IOException 79 { 80 _closed=true; 81 } 82 83 84 public void mark(int readLimit) 85 { 86 } 87 88 89 public boolean markSupported() 90 { 91 return false; 92 } 93 94 95 public void reset() throws IOException 96 { 97 throw new IOException ("reset() not supported"); 98 } 99 100 101 public int read() throws IOException 102 { 103 if (_closed) 104 return -1; 105 106 if (_packet.unconsumedData()<=0) 107 { 108 fillPacket(); 109 if (_packet.unconsumedData()<=0) 110 { 111 _closed=true; 112 return -1; 113 } 114 } 115 return _packet.getByte(); 116 } 117 118 119 public int read(byte[] b, int off, int len) throws IOException 120 { 121 if (_closed) 122 return -1; 123 124 if (_packet.unconsumedData()==0) 125 { 126 fillPacket(); 127 if (_packet.unconsumedData()==0) 128 { 129 _closed=true; 130 return -1; 131 } 132 } 133 134 return _packet.getBytes(b,off,len); 135 } 136 137 138 143 public AJP13RequestPacket nextPacket() throws IOException 144 { 145 if (_packet.read(_in)) 146 return _packet; 147 return null; 148 } 149 150 151 private void fillPacket() throws IOException 152 { 153 if (_closed) 154 return; 155 156 if (_gotFirst||_in.available()==0) 157 _getBodyChunk.write(_out); 158 _gotFirst=true; 159 160 if (!_packet.read(_in)) 162 throw new IOException ("EOF"); 163 164 if (_packet.unconsumedData()<=0) 165 _closed=true; 166 else if (_packet.getInt()>_packet.getBufferSize()) 167 throw new IOException ("AJP Protocol error"); 168 } 169 170 171 public long skip(long n) throws IOException 172 { 173 if (_closed) 174 return -1; 175 176 for (int i=0; i<n; i++) 177 if (read()<0) 178 return i==0?-1:i; 179 return n; 180 } 181 } 182 | Popular Tags |