sizelimitedinputstream.java

来自「java 开发的邮件服务器平台。支持以下协议。 协议可以修改为自己的专门标识」· Java 代码 · 共 86 行

JAVA
86
字号
/*********************************************************************** * Copyright (c) 2000-2004 The Apache Software Foundation.             * * All rights reserved.                                                * * ------------------------------------------------------------------- * * Licensed under the Apache License, Version 2.0 (the "License"); you * * may not use this file except in compliance with the License. You    * * may obtain a copy of the License at:                                * *                                                                     * *     http://www.apache.org/licenses/LICENSE-2.0                      * *                                                                     * * Unless required by applicable law or agreed to in writing, software * * distributed under the License is distributed on an "AS IS" BASIS,   * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or     * * implied.  See the License for the specific language governing       * * permissions and limitations under the License.                      * ***********************************************************************/package org.apache.james.smtpserver;import java.io.IOException;import java.io.InputStream;/**   * Wraps an underlying input stream, limiting the allowable size  * of incoming data. The size limit is configured in the conf file,  * and when the limit is reached, a MessageSizeException is thrown.  */public class SizeLimitedInputStream extends InputStream {    /**     * Maximum number of bytes to read.     */    private long maxmessagesize = 0;    /**     * Running total of bytes read from wrapped stream.     */    private long bytesread = 0;    /**     * InputStream that will be wrapped.     */    private InputStream in = null;    /**     * Constructor for the stream. Wraps an underlying stream.     * @param in InputStream to use as basis for new Stream.     * @param maxmessagesize Message size limit, in Kilobytes     */    public SizeLimitedInputStream(InputStream in, long maxmessagesize) {        this.in = in;        this.maxmessagesize = maxmessagesize;    }    /**     * Overrides the read method of InputStream to call the read() method of the     * wrapped input stream.     * @throws IOException Throws a MessageSizeException, which is a sub-type of IOException     * @return Returns the number of bytes read.     */    public int read(byte[] b, int off, int len) throws IOException {        int l = in.read(b, off, len);        bytesread += l;        if (maxmessagesize > 0 && bytesread > maxmessagesize) {            throw new MessageSizeException();        }        return l;    }    /**     * Overrides the read method of InputStream to call the read() method of the     * wrapped input stream.     * @throws IOException Throws a MessageSizeException, which is a sub-type of IOException.     * @return Returns the int character value of the byte read.     */    public int read() throws IOException {        if (maxmessagesize > 0 && bytesread <= maxmessagesize) {            bytesread++;            return in.read();        } else {            throw new MessageSizeException();        }    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?