1 5 package com.oreilly.servlet.multipart; 6 7 import java.io.IOException ; 8 9 import javax.servlet.ServletInputStream ; 10 11 22 public class LimitedServletInputStream extends ServletInputStream { 23 24 25 private ServletInputStream in; 26 27 28 private int totalExpected; 29 30 31 private int totalRead = 0; 32 33 37 public LimitedServletInputStream(ServletInputStream in, int totalExpected) { 38 this.in = in; 39 this.totalExpected = totalExpected; 40 } 41 42 55 public int readLine(byte b[], int off, int len) throws IOException { 56 int result, left = totalExpected - totalRead; 57 if (left <= 0) { 58 return -1; 59 } else { 60 result = ((ServletInputStream )in).readLine(b, off, Math.min(left, len)); 61 } 62 if (result > 0) { 63 totalRead += result; 64 } 65 return result; 66 } 67 68 76 public int read() throws IOException { 77 if (totalRead >= totalExpected) { 78 return -1; 79 } 80 81 int result = in.read(); 82 if (result != -1) { 83 totalRead++; 84 } 85 return result; 86 } 87 88 99 public int read( byte b[], int off, int len ) throws IOException { 100 int result, left = totalExpected - totalRead; 101 if (left <= 0) { 102 return -1; 103 } else { 104 result = in.read(b, off, Math.min(left, len)); 105 } 106 if (result > 0) { 107 totalRead += result; 108 } 109 return result; 110 } 111 } 112 | Popular Tags |