1 16 17 package org.apache.coyote.tomcat4; 18 19 import java.io.IOException ; 20 21 import javax.servlet.ServletInputStream ; 22 23 import org.apache.coyote.Request; 24 import org.apache.tomcat.util.buf.ByteChunk; 25 26 27 32 public class CoyoteInputStream extends ServletInputStream { 33 34 35 37 38 private boolean closed = false; 39 40 private Request coyoteRequest; 41 42 private ByteChunk readChunk = new ByteChunk(); 43 44 47 private int pos = -1; 48 private int end = -1; 49 private byte[] readBuffer = null; 50 51 52 54 55 void setRequest(Request coyoteRequest) { 56 this.coyoteRequest = coyoteRequest; 57 } 58 59 60 63 void recycle() { 64 closed = false; 65 pos = -1; 66 end = -1; 67 readBuffer = null; 68 } 69 70 71 73 74 public int read() 75 throws IOException { 76 77 if (closed) 78 throw new IOException ("Stream closed"); 79 80 while (pos >= end) { 82 if (readBytes() < 0) 83 return -1; 84 } 85 86 return (readBuffer[pos++] & 0xFF); 87 88 } 89 90 public int available() throws IOException { 91 if( pos < end ) return end-pos; 92 return 0; 93 } 94 95 public int read(byte[] b) throws IOException { 96 97 return read(b, 0, b.length); 98 99 } 100 101 102 public int read(byte[] b, int off, int len) 103 throws IOException { 104 105 if (closed) 106 throw new IOException ("Stream closed"); 107 108 while (pos >= end) { 110 if (readBytes() < 0) 111 return -1; 112 } 113 114 int n = -1; 115 if ((end - pos) > len) { 116 n = len; 117 } else { 118 n = end - pos; 119 } 120 121 System.arraycopy(readBuffer, pos, b, off, n); 122 123 pos += n; 124 return n; 125 126 } 127 128 129 public int readLine(byte[] b, int off, int len) throws IOException { 130 return super.readLine(b, off, len); 131 } 132 133 134 139 public void close() { 140 closed = true; 141 } 142 143 144 146 147 150 protected int readBytes() 151 throws IOException { 152 153 int result = coyoteRequest.doRead(readChunk); 154 if (result > 0) { 155 readBuffer = readChunk.getBytes(); 156 end = readChunk.getEnd(); 157 pos = readChunk.getStart(); 158 } 159 return result; 160 161 } 162 163 164 } 165
| Popular Tags
|