1 17 18 package org.apache.james.smtpserver; 19 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 23 28 public class SizeLimitedInputStream extends InputStream { 29 32 private long maxmessagesize = 0; 33 36 private long bytesread = 0; 37 38 41 private InputStream in = null; 42 43 48 public SizeLimitedInputStream(InputStream in, long maxmessagesize) { 49 this.in = in; 50 this.maxmessagesize = maxmessagesize; 51 } 52 53 59 public int read(byte[] b, int off, int len) throws IOException { 60 int l = in.read(b, off, len); 61 62 bytesread += l; 63 64 if (maxmessagesize > 0 && bytesread > maxmessagesize) { 65 throw new MessageSizeException(); 66 } 67 68 return l; 69 } 70 71 77 public int read() throws IOException { 78 if (maxmessagesize > 0 && bytesread <= maxmessagesize) { 79 bytesread++; 80 return in.read(); 81 } else { 82 throw new MessageSizeException(); 83 } 84 } 85 } 86 | Popular Tags |