java.io

Class BufferedInputStream

Implemented Interfaces:
Closeable
Known Direct Subclasses:
MonitorInputStream

public class BufferedInputStream
extends FilterInputStream

A BufferedInputStream adds functionality to another input stream-namely, the ability to buffer the input and to support the mark and reset methods. When the BufferedInputStream is created, an internal buffer array is created. As bytes from the stream are read or skipped, the internal buffer is refilled as necessary from the contained input stream, many bytes at a time. The mark operation remembers a point in the input stream and the reset operation causes all the bytes read since the most recent mark operation to be reread before new bytes are taken from the contained input stream.
Since:
JDK1.0

Field Summary

protected byte[]
buf
The internal buffer array where the data is stored.
protected int
count
The index one greater than the index of the last valid byte in the buffer.
protected int
marklimit
The maximum read ahead allowed after a call to the mark method before subsequent calls to the reset method fail.
protected int
markpos
The value of the pos field at the time the last mark method was called.
protected int
pos
The current position in the buffer.

Fields inherited from class java.io.FilterInputStream

in

Constructor Summary

BufferedInputStream(InputStream in)
Creates a BufferedInputStream and saves its argument, the input stream in, for later use.
BufferedInputStream(InputStream in, int size)
Creates a BufferedInputStream with the specified buffer size, and saves its argument, the input stream in, for later use.

Method Summary

int
available()
Returns the number of bytes that can be read from this input stream without blocking.
void
close()
Closes this input stream and releases any system resources associated with the stream.
void
mark(int readlimit)
See the general contract of the mark method of InputStream.
boolean
markSupported()
Tests if this input stream supports the mark and reset methods.
int
read()
See the general contract of the read method of InputStream.
int
read(b[] , int off, int len)
Reads bytes from this byte-input stream into the specified byte array, starting at the given offset.
void
reset()
See the general contract of the reset method of InputStream.
long
skip(long n)
See the general contract of the skip method of InputStream.

Methods inherited from class java.io.FilterInputStream

available, close, mark, markSupported, read, read, read, reset, skip

Methods inherited from class java.io.InputStream

available, close, mark, markSupported, read, read, read, reset, skip

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Field Details

buf

protected byte[] buf
The internal buffer array where the data is stored. When necessary, it may be replaced by another array of a different size.

count

protected int count
The index one greater than the index of the last valid byte in the buffer. This value is always in the range 0 through buf.length; elements buf[0] through buf[count-1] contain buffered input data obtained from the underlying input stream.

marklimit

protected int marklimit
The maximum read ahead allowed after a call to the mark method before subsequent calls to the reset method fail. Whenever the difference between pos and markpos exceeds marklimit, then the mark may be dropped by setting markpos to -1.
See Also:
mark(int), reset()

markpos

protected int markpos
The value of the pos field at the time the last mark method was called.

This value is always in the range -1 through pos. If there is no marked position in the input stream, this field is -1. If there is a marked position in the input stream, then buf[markpos] is the first byte to be supplied as input after a reset operation. If markpos is not -1, then all bytes from positions buf[markpos] through buf[pos-1] must remain in the buffer array (though they may be moved to another place in the buffer array, with suitable adjustments to the values of count, pos, and markpos); they may not be discarded unless and until the difference between pos and markpos exceeds marklimit.

See Also:
mark(int), pos

pos

protected int pos
The current position in the buffer. This is the index of the next character to be read from the buf array.

This value is always in the range 0 through count. If it is less than count, then buf[pos] is the next byte to be supplied as input; if it is equal to count, then the next read or skip operation will require more bytes to be read from the contained input stream.

See Also:
buf

Constructor Details

BufferedInputStream

public BufferedInputStream(InputStream in)
Creates a BufferedInputStream and saves its argument, the input stream in, for later use. An internal buffer array is created and stored in buf.
Parameters:
in - the underlying input stream.
Usages and Demos :

View More Examples of BufferedInputStream(InputStream in)
   1:     {
   2:         file = new DataInputStream(new BufferedInputStream((url.openConnection()).getInputStream()));
   3:     }
   4: 
   5:     public PCBinaryInputStream(InputStream input) throws IOException

