001 /****************************************************************
002 * Licensed to the Apache Software Foundation (ASF) under one *
003 * or more contributor license agreements. See the NOTICE file *
004 * distributed with this work for additional information *
005 * regarding copyright ownership. The ASF licenses this file *
006 * to you under the Apache License, Version 2.0 (the *
007 * "License"); you may not use this file except in compliance *
008 * with the License. You may obtain a copy of the License at *
009 * *
010 * http://www.apache.org/licenses/LICENSE-2.0 *
011 * *
012 * Unless required by applicable law or agreed to in writing, *
013 * software distributed under the License is distributed on an *
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
015 * KIND, either express or implied. See the License for the *
016 * specific language governing permissions and limitations *
017 * under the License. *
018 ****************************************************************/
019
020 package com.hs.mail.io;
021
022 import java.io.IOException;
023 import java.io.InputStream;
024
025 /**
026 * Wraps an underlying input stream, limiting the allowable size
027 * of incoming data. The size limit is configured in the conf file,
028 * and when the limit is reached, a MessageSizeException is thrown.
029 */
030 public class SizeLimitedInputStream extends InputStream {
031 /**
032 * Maximum number of bytes to read.
033 */
034 private long maxmessagesize = 0;
035 /**
036 * Running total of bytes read from wrapped stream.
037 */
038 private long bytesread = 0;
039
040 /**
041 * InputStream that will be wrapped.
042 */
043 private InputStream in = null;
044
045 /**
046 * Constructor for the stream. Wraps an underlying stream.
047 * @param in InputStream to use as basis for new Stream.
048 * @param maxmessagesize Message size limit, in Kilobytes
049 */
050 public SizeLimitedInputStream(InputStream in, long maxmessagesize) {
051 this.in = in;
052 this.maxmessagesize = maxmessagesize;
053 }
054
055 /**
056 * Overrides the read method of InputStream to call the read() method of the
057 * wrapped input stream.
058 * @throws IOException Throws a MessageSizeException, which is a sub-type of IOException
059 * @return Returns the number of bytes read.
060 */
061 public int read(byte[] b, int off, int len) throws IOException {
062 int l = in.read(b, off, len);
063
064 bytesread += l;
065
066 if (maxmessagesize > 0 && bytesread > maxmessagesize) {
067 throw new MessageSizeException();
068 }
069
070 return l;
071 }
072
073 /**
074 * Overrides the read method of InputStream to call the read() method of the
075 * wrapped input stream.
076 * @throws IOException Throws a MessageSizeException, which is a sub-type of IOException.
077 * @return Returns the int character value of the byte read.
078 */
079 public int read() throws IOException {
080 if (maxmessagesize > 0 && bytesread <= maxmessagesize) {
081 bytesread++;
082 return in.read();
083 } else {
084 throw new MessageSizeException();
085 }
086 }
087 }