classpathx-javamail
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Classpathx-javamail] gnu.mail.util.ReadFilterInputStream.java


From: Doug Porter
Subject: [Classpathx-javamail] gnu.mail.util.ReadFilterInputStream.java
Date: Fri, 19 Sep 2003 03:28:35 -0800

/*
 * ReadFilterInputStream.java
 * Copyright (C) 2003 Doug Porter
 * 
 * This file is part of GNU JavaMail, a library.
 * 
 * GNU JavaMail is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * GNU JavaMail is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 
 USA
 *
 * As a special exception, if you link this library with other files to
 * produce an executable, this library does not by itself cause the
 * resulting executable to be covered by the GNU General Public License.
 * This exception does not however invalidate any other reasons why the
 * executable file might be covered by the GNU General Public License.
 */

package gnu.mail.util;

import java.io.BufferedInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.mail.Session;

/**
 * An filter input stream that invokes read() from read(byte[]) and
 read(byte[],int,int).
 * <p>
 * This is different from FilterInputStream in that FilterInputStream
 invokes
 * in.read(byte[]) from read(byte[]), and in.read(byte[],int,int) from
 read(byte[],int,int).
 * In turn in.read(byte[])  calls in.read(). 
 * This means that FilterInputStream never calls the read() method of
 subclasses.
 * This class allows subclasses to override just the read() method and
 have other
 * methods in superclasses eventually use that subclass's read() method.
 * 
 * @author Doug Porter
 */
public class ReadFilterInputStream
extends FilterInputStream
{
    /**
    * Constructor.
    * @param in the inner input stream
    */
    public ReadFilterInputStream (InputStream in) {
        super (in);
    }
    
    /**
     * Reads from this stream into the specified byte array.
     * @param b byte array to read into
     * @return The number of bytes read, or -1 on dot line or end of
     stream
     */
    public int read (byte[] b)
    throws IOException
    {
        return read (b, 0, b.length);
    }
    
    /**
     * Reads from this stream into the specified byte array, at the
     offset and 
     * for the length specified.
     * <p>
     * On end of stream, returns -1.
     *
     * @param b byte array to read into
     * @param off offset in byte array at which to begin reading
     * @param len maximum number of bytes to read
     * @return The number of bytes read, or -1 on dot line or end of
     stream
     */
    public int read (byte[] b, 
                     int off, 
                     int len)
    throws IOException
    {
        final int EOS = -1;
        int i = 0;
        int n;
        while (i < len &&
               (n = read ()) >= 0) {
                   
            b [off + i] = (byte) n;
            ++ i;
            
        }
        
        int result;
        if (i > 0) {
            
            result = i;
            
        }
        else {
                
            result = EOS;
            
        }
        
        return result;
    }
}

-- 
  Doug Porter
  address@hidden

-- 
http://www.fastmail.fm - A no graphics, no pop-ups email service




reply via email to

[Prev in Thread] Current Thread [Next in Thread]