Package io.microsphere.io
Class FastByteArrayInputStream
- java.lang.Object
-
- java.io.InputStream
-
- java.io.ByteArrayInputStream
-
- io.microsphere.io.FastByteArrayInputStream
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public class FastByteArrayInputStream extends java.io.ByteArrayInputStreamA fast, non-thread-safe implementation ofByteArrayInputStream.This class extends the standard
ByteArrayInputStreamto provide optimized performance in single-threaded scenarios. Unlike the standard implementation, it does not perform synchronization, making it faster but unsafe for concurrent use.Example Usage
byte[] data = "Hello, World!".getBytes(StandardCharsets.UTF_8); FastByteArrayInputStream inputStream = new FastByteArrayInputStream(data); int c; while ((c = inputStream.read()) != -1) { System.out.print((char) c); } inputStream.close();For reading a subset of the byte array:
byte[] data = "Microservices are great!".getBytes(StandardCharsets.UTF_8); FastByteArrayInputStream inputStream = new FastByteArrayInputStream(data, 0, 11); // Read only first 11 bytes int c; while ((c = inputStream.read()) != -1) { System.out.print((char) c); } inputStream.close();- Since:
- 1.0.0
- Author:
- Mercy
- See Also:
ByteArrayInputStream
-
-
Constructor Summary
Constructors Constructor Description FastByteArrayInputStream(byte[] buf)Creates aFastByteArrayInputStreamso that it usesbufas its buffer array.FastByteArrayInputStream(byte[] buf, int offset, int length)CreatesFastByteArrayInputStreamthat usesbufas its buffer array.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intavailable()voidclose()booleanequals(java.lang.Object obj)inthashCode()intread()intread(byte[] b, int off, int len)voidreset()longskip(long n)
-
-
-
Constructor Detail
-
FastByteArrayInputStream
public FastByteArrayInputStream(byte[] buf)
Creates aFastByteArrayInputStreamso that it usesbufas its buffer array. The buffer array is not copied. The initial value ofposis0and the initial value ofcountis the length ofbuf.- Parameters:
buf- the input buffer.
-
FastByteArrayInputStream
public FastByteArrayInputStream(byte[] buf, int offset, int length)CreatesFastByteArrayInputStreamthat usesbufas its buffer array. The initial value ofposisoffsetand the initial value ofcountis the minimum ofoffset+lengthandbuf.length. The buffer array is not copied. The buffer's mark is set to the specified offset.- Parameters:
buf- the input buffer.offset- the offset in the buffer of the first byte to read.length- the maximum number of bytes to read from the buffer.
-
-
Method Detail
-
read
public int read()
- Overrides:
readin classjava.io.ByteArrayInputStream
-
read
public int read(byte[] b, int off, int len)- Overrides:
readin classjava.io.ByteArrayInputStream
-
skip
public long skip(long n)
- Overrides:
skipin classjava.io.ByteArrayInputStream
-
available
public int available()
- Overrides:
availablein classjava.io.ByteArrayInputStream
-
reset
public void reset()
- Overrides:
resetin classjava.io.ByteArrayInputStream
-
close
public void close()
- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classjava.io.ByteArrayInputStream
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
-