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.ByteArrayInputStream
A fast, non-thread-safe implementation ofByteArrayInputStream
.This class extends the standard
ByteArrayInputStream
to 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 aFastByteArrayInputStream
so that it usesbuf
as its buffer array.FastByteArrayInputStream(byte[] buf, int offset, int length)
CreatesFastByteArrayInputStream
that usesbuf
as its buffer array.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
available()
void
close()
boolean
equals(java.lang.Object obj)
int
hashCode()
int
read()
int
read(byte[] b, int off, int len)
void
reset()
long
skip(long n)
-
-
-
Constructor Detail
-
FastByteArrayInputStream
public FastByteArrayInputStream(byte[] buf)
Creates aFastByteArrayInputStream
so that it usesbuf
as its buffer array. The buffer array is not copied. The initial value ofpos
is0
and the initial value ofcount
is the length ofbuf
.- Parameters:
buf
- the input buffer.
-
FastByteArrayInputStream
public FastByteArrayInputStream(byte[] buf, int offset, int length)
CreatesFastByteArrayInputStream
that usesbuf
as its buffer array. The initial value ofpos
isoffset
and the initial value ofcount
is the minimum ofoffset+length
andbuf.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:
read
in classjava.io.ByteArrayInputStream
-
read
public int read(byte[] b, int off, int len)
- Overrides:
read
in classjava.io.ByteArrayInputStream
-
skip
public long skip(long n)
- Overrides:
skip
in classjava.io.ByteArrayInputStream
-
available
public int available()
- Overrides:
available
in classjava.io.ByteArrayInputStream
-
reset
public void reset()
- Overrides:
reset
in classjava.io.ByteArrayInputStream
-
close
public void close()
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Overrides:
close
in classjava.io.ByteArrayInputStream
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
-