01: /****************************************************************
02: * Licensed to the Apache Software Foundation (ASF) under one *
03: * or more contributor license agreements. See the NOTICE file *
04: * distributed with this work for additional information *
05: * regarding copyright ownership. The ASF licenses this file *
06: * to you under the Apache License, Version 2.0 (the *
07: * "License"); you may not use this file except in compliance *
08: * with the License. You may obtain a copy of the License at *
09: * *
10: * http://www.apache.org/licenses/LICENSE-2.0 *
11: * *
12: * Unless required by applicable law or agreed to in writing, *
13: * software distributed under the License is distributed on an *
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15: * KIND, either express or implied. See the License for the *
16: * specific language governing permissions and limitations *
17: * under the License. *
18: ****************************************************************/package org.apache.james.smtpserver;
19:
20: import java.io.IOException;
21: import java.io.InputStream;
22:
23: /**
24: * Wraps an underlying input stream, limiting the allowable size
25: * of incoming data. The size limit is configured in the conf file,
26: * and when the limit is reached, a MessageSizeException is thrown.
27: */
28: public class SizeLimitedInputStream extends InputStream {
29: /**
30: * Maximum number of bytes to read.
31: */
32: private long maxmessagesize = 0;
33: /**
34: * Running total of bytes read from wrapped stream.
35: */
36: private long bytesread = 0;
37:
38: /**
39: * InputStream that will be wrapped.
40: */
41: private InputStream in = null;
42:
43: /**
44: * Constructor for the stream. Wraps an underlying stream.
45: * @param in InputStream to use as basis for new Stream.
46: * @param maxmessagesize Message size limit, in Kilobytes
47: */
48: public SizeLimitedInputStream(InputStream in, long maxmessagesize) {
49: this .in = in;
50: this .maxmessagesize = maxmessagesize;
51: }
52:
53: /**
54: * Overrides the read method of InputStream to call the read() method of the
55: * wrapped input stream.
56: * @throws IOException Throws a MessageSizeException, which is a sub-type of IOException
57: * @return Returns the number of bytes read.
58: */
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: /**
72: * Overrides the read method of InputStream to call the read() method of the
73: * wrapped input stream.
74: * @throws IOException Throws a MessageSizeException, which is a sub-type of IOException.
75: * @return Returns the int character value of the byte read.
76: */
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: }
|