BufferedInputStream constructor

BufferedInputStream(
  1. InputStream input, {
  2. int bufferSize = _defaultBufferSize,
})

Creates a BufferedInputStream and saves its argument, the input stream input, for later use.

Parameters

  • input: The underlying input stream
  • bufferSize: The buffer size (default: 8192 bytes)

Example

final buffered = BufferedInputStream(FileInputStream('data.bin'));
final largeBuffered = BufferedInputStream(
  FileInputStream('huge_file.bin'), 
  bufferSize: 65536
);

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.

Example Usage

// Basic buffered reading
final buffered = BufferedInputStream(FileInputStream('large_file.bin'));
try {
  final buffer = Uint8List(1024);
  int bytesRead;
  while ((bytesRead = await buffered.read(buffer)) != -1) {
    processData(buffer.sublist(0, bytesRead));
  }
} finally {
  await buffered.close();
}

// Using mark and reset
final buffered = BufferedInputStream(FileInputStream('data.bin'));
try {
  buffered.mark(1024); // Mark current position
  
  final header = await buffered.readFully(10);
  if (!isValidHeader(header)) {
    await buffered.reset(); // Go back to marked position
    processAsRawData(buffered);
  } else {
    processStructuredData(buffered);
  }
} finally {
  await buffered.close();
}

Implementation

BufferedInputStream(InputStream input, {int bufferSize = _defaultBufferSize})
    : _input = input,
      _buffer = Uint8List(bufferSize);