Class IOUtils
- java.lang.Object
-
- co.elastic.apm.agent.util.IOUtils
-
public class IOUtils extends java.lang.Object
-
-
Field Summary
Fields Modifier and Type Field Description protected static intBYTE_BUFFER_CAPACITYprotected static java.lang.ThreadLocal<java.nio.ByteBuffer>threadLocalByteBufferprotected static java.lang.ThreadLocal<java.nio.charset.CharsetDecoder>threadLocalCharsetDecoder
-
Constructor Summary
Constructors Constructor Description IOUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description protected static java.nio.charset.CoderResultdecode(java.nio.CharBuffer charBuffer, java.nio.ByteBuffer buffer)static java.nio.charset.CoderResultdecodeUtf8Byte(byte b, java.nio.CharBuffer charBuffer)Decodes a single UTF-8 encoded byte into a char buffer, without allocating memory.static java.nio.charset.CoderResultdecodeUtf8Bytes(byte[] bytes, int offset, int length, java.nio.CharBuffer charBuffer)Decodes a UTF-8 encoded byte array into a char buffer, without allocating memory.static java.nio.charset.CoderResultdecodeUtf8Bytes(byte[] bytes, java.nio.CharBuffer charBuffer)Decodes a UTF-8 encoded byte array into a char buffer, without allocating memory.static java.io.FileexportResourceToDirectory(java.lang.String resource, java.lang.String parentDirectory, java.lang.String tempFileNamePrefix, java.lang.String tempFileNameExtension)Why it's synchronized : if the same JVM try to lock file, we got an java.nio.channels.OverlappingFileLockException.static java.lang.Stringmd5Hash(java.io.InputStream resourceAsStream)static booleanreadUtf8Stream(java.io.InputStream is, java.nio.CharBuffer charBuffer)Reads the providedInputStreaminto theCharBufferwithout causing allocations.
-
-
-
Field Detail
-
BYTE_BUFFER_CAPACITY
protected static final int BYTE_BUFFER_CAPACITY
- See Also:
- Constant Field Values
-
threadLocalByteBuffer
protected static final java.lang.ThreadLocal<java.nio.ByteBuffer> threadLocalByteBuffer
-
threadLocalCharsetDecoder
protected static final java.lang.ThreadLocal<java.nio.charset.CharsetDecoder> threadLocalCharsetDecoder
-
-
Method Detail
-
readUtf8Stream
public static boolean readUtf8Stream(java.io.InputStream is, java.nio.CharBuffer charBuffer) throws java.io.IOExceptionReads the providedInputStreaminto theCharBufferwithout causing allocations.The
InputStreamis assumed to yield an UTF-8 encoded string.If the
InputStreamyields more chars than theBuffer.limit()of the providedCharBuffer, the rest of the input is silently ignored.- Parameters:
is- the sourceInputStream, which should be encoded with UTF-8.charBuffer- theCharBuffertheInputStreamshould be written into- Returns:
true, if the input stream could be decoded with the UTF-8 charset,falseotherwise.- Throws:
java.io.IOException- in case of errors reading from the providedInputStream
-
decodeUtf8Bytes
public static java.nio.charset.CoderResult decodeUtf8Bytes(byte[] bytes, java.nio.CharBuffer charBuffer)Decodes a UTF-8 encoded byte array into a char buffer, without allocating memory.The
byte[]is assumed to yield an UTF-8 encoded string. If this is not true, the returnedCoderResultwill have theCoderResult.isError()flag set to true.If the
byte[]yields more chars than theBuffer.limit()of the providedCharBuffer, the returnedCoderResultwill have theCoderResult.isOverflow()flag set to true.NOTE: This method does not
CharBuffer.flip()the providedCharBufferso that this method can be called multiple times with the sameCharBuffer. If you are done with appending to theCharBuffer, you have to callCharBuffer.flip()manually.- Parameters:
bytes- the source byte[], which should be encoded with UTF-8.charBuffer- theCharBuffertheInputStreamshould be written into- Returns:
- a
CoderResult, indicating the success or failure of the decoding
-
decodeUtf8Bytes
public static java.nio.charset.CoderResult decodeUtf8Bytes(byte[] bytes, int offset, int length, java.nio.CharBuffer charBuffer)Decodes a UTF-8 encoded byte array into a char buffer, without allocating memory.The
byte[]is assumed to yield an UTF-8 encoded string. If this is not true, the returnedCoderResultwill have theCoderResult.isError()flag set to true.If the
byte[]yields more chars than theBuffer.limit()of the providedCharBuffer, the returnedCoderResultwill have theCoderResult.isOverflow()flag set to true.NOTE: This method does not
CharBuffer.flip()the providedCharBufferso that this method can be called multiple times with the sameCharBuffer. If you are done with appending to theCharBuffer, you have to callCharBuffer.flip()manually.- Parameters:
bytes- the source byte[], which should be encoded with UTF-8charBuffer- theCharBuffertheInputStreamshould be written intooffset- the start offset in arraybytesat which the data is readlength- the maximum number of bytes to read- Returns:
- a
CoderResult, indicating the success or failure of the decoding
-
decodeUtf8Byte
public static java.nio.charset.CoderResult decodeUtf8Byte(byte b, java.nio.CharBuffer charBuffer)Decodes a single UTF-8 encoded byte into a char buffer, without allocating memory.The
byteis assumed to yield an UTF-8 encoded string. If this is not true, the returnedCoderResultwill have theCoderResult.isError()flag set to true.If the provided
CharBufferhas already reached itsBuffer.limit(), the returnedCoderResultwill have theCoderResult.isOverflow()flag set to true.NOTE: This method does not
CharBuffer.flip()the providedCharBufferso that this method can be called multiple times with the sameCharBuffer. If you are done with appending to theCharBuffer, you have to callCharBuffer.flip()manually.- Parameters:
b- the source byte[], which should be encoded with UTF-8charBuffer- theCharBuffertheInputStreamshould be written into- Returns:
- a
CoderResult, indicating the success or failure of the decoding
-
decode
protected static java.nio.charset.CoderResult decode(java.nio.CharBuffer charBuffer, java.nio.ByteBuffer buffer)
-
exportResourceToDirectory
public static java.io.File exportResourceToDirectory(java.lang.String resource, java.lang.String parentDirectory, java.lang.String tempFileNamePrefix, java.lang.String tempFileNameExtension)Why it's synchronized : if the same JVM try to lock file, we got an java.nio.channels.OverlappingFileLockException. So we need to block until the file is totally written.
-
md5Hash
public static java.lang.String md5Hash(java.io.InputStream resourceAsStream) throws java.io.IOException, java.security.NoSuchAlgorithmException- Throws:
java.io.IOExceptionjava.security.NoSuchAlgorithmException
-
-