Class IOUtils


  • public class IOUtils
    extends java.lang.Object
    • Constructor Summary

      Constructors 
      Constructor Description
      IOUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      protected static java.nio.charset.CoderResult decode​(java.nio.CharBuffer charBuffer, java.nio.ByteBuffer buffer)  
      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.
      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.
      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.
      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.
      static java.lang.String md5Hash​(java.io.InputStream resourceAsStream)  
      static boolean readUtf8Stream​(java.io.InputStream is, java.nio.CharBuffer charBuffer)
      Reads the provided InputStream into the CharBuffer without causing allocations.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • threadLocalByteBuffer

        protected static final java.lang.ThreadLocal<java.nio.ByteBuffer> threadLocalByteBuffer
      • threadLocalCharsetDecoder

        protected static final java.lang.ThreadLocal<java.nio.charset.CharsetDecoder> threadLocalCharsetDecoder
    • Constructor Detail

      • IOUtils

        public IOUtils()
    • Method Detail

      • readUtf8Stream

        public static boolean readUtf8Stream​(java.io.InputStream is,
                                             java.nio.CharBuffer charBuffer)
                                      throws java.io.IOException
        Reads the provided InputStream into the CharBuffer without causing allocations.

        The InputStream is assumed to yield an UTF-8 encoded string.

        If the InputStream yields more chars than the Buffer.limit() of the provided CharBuffer, the rest of the input is silently ignored.

        Parameters:
        is - the source InputStream, which should be encoded with UTF-8.
        charBuffer - the CharBuffer the InputStream should be written into
        Returns:
        true, if the input stream could be decoded with the UTF-8 charset, false otherwise.
        Throws:
        java.io.IOException - in case of errors reading from the provided InputStream
      • 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 returned CoderResult will have the CoderResult.isError() flag set to true.

        If the byte[] yields more chars than the Buffer.limit() of the provided CharBuffer, the returned CoderResult will have the CoderResult.isOverflow() flag set to true.

        NOTE: This method does not CharBuffer.flip() the provided CharBuffer so that this method can be called multiple times with the same CharBuffer. If you are done with appending to the CharBuffer, you have to call CharBuffer.flip() manually.

        Parameters:
        bytes - the source byte[], which should be encoded with UTF-8.
        charBuffer - the CharBuffer the InputStream should 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 returned CoderResult will have the CoderResult.isError() flag set to true.

        If the byte[] yields more chars than the Buffer.limit() of the provided CharBuffer, the returned CoderResult will have the CoderResult.isOverflow() flag set to true.

        NOTE: This method does not CharBuffer.flip() the provided CharBuffer so that this method can be called multiple times with the same CharBuffer. If you are done with appending to the CharBuffer, you have to call CharBuffer.flip() manually.

        Parameters:
        bytes - the source byte[], which should be encoded with UTF-8
        charBuffer - the CharBuffer the InputStream should be written into
        offset - the start offset in array bytes at which the data is read
        length - 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 byte is assumed to yield an UTF-8 encoded string. If this is not true, the returned CoderResult will have the CoderResult.isError() flag set to true.

        If the provided CharBuffer has already reached its Buffer.limit(), the returned CoderResult will have the CoderResult.isOverflow() flag set to true.

        NOTE: This method does not CharBuffer.flip() the provided CharBuffer so that this method can be called multiple times with the same CharBuffer. If you are done with appending to the CharBuffer, you have to call CharBuffer.flip() manually.

        Parameters:
        b - the source byte[], which should be encoded with UTF-8
        charBuffer - the CharBuffer the InputStream should 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.IOException
        java.security.NoSuchAlgorithmException