View Full Code Here
   1:        try {
   2:            this.image = ImageIO.read(new BufferedInputStream(GhostPictureAdapter.class.getResourceAsStream(picture)));
   3:        } catch (MalformedURLException mue) {
   4:            throw new IllegalStateException("Invalid picture URL.");
   5:        } catch (IOException ioe) {

View Full Code Here
   1:                 } else if (input instanceof IStorageEditorInput) {
   2:                     InputStream is = new BufferedInputStream(((IStorageEditorInput) input).getStorage().getContents());
   3:                     model =  new TargetModel();
   4:                     model.load(is, false);
   5:                 }

View Full Code Here
   1:                 } else if (input instanceof IStorageEditorInput) {
   2:                     InputStream is = new BufferedInputStream(((IStorageEditorInput) input).getStorage()
   3:                             .getContents());
   4:                     model =  new ProductModel();
   5:                     model.load(is, false);

View Full Code Here
   1:             return(inputStream);
   2:         return(new BufferedInputStream(MiSystem.getIOManager().getInputResourceAsStream(filename)));
   3:         }
   4:     public        OutputStream        getOutputStream() throws Exception
   5:         {

View Full Code Here

BufferedInputStream

public BufferedInputStream(InputStream in,
                           int size)
Creates a BufferedInputStream with the specified buffer size, and saves its argument, the input stream in, for later use. An internal buffer array of length size is created and stored in buf.
Parameters:
in - the underlying input stream.
size - the buffer size.
Throws:
IllegalArgumentException - if size <= 0.
Usages and Demos :

View More Examples of BufferedInputStream(InputStream in,int size)
   1:     if (!(input instanceof BufferedInputStream)) {
   2:         input = new BufferedInputStream(input, 80);
   3:     }
   4:     }

View Full Code Here
   1:         buffer = new byte[pattern.length];
   2:         input = new BufferedInputStream(in, pattern.length + 6);
   3:         try
   4:         {
   5:             readUntilBoundary();

View Full Code Here
   1:         }
   2:         in = new BufferedInputStream(in, bufferSize);
   3:         decoder.setInput(in, "ISO-8859-1");
   4:         long start = 0;
   5:         Object token = null;

View Full Code Here
   1:         case COMPRESS_NONE:
   2:             uncompress_in = buf_in = new BufferedInputStream(hd1_in,4096);
   3:             break;
   4:         default:
   5:         case COMPRESS_GZIP:

View Full Code Here

Method Details

available

public int available()
            throws IOException
Returns the number of bytes that can be read from this input stream without blocking.

The available method of BufferedInputStream returns the sum of the number of bytes remaining to be read in the buffer (count - pos) and the result of calling the available method of the underlying input stream.

Overrides:
available in interface FilterInputStream
Returns:
the number of bytes that can be read from this input stream without blocking.
Throws:
IOException - if an I/O error occurs.
Usages and Demos :

View More Examples of available()
   1:       FileOutputStream out;
   2:       BufferedInputStream bin;
   3:       BufferedOutputStream bout;
   4:         ...
   5:          in   = new FileInputStream( origFile );
   6:          bin  = new BufferedInputStream( in );
   7:          out  = new FileOutputStream( fileCopy );
   8:         ...
   9: 
  10:          while( bin.available() > 0 ) {
  11:             bytesRead = bin.read( bArray );

View Full Code Here
   1:       FileOutputStream out;
   2:       BufferedInputStream bin;
   3:       BufferedOutputStream bout;
   4:         ...
   5:          in   = new FileInputStream( origFile );
   6:          bin  = new BufferedInputStream( in );
   7:          out  = new FileOutputStream( fileCopy );
   8:         ...
   9: 
  10:          while( bin.available() > 0 ) {
  11:             bytesRead = bin.read( bArray );

View Full Code Here
   1: 
   2:             BufferedInputStream bufin = new BufferedInputStream(in);
   3:             
   4:         ...
   5:             {
   6:                 System.out.println("BufferedInputStream  support 'mark' method !!!");
   7:                 
   8:         ...
   9:                 int limit=bufin.available();
  10:                 System.out.println("limit is "+limit);

View Full Code Here
   1:             FileInputStream keyfis = new FileInputStream(args[0]);
   2:             byte[] encKey = new byte[keyfis.available()];  
   3:             keyfis.read(encKey);
   4:         ...
   5:             FileInputStream sigfis = new FileInputStream(args[1]);
   6:             byte[] sigToVerify = new byte[sigfis.available()]; 
   7:             sigfis.read(sigToVerify );
   8:         ...
   9:             FileInputStream datafis = new FileInputStream(args[2]);
  10:             BufferedInputStream bufin = new BufferedInputStream(datafis);
  11: 
  12:         ...
  13:             int len;
  14:             while (bufin.available() != 0) {

View Full Code Here
   1:             FileInputStream fis = new FileInputStream(args[0]);
   2:             BufferedInputStream bufin = new BufferedInputStream(fis);
   3:             byte[] buffer = new byte[1024];
   4:         ...
   5:             int len;
   6:             while (bufin.available() != 0) {
   7:                 len = bufin.read(buffer);
   8:                 dsa.update(buffer, 0, len);
   9:                 };

View Full Code Here

close

public void close()
            throws IOException
Closes this input stream and releases any system resources associated with the stream.
Specified by:
close in interface Closeable
Overrides:
close in interface FilterInputStream
Throws:
IOException - if an I/O error occurs.
Usages and Demos :

View More Examples of close()
   1:         String toFileName = args[1];
   2:         BufferedInputStream in = new BufferedInputStream(
   3:             new FileInputStream( fromFileName ) );
   4:         ...
   5:             out.write( buff, 0, len );
   6:         in.close();
   7:         ...
   8:         out.close();
   9:     }

View Full Code Here
   1:   public static byte[] read(File bFile) throws IOException{
   2:     BufferedInputStream bf = new BufferedInputStream(
   3:       new FileInputStream(bFile));
   4:         ...
   5:     } finally {
   6:       bf.close();
   7:     }
   8:   }
   9:   public static byte[]

View Full Code Here
   1: 
   2: import java.io.BufferedInputStream;
   3: import java.io.ByteArrayOutputStream;
   4:         ...
   5:     protected String getContentsFromStream(InputStream is) throws IOException {
   6:         BufferedInputStream bis = new BufferedInputStream(is);
   7:         ByteArrayOutputStream bos = new ByteArrayOutputStream();
   8:         ...
   9:         String contents = bos.toString();
  10:         bis.close();
  11:         ...
  12:         bos.close();

View Full Code Here
   1:       FileOutputStream out;
   2:       BufferedInputStream bin;
   3:       BufferedOutputStream bout;
   4:         ...
   5:          in   = new FileInputStream( origFile );
   6:          bin  = new BufferedInputStream( in );
   7:          out  = new FileOutputStream( fileCopy );
   8:         ...
   9: 
  10:          bin.close();
  11:         ...
  12:          bout.close();

View Full Code Here
   1:       FileOutputStream out;
   2:       BufferedInputStream bin;
   3:       BufferedOutputStream bout;
   4:         ...
   5:          in   = new FileInputStream( origFile );
   6:          bin  = new BufferedInputStream( in );
   7:          out  = new FileOutputStream( fileCopy );
   8:         ...
   9: 
  10:          bin.close();
  11:         ...
  12:          bout.close();

View Full Code Here

mark

public void mark(int readlimit)
See the general contract of the mark method of InputStream.
Overrides:
mark in interface FilterInputStream
Parameters:
readlimit - the maximum limit of bytes that can be read before the mark position becomes invalid.
See Also:
reset()
Usages and Demos :

View More Examples of mark(int readlimit)
   1:             
   2:             BufferedInputStream bufin = new BufferedInputStream(in, 1024);
   3:             
   4:         ...
   5: 
   6:             bufin.mark(18);
   7:             bufin.skip(half);        
   8: 
   9:         System.out.println("tail half:");

View Full Code Here
   1:     for (;;) {
   2:         br.mark(READLEN);
   3:         int charsRead = 0;
   4:         ...
   5: 
   6:         BufferedInputStream b =
   7:         ...
   8:       new BufferedInputStream(new StringBufferInputStream("ABCDEF"));
   9:         ...
  10:         b.mark(3);

View Full Code Here
   1: 
   2:             BufferedInputStream in=new BufferedInputStream(socket.getInputStream());
   3:            
   4:         ...
   5:                 
   6:                 in.mark(2);
   7:             
   8:                 if(in.read() == -1)
   9:                 {

View Full Code Here
   1:     public X509Certificate readCertificate(String cerfile) throws Exception {
   2:         BufferedInputStream bis = new BufferedInputStream(new FileInputStream(cerfile));
   3:         CertificateFactory cf = CertificateFactory.getInstance("X.509");
   4:         ...
   5:         X509Certificate cert = null;
   6:         bis.mark(1024);
   7:         try {
   8:             CertPath cp;
   9:             try {

View Full Code Here
   1: import java.io.FileInputStream;
   2: import java.io.BufferedInputStream;
   3: import org.jstk.cert.rep.FileBasedRepository;
   4:         ...
   5:             StringBuffer sb = new StringBuffer();
   6:             BufferedInputStream bis = new BufferedInputStream(new FileInputStream(cerfile));
   7:         ...
   8:             bis.mark(1024);
   9:             try {

View Full Code Here

markSupported

public boolean markSupported()
Tests if this input stream supports the mark and reset methods. The markSupported method of BufferedInputStream returns true.
Overrides:
markSupported in interface FilterInputStream
Returns:
a boolean indicating if this stream type supports the mark and reset methods.

read

public int read()
            throws IOException
See the general contract of the read method of InputStream.
Overrides:
read in interface FilterInputStream
Returns:
the next byte of data, or -1 if the end of the stream is reached.
Throws:
IOException - if an I/O error occurs.
Usages and Demos :

View More Examples of read()
   1: 
   2:             BufferedInputStream bufin = new BufferedInputStream(in);
   3:             
   4:         ...
   5:             {
   6:                 System.out.println("BufferedInputStream  support 'mark' method !!!");
   7:                 
   8:         ...
   9:                 for (int i = 0; i < limit; i++)
  10:                     System.out.print((char)(bufin.read()));
  11:                 
  12:         ...
  13:             int c;
  14:             while ((c=bufin.read()) >= 0)   

View Full Code Here
   1: 
   2: import java.io.BufferedInputStream;
   3: import java.io.FilterInputStream;
   4:         ...
   5:     
   6:     public NPECatchingInputStream (final BufferedInputStream in) {
   7:         super(in);
   8:         ...
   9:         try {
  10:             return in.read();
  11:         } catch(NullPointerException npe) {

View Full Code Here
   1: 
   2:         BufferedInputStream b =
   3:         ...
   4:       new BufferedInputStream(new StringBufferInputStream("ABCDEF"));
   5:         b.mark(3);
   6:         ...
   7:         b.read();
   8:         ...
   9:         b.read();

View Full Code Here
   1:             scene=targetscene;
   2:             BufferedInputStream in=new BufferedInputStream(inStream);
   3:             try
   4:         ...
   5:             byte inByte;
   6:             while ((inByte=(byte)in.read())!=0) result+=(char)inByte;
   7:             return result;
   8:         ...
   9:         {
  10:             return in.read()|(in.read()<<8)|(in.read()<<16)|(in.read()<<24);
  11:         }
  12:         ...
  13:         {
  14:             return (in.read()|(in.read()<<8));

View Full Code Here
   1: 
   2: public class BufferedInputStreamTest extends BufferedInputStream
   3: {
   4:         ...
   5: {
   6:   BufferedInputStream bis = new BufferedInputStream(ins, 15);
   7: 
   8:         ...
   9:   bis.mark(75);
  10:   bis.read();
  11:   bis.read(buf);
  12:         ...
  13: 
  14:       bist.read();

View Full Code Here

read

public int read(b[] ,
                int off,
                int len)
            throws IOException
Reads bytes from this byte-input stream into the specified byte array, starting at the given offset.

This method implements the general contract of the corresponding read method of the InputStream class. As an additional convenience, it attempts to read as many bytes as possible by repeatedly invoking the read method of the underlying stream. This iterated read continues until one of the following conditions becomes true:

  • The specified number of bytes have been read,
  • The read method of the underlying stream returns -1, indicating end-of-file, or
  • The available method of the underlying stream returns zero, indicating that further input requests would block.
If the first read on the underlying stream returns -1 to indicate end-of-file then this method returns -1. Otherwise this method returns the number of bytes actually read.

Subclasses of this class are encouraged, but not required, to attempt to read as many bytes as possible in the same fashion.

Overrides:
read in interface FilterInputStream
Parameters:
off - offset at which to start storing bytes.
len - maximum number of bytes to read.
Returns:
the number of bytes read, or -1 if the end of the stream has been reached.
Throws:
IOException - if an I/O error occurs.
Usages and Demos :

View More Examples of read(b[] ,int off,int len)
   1: 
   2: import java.io.BufferedInputStream;
   3: import java.io.FilterInputStream;
   4:         ...
   5:     
   6:     public NPECatchingInputStream (final BufferedInputStream in) {
   7:         super(in);
   8:         ...
   9:         try {
  10:             return in.read(b, off, len);
  11:         } catch(NullPointerException npe) {

View Full Code Here
   1:       FileInputStream fis = new FileInputStream(file);
   2:       BufferedInputStream bis = new BufferedInputStream(fis);
   3: 
   4:         ...
   5: 
   6:       bis.read(data, 0, size);
   7:       bis.close();
   8: 
   9:       return defineClass(name, data, 0, data.length);

View Full Code Here
   1:     protected BufferedOutputStream out = null;
   2:     protected BufferedInputStream in = null;
   3:     protected Socket sc = null;
   4:         ...
   5:             out = new BufferedOutputStream(sc.getOutputStream());
   6:             in = new BufferedInputStream(sc.getInputStream());
   7:         } catch (IOException e) {
   8:         ...
   9:             out = new BufferedOutputStream(sc.getOutputStream());
  10:             in = new BufferedInputStream(sc.getInputStream());
  11:             this.epid = (HostPortEpid)OrbAccessor.getEpid();
  12:         ...
  13:             buf = new byte[bufSize];
  14:             in.read(buf, 0, bufSize);

View Full Code Here
   1:             FileInputStream fis = new FileInputStream (fileName);
   2:             BufferedInputStream bis = new BufferedInputStream (fis);
   3: 
   4:         ...
   5:             content = new byte[fileSize];
   6:             bis.read(content, 0, fileSize);
   7: 
   8:         ...
   9:             FileInputStream fis = new FileInputStream (fileName);
  10:             BufferedInputStream bis = new BufferedInputStream (fis);
  11: 
  12:         ...
  13:             byte[] content = new byte[fileSize];
  14:             bis.read(content, 0, fileSize);

View Full Code Here
   1:   private Vector hdrVec = new Vector();
   2:   private BufferedInputStream bufferedIn;
   3: 
   4:         ...
   5:   {
   6:     bufferedIn = new BufferedInputStream(sock.getInputStream());
   7: 
   8:         ...
   9:           {
  10:             bufferedIn.read(ch, 0, 1);
  11:         if (ch[0] == '\n')

View Full Code Here

reset

public void reset()
            throws IOException
See the general contract of the reset method of InputStream.

If markpos is -1 (no mark has been set or the mark has been invalidated), an IOException is thrown. Otherwise, pos is set equal to markpos.

Overrides:
reset in interface FilterInputStream
Throws:
IOException - if this stream has not been marked or if the mark has been invalidated.
See Also:
mark(int)
Usages and Demos :

View More Examples of reset()
   1:             
   2:             BufferedInputStream bufin = new BufferedInputStream(in, 1024);
   3:             
   4:         ...
   5:                     
   6:         bufin.reset();
   7:         System.out.println();
   8:         System.out.println("total:");
   9:         int c;

View Full Code Here
   1: 
   2:             BufferedInputStream bufin = new BufferedInputStream(in);
   3:             
   4:         ...
   5:             {
   6:                 System.out.println("BufferedInputStream  support 'mark' method !!!");
   7:                 
   8:         ...
   9:                 
  10:                 bufin.reset();
  11:             }

View Full Code Here
   1: import java.io.FileInputStream;
   2: import java.io.BufferedInputStream;
   3: 
   4:         ...
   5:         File file = new File(certpathfile);
   6:         BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file), bufsize);
   7:         bis.mark(bufsize);
   8:         ...
   9:         } catch (CertificateException ce) {
  10:             bis.reset();
  11:             try {

View Full Code Here
   1:         System.out.println ("read " + new String(buf));
   2:         br.reset();
   3:     }
   4:         ...
   5: 
   6:         BufferedInputStream b =
   7:         ...
   8:       new BufferedInputStream(new StringBufferInputStream("ABCDEF"));
   9:         b.mark(3);
  10:         ...
  11:         b.read();
  12:         b.reset();

View Full Code Here
   1: 
   2:             BufferedInputStream in=new BufferedInputStream(socket.getInputStream());
   3:            
   4:         ...
   5:                 }
   6:                 in.reset();
   7:                 Message ToProcess = new Message();  
   8:                 outputLine = ToProcess.processInput(in,out);
   9:                 try

View Full Code Here

skip

public long skip(long n)
            throws IOException
See the general contract of the skip method of InputStream.
Overrides:
skip in interface FilterInputStream
Parameters:
n - the number of bytes to be skipped.
Returns:
the actual number of bytes skipped.
Throws:
IOException - if an I/O error occurs.
Usages and Demos :

View More Examples of skip(long n)
   1:             
   2:             BufferedInputStream bufin = new BufferedInputStream(in, 1024);
   3:             
   4:         ...
   5:             bufin.mark(18);
   6:             bufin.skip(half);        
   7: 
   8:         System.out.println("tail half:");
   9:             byte[] buf = new byte[1024];

View Full Code Here
   1: 
   2: import java.io.BufferedInputStream;
   3: import java.io.FilterInputStream;
   4:         ...
   5:     
   6:     public NPECatchingInputStream (final BufferedInputStream in) {
   7:         super(in);
   8:         ...
   9:         try {
  10:             return in.skip(n);
  11:         } catch(NullPointerException npe) {

View Full Code Here
   1: 
   2: import java.io.BufferedInputStream;
   3: import java.io.BufferedOutputStream;
   4:         ...
   5:             if (parent.getResumePosition() > 0)
   6:                 dataStream.skip(parent.getResumePosition());
   7: 
   8:         ...
   9:         BufferedOutputStream writer = null;
  10:         BufferedInputStream dataStream = null;
  11:         int byt = -1;
  12:         ...
  13: 
  14:         dataStream = new BufferedInputStream(parent.getCurrentObject()

View Full Code Here
   1: import java.io.InputStream;
   2: import java.io.BufferedInputStream;
   3: import java.io.IOException;
   4:         ...
   5: 
   6:   private BufferedInputStream br;
   7:   private int m_index, m_end, m_bufsize;
   8:         ...
   9:   RECharIndexedInputStream(InputStream str, int index) {
  10:     if (str instanceof BufferedInputStream) br = (BufferedInputStream) str;
  11:     else br = new BufferedInputStream(str,BUFFER_INCREMENT);
  12:         ...
  13:     br.mark(m_bufsize);
  14:     br.skip(index-1);

View Full Code Here
   1: package gnu.regexp;
   2: import java.io.BufferedInputStream;
   3: import java.io.IOException;
   4:         ...
   5:     
   6:     private BufferedInputStream br;
   7: 
   8:         ...
   9:     CharIndexedInputStream(InputStream str, int index) {
  10:         if (str instanceof BufferedInputStream) br = (BufferedInputStream) str;
  11:         else br = new BufferedInputStream(str,BUFFER_INCREMENT);
  12:         ...
  13:                 br.mark(bufsize);
  14:                 br.skip(index-1);

View Full Code Here