1 18 19 package org.apache.struts.upload; 20 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.io.ByteArrayOutputStream ; 24 25 33 public class BufferedMultipartInputStream extends InputStream { 34 35 38 protected InputStream inputStream; 39 40 43 protected byte[] buffer; 44 45 48 protected int bufferOffset = 0; 49 50 53 protected int bufferSize = 8192; 54 55 59 protected int bufferLength = 0; 60 61 64 protected int totalLength = 0; 65 66 69 protected long contentLength; 70 71 75 protected long maxSize = -1; 76 77 80 protected boolean contentLengthMet = false; 81 82 85 protected boolean maxLengthMet = false; 86 87 88 97 public BufferedMultipartInputStream(InputStream inputStream, 98 int bufferSize, 99 long contentLength, 100 long maxSize) throws IOException { 101 this.inputStream = inputStream; 102 this.bufferSize = bufferSize; 103 this.contentLength = contentLength; 104 this.maxSize = maxSize; 105 106 if ((maxSize != -1) && (maxSize < contentLength)) { 107 throw new MaxLengthExceededException(maxSize); 108 } 109 buffer = new byte[bufferSize]; 110 fill(); 111 } 112 113 117 public int available() { 118 return bufferLength - bufferOffset; 119 } 120 121 124 public void close() throws IOException { 125 inputStream.close(); 126 } 127 128 131 public void mark(int position) { 132 inputStream.mark(position); 133 } 134 135 139 public boolean markSupported() { 140 return inputStream.markSupported(); 141 } 142 143 146 public boolean maxLengthMet() { 147 return maxLengthMet; 148 } 149 150 153 public boolean contentLengthMet() { 154 return contentLengthMet; 155 } 156 157 162 public int read() throws IOException { 163 164 if (maxLengthMet) { 165 throw new MaxLengthExceededException(maxSize); 166 } 167 if (contentLengthMet) { 168 throw new ContentLengthExceededException(contentLength); 169 } 170 if (buffer == null) { 171 return -1; 172 } 173 174 if (bufferOffset < bufferLength) { 175 return (int)(char) buffer[bufferOffset++]; 176 } 177 fill(); 178 return read(); 179 } 180 181 185 public int read(byte[] b) throws IOException { 186 return read(b, 0, b.length); 187 } 188 189 193 public int read(byte[] b, int offset, int length) throws IOException { 194 195 int count = 0; 196 int read = read(); 197 if (read == -1) { 198 return -1; 199 } 200 201 while ((read != -1) && (count < length)) { 202 b[offset] = (byte) read; 203 read = read(); 204 count++; 205 offset++; 206 } 207 return count; 208 } 209 210 215 public int readLine(byte[] b, int offset, int length) throws IOException { 216 217 int count = 0; 218 int read = read(); 219 if (read == -1) { 220 return -1; 221 } 222 223 while ((read != -1) && (count < length)) { 224 if (read == '\n') 225 break; 226 b[offset] = (byte) read; 227 count++; 228 offset++; 229 read = read(); 230 } 231 return count; 232 } 233 234 238 public byte[] readLine() throws IOException { 239 240 int read = read(); 241 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 242 243 if( -1 == read ) 245 return null; 246 247 while ((read != -1) && (read != '\n')) { 248 baos.write(read); 249 read = read(); 250 } 251 252 return baos.toByteArray(); 253 } 254 255 259 public void reset() throws IOException { 260 inputStream.reset(); 261 } 262 263 268 protected void fill() throws IOException { 269 270 if ((bufferOffset > -1) && (bufferLength > -1)) { 271 int length = Math.min(bufferSize, (((int) contentLength+1) - totalLength)); 272 if (length == 0) { 273 contentLengthMet = true; 274 } 275 if ((maxSize > -1) && (length > 0)){ 276 length = Math.min(length, ((int) maxSize - totalLength)); 277 if (length == 0) { 278 maxLengthMet = true; 279 } 280 } 281 282 int bytesRead = -1; 283 if (length > 0) { 284 bytesRead = inputStream.read(buffer, 0, length); 285 } 286 if (bytesRead == -1) { 287 buffer = null; 288 bufferOffset = -1; 289 bufferLength = -1; 290 } 291 else { 292 bufferLength = bytesRead; 293 totalLength += bytesRead; 294 bufferOffset = 0; 295 } 296 } 297 } 298 } | Popular Tags |