Class Utils



  • public final class Utils
    extends java.lang.Object
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.Exception checkGrpcUrl​(java.lang.String url)
      Check if the strings Grpc url is valid
      static java.lang.String combinePaths​(java.lang.String first, java.lang.String... other)
      Combine two or more paths
      static void deleteFileOrDirectory​(java.io.File file)
      Delete a file or directory
      static java.lang.String generateDirectoryHash​(java.lang.String rootDir, java.lang.String chaincodeDir, java.lang.String hash)
      Generate hash of a chaincode directory
      static byte[] generateNonce​()  
      static java.lang.String generateParameterHash​(java.lang.String path, java.lang.String func, java.util.List<java.lang.String> args)
      Generate parameter hash for the given chaincode path,func and args
      static byte[] generateTarGz​(java.io.File sourceDirectory, java.lang.String pathPrefix, java.io.File chaincodeMetaInf)
      Compress the contents of given directory using Tar and Gzip to an in-memory byte array.
      static com.google.protobuf.Timestamp generateTimestamp​()
      Create a new Timestamp instance based on the current time
      static java.lang.String generateUUID​()
      Generate a v4 UUID
      static byte[] hash​(byte[] input, org.bouncycastle.crypto.Digest digest)
      Generate hash of the given input using the given Digest.
      static boolean isNullOrEmpty​(java.lang.String url)
      Check if a string is null or empty.
      static java.lang.String logString​(java.lang.String string)
      Makes logging strings which can be long or with unprintable characters be logged and trimmed.
      static java.util.Properties parseGrpcUrl​(java.lang.String url)  
      static byte[] readFile​(java.io.File input)
      Read the contents a file.
      static byte[] readFileFromClasspath​(java.lang.String fileName)
      Read a file from classpath
      static java.lang.String toHexString​(byte[] bytes)  
      static java.lang.String toHexString​(com.google.protobuf.ByteString byteString)  
      • Methods inherited from class java.lang.Object

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

      • generateParameterHash

        public static java.lang.String generateParameterHash​(java.lang.String path,
                                                             java.lang.String func,
                                                             java.util.List<java.lang.String> args)
        Generate parameter hash for the given chaincode path,func and args
        Parameters:
        path - Chaincode path
        func - Chaincode function name
        args - List of arguments
        Returns:
        hash of path, func and args
      • generateDirectoryHash

        public static java.lang.String generateDirectoryHash​(java.lang.String rootDir,
                                                             java.lang.String chaincodeDir,
                                                             java.lang.String hash)
                                                      throws java.io.IOException
        Generate hash of a chaincode directory
        Parameters:
        rootDir - Root directory
        chaincodeDir - Channel code directory
        hash - Previous hash (if any)
        Returns:
        hash of the directory
        Throws:
        java.io.IOException
      • generateTarGz

        public static byte[] generateTarGz​(java.io.File sourceDirectory,
                                           java.lang.String pathPrefix,
                                           java.io.File chaincodeMetaInf)
                                    throws java.io.IOException
        Compress the contents of given directory using Tar and Gzip to an in-memory byte array.
        Parameters:
        sourceDirectory - the source directory.
        pathPrefix - a path to be prepended to every file name in the .tar.gz output, or null if no prefix is required.
        chaincodeMetaInf -
        Returns:
        the compressed directory contents.
        Throws:
        java.io.IOException
      • readFile

        public static byte[] readFile​(java.io.File input)
                               throws java.io.IOException
        Read the contents a file.
        Parameters:
        input - source file to read.
        Returns:
        contents of the file.
        Throws:
        java.io.IOException
      • generateUUID

        public static java.lang.String generateUUID​()
        Generate a v4 UUID
        Returns:
        String representation of UUID
      • generateTimestamp

        public static com.google.protobuf.Timestamp generateTimestamp​()
        Create a new Timestamp instance based on the current time
        Returns:
        timestamp
      • deleteFileOrDirectory

        public static void deleteFileOrDirectory​(java.io.File file)
                                          throws java.io.IOException
        Delete a file or directory
        Parameters:
        file - File representing file or directory
        Throws:
        java.io.IOException
      • hash

        public static byte[] hash​(byte[] input,
                                  org.bouncycastle.crypto.Digest digest)
        Generate hash of the given input using the given Digest.
        Parameters:
        input - input data.
        digest - the digest to use for hashing
        Returns:
        hashed data.
      • combinePaths

        public static java.lang.String combinePaths​(java.lang.String first,
                                                    java.lang.String... other)
        Combine two or more paths
        Parameters:
        first - parent directory path
        other - children
        Returns:
        combined path
      • readFileFromClasspath

        public static byte[] readFileFromClasspath​(java.lang.String fileName)
                                            throws java.io.IOException
        Read a file from classpath
        Parameters:
        fileName -
        Returns:
        byte[] data
        Throws:
        java.io.IOException
      • parseGrpcUrl

        public static java.util.Properties parseGrpcUrl​(java.lang.String url)
      • checkGrpcUrl

        public static java.lang.Exception checkGrpcUrl​(java.lang.String url)
        Check if the strings Grpc url is valid
        Parameters:
        url -
        Returns:
        Return the exception that indicates the error or null if ok.
      • isNullOrEmpty

        public static boolean isNullOrEmpty​(java.lang.String url)
        Check if a string is null or empty.
        Parameters:
        url - the string to test.
        Returns:
        true if the string is null or empty; otherwise false.
      • logString

        public static java.lang.String logString​(java.lang.String string)
        Makes logging strings which can be long or with unprintable characters be logged and trimmed.
        Parameters:
        string - Unsafe string too long
        Returns:
        returns a string which does not have unprintable characters and trimmed in length.
      • generateNonce

        public static byte[] generateNonce​()
      • toHexString

        public static java.lang.String toHexString​(com.google.protobuf.ByteString byteString)
      • toHexString

        public static java.lang.String toHexString​(byte[] bytes)