FileDocCategorySizeDatePackage
SharedFileInputStream.javaAPI DocBouncy Castle Crypto API 1.41 (Java 1.5)5214Wed Oct 01 10:55:30 BST 2008org.bouncycastle.mail.smime.util

SharedFileInputStream

public class SharedFileInputStream extends FilterInputStream implements SharedInputStream

Fields Summary
private final SharedFileInputStream
_parent
private final File
_file
private final long
_start
private final long
_length
private long
_position
private long
_markedPosition
private List
_subStreams
Constructors Summary
public SharedFileInputStream(String fileName)

    
     
          
         
    
        this(new File(fileName));
    
public SharedFileInputStream(File file)

        this(file, 0, file.length());
    
private SharedFileInputStream(File file, long start, long length)

        super(new BufferedInputStream(new FileInputStream(file)));

        _parent = null;
        _file = file;
        _start = start;
        _length = length;
        
        in.skip(start);
    
private SharedFileInputStream(SharedFileInputStream parent, long start, long length)

        super(new BufferedInputStream(new FileInputStream(parent._file)));

        _parent = parent;
        _file = parent._file;
        _start = start;
        _length = length;

        in.skip(start);
    
Methods Summary
public voiddispose()
Close of this stream and any substreams that have been created from it.

throws
IOException on problem closing the main stream.

        Iterator it = _subStreams.iterator();
        
        while (it.hasNext())
        {
            try
            {
                ((SharedFileInputStream)it.next()).dispose();
            }
            catch (IOException e)
            {
                // ignore
            }
        }

        in.close();
    
public longgetPosition()

        return _position;
    
public org.bouncycastle.mail.smime.util.SharedFileInputStreamgetRoot()
Return the shared stream that represents the top most stream that this stream inherits from.

return
the base of the shared stream tree.

        if (_parent != null)
        {
            return _parent.getRoot();
        }

        return this;
    
public voidmark(int readLimit)

        _markedPosition = _position;
        in.mark(readLimit);
    
public booleanmarkSupported()

        return true;
    
public java.io.InputStreamnewStream(long start, long finish)

        try
        {
            SharedFileInputStream stream;
            
            if (finish < 0)
            {
                if (_length > 0)
                {
                    stream = new SharedFileInputStream(this, _start + start, _length - start);
                }
                else if (_length == 0)
                {
                    stream = new SharedFileInputStream(this, _start + start, 0);
                }
                else
                {
                    stream = new SharedFileInputStream(this, _start + start, -1);
                }
            }
            else
            {
                stream = new SharedFileInputStream(this, _start + start, finish - start);
            }
            
            _subStreams.add(stream);
            
            return stream;
        }
        catch (IOException e)
        {
            throw new IllegalStateException("unable to create shared stream: " + e);
        }
    
public intread(byte[] buf)

        return this.read(buf, 0, buf.length);
    
public intread(byte[] buf, int off, int len)

        int count = 0;
        
        if (len == 0)
        {
            return 0;
        }
        
        while (count < len)
        {
            int ch = this.read();
            
            if (ch < 0)
            {
                break;
            }
            
            buf[off + count] = (byte)ch;
            count++;
        }
        
        if (count == 0)
        {
            return -1;  // EOF
        }
        
        return count;
    
public intread()

        if (_position == _length)
        {
            return -1;
        }

        _position++;
        return in.read();
    
public voidreset()

        _position = _markedPosition;
        in.reset();
    
public longskip(long n)

        long count;

        for (count = 0; count != n; count++)
        {
            if (this.read() < 0)
            {
                break;
            }
        }

        return count;