Package com.cedarsoftware.util
Class FastByteArrayInputStream
java.lang.Object
java.io.InputStream
com.cedarsoftware.util.FastByteArrayInputStream
- All Implemented Interfaces:
Closeable,AutoCloseable
Faster version of ByteArrayInputStream that does not have synchronized methods.
- Author:
- John DeRegnaucourt ([email protected])
Copyright (c) Cedar Software LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
License
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintvoidclose()voidmark(int readLimit) booleanintread()intread(byte[] b, int off, int len) byte[]Reads all remaining bytes from this input stream.byte[]readNBytes(int len) Reads up to a specified number of bytes from this input stream.voidreset()longskip(long n) longtransferTo(OutputStream out) Transfers all remaining bytes from this input stream to the given output stream.Methods inherited from class java.io.InputStream
nullInputStream, read, readNBytes, skipNBytes
-
Constructor Details
-
FastByteArrayInputStream
public FastByteArrayInputStream(byte[] buf)
-
-
Method Details
-
read
public int read()- Specified by:
readin classInputStream
-
read
public int read(byte[] b, int off, int len) - Overrides:
readin classInputStream
-
skip
public long skip(long n) - Overrides:
skipin classInputStream
-
available
public int available()- Overrides:
availablein classInputStream
-
mark
public void mark(int readLimit) - Overrides:
markin classInputStream
-
reset
public void reset()- Overrides:
resetin classInputStream
-
markSupported
public boolean markSupported()- Overrides:
markSupportedin classInputStream
-
readAllBytes
public byte[] readAllBytes()Reads all remaining bytes from this input stream. This method provides an optimized implementation that directly copies from the internal buffer rather than using the default InputStream implementation which uses intermediate buffers.This method is compatible with JDK 9+ where it overrides
InputStream.readAllBytes(), and works as a regular method on JDK 8.- Overrides:
readAllBytesin classInputStream- Returns:
- a byte array containing all remaining bytes from this stream
-
readNBytes
public byte[] readNBytes(int len) Reads up to a specified number of bytes from this input stream. This method provides an optimized implementation that directly copies from the internal buffer.This method is compatible with JDK 11+ where it overrides
InputStream.readNBytes(int), and works as a regular method on earlier JDK versions.- Overrides:
readNBytesin classInputStream- Parameters:
len- the maximum number of bytes to read- Returns:
- a byte array containing the bytes read from this stream
- Throws:
IllegalArgumentException- if len is negative
-
transferTo
Transfers all remaining bytes from this input stream to the given output stream. This method provides an optimized implementation that writes directly from the internal buffer in a single operation.This method is compatible with JDK 9+ where it overrides
InputStream.transferTo(OutputStream), and works as a regular method on JDK 8.- Overrides:
transferToin classInputStream- Parameters:
out- the output stream to write to- Returns:
- the number of bytes transferred
- Throws:
IOException- if an I/O error occurs when writing to the output streamNullPointerException- if out is null
-
close
public void close()- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classInputStream
-