com.mysql.cj.util

## Class Util

- java.lang.Object
- - com.mysql.cj.util.Util

    public class Util
    extends Object

Various utility methods for the driver.

### Field Summary

Fields
`private static ConcurrentMap<Class<?>,Class<?>[]>` `implementedInterfacesCache` Cache for the implemented interfaces searched.
`private static ConcurrentMap<Class<?>,Boolean>` `isJdbcInterfaceCache` Cache for the JDBC interfaces already verified
`private static int` `jvmUpdateNumber`
`private static int` `jvmVersion`

### Constructor Summary

Constructors
Constructor and Description
`Util()`

### Method Summary

All Methods Static Methods Concrete Methods
`static Map<Object,Object>` `calculateDifferences(Map<?,?> map1, Map<?,?> map2)`
`static Class<?>[]` `getImplementedInterfaces(Class<?> clazz)` Retrieves a list with all interfaces implemented by the given class.
`static <T> T` `getInstance(Class<T> returnType, String className, Class<?>[] argTypes, Object[] args, ExceptionInterceptor exceptionInterceptor)` Creates an instance of the specified class name through reflection using the given arguments, as long as the class implements the return type specified.
`static int` `getJVMUpdateNumber()`
`static int` `getJVMVersion()`
`static String` `getPackageName(Class<?> clazz)` Returns the package name of the given class.
`static <T> T` `handleNewInstance(Constructor<T> ctor, Object[] args, ExceptionInterceptor exceptionInterceptor)` Handles constructing new instance with the given constructor and wrapping (or not, as required) the exceptions that could possibly be generated.
`static boolean` `isCommunityEdition(String serverVersion)` Checks whether the given server version string is a MySQL Community edition.
`static boolean` `isEnterpriseEdition(String serverVersion)` Checks whether the given server version string is a MySQL Enterprise/Commercial edition.
`static boolean` `isJdbcInterface(Class<?> clazz)` Recursively checks for interfaces on the given class to determine if it implements a java.sql, javax.sql or com.mysql.cj.jdbc interface.
`static boolean` `isJdbcPackage(String packageName)` Check if the package name is a known JDBC package.
`static boolean` `isRunningOnWindows()` Checks if the JVM is running on Windows Operating System.
`static boolean` `jvmMeetsMinimum(int version, int updateNumber)`
`static <T> List<T>` `loadClasses(Class<T> instancesType, String extensionClassNames, String errorMessageKey, ExceptionInterceptor exceptionInterceptor)`
`static int` `readBlock(InputStream i, byte[] b, ExceptionInterceptor exceptionInterceptor)`
`static int` `readBlock(InputStream i, byte[] b, int length, ExceptionInterceptor exceptionInterceptor)`
`static int` `readFully(Reader reader, char[] buf, int length)` Reads length bytes from reader into buf.
`static long` `secondsSinceMillis(long timeInMillis)` Computes the number of seconds elapsed since the given time in milliseconds.
`static String` `stackTraceToString(Throwable ex)` Converts a nested exception into a nicer message.
`static int` `truncateAndConvertToInt(long longValue)` Converts long to int, truncating to maximum/minimum value if needed.
`static int[]` `truncateAndConvertToInt(long[] longArray)` Converts long[] to int[], truncating to maximum/minimum value if needed.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Field Detail

#### jvmVersion

    private static int jvmVersion

#### jvmUpdateNumber

    private static int jvmUpdateNumber

#### isJdbcInterfaceCache

    private static final ConcurrentMap<Class<?>,Boolean> isJdbcInterfaceCache

Cache for the JDBC interfaces already verified

#### implementedInterfacesCache

    private static final ConcurrentMap<Class<?>,Class<?>[]> implementedInterfacesCache

Cache for the implemented interfaces searched.

### Constructor Detail

#### Util

    public Util()

### Method Detail

#### getJVMVersion

    public static int getJVMVersion()

#### jvmMeetsMinimum

    public static boolean jvmMeetsMinimum(int version,
                                          int updateNumber)

#### getJVMUpdateNumber

    public static int getJVMUpdateNumber()

#### isCommunityEdition

    public static boolean isCommunityEdition(String serverVersion)

Checks whether the given server version string is a MySQL Community edition.

Parameters:
  `serverVersion` - full server version string
Returns:
  true if version does not contain "enterprise", "commercial" or "advanced"

#### isEnterpriseEdition

    public static boolean isEnterpriseEdition(String serverVersion)

Checks whether the given server version string is a MySQL Enterprise/Commercial edition.

Parameters:
  `serverVersion` - full server version string
Returns:
  true if version contains "enterprise", "commercial" or "advanced"

#### stackTraceToString

    public static String stackTraceToString(Throwable ex)

Converts a nested exception into a nicer message.

Parameters:
  `ex` - the exception to expand into a message.
Returns:
  a message containing the exception, the message (if any), and a stacktrace.

#### getInstance

    public static <T> T getInstance(Class<T> returnType,
                                    String className,
                                    Class<?>[] argTypes,
                                    Object[] args,
                                    ExceptionInterceptor exceptionInterceptor)

Creates an instance of the specified class name through reflection using the given arguments, as long as the class implements the return type specified.

Type Parameters:
  `T` - The return type of the new instance.
Parameters:
  `returnType` - the class of the return type of the new instance, usually an interface.
  `className` - the name of the class to instantiate through reflection.
  `argTypes` - the type of the arguments of the constructor to be used for instantiating the class.
  `args` - the arguments to supply when exectuing the new instance constructor.
  `exceptionInterceptor` - the `ExceptionInterceptor` to handle new exceptions thrown.
Returns:
  if all validations succeed, an instance of the class with the specified name.

#### handleNewInstance

    public static <T> T handleNewInstance(Constructor<T> ctor,
                                          Object[] args,
                                          ExceptionInterceptor exceptionInterceptor)

Handles constructing new instance with the given constructor and wrapping (or not, as required) the exceptions that could possibly be generated.

Type Parameters:
  `T` - The type of the new class instance to return.
Parameters:
  `ctor` - constructor
  `args` - arguments for constructor
  `exceptionInterceptor` - exception interceptor
Returns:
  object

#### calculateDifferences

    public static Map<Object,Object> calculateDifferences(Map<?,?> map1,
                                                          Map<?,?> map2)

#### loadClasses

    public static <T> List<T> loadClasses(Class<T> instancesType,
                                          String extensionClassNames,
                                          String errorMessageKey,
                                          ExceptionInterceptor exceptionInterceptor)

#### isJdbcInterface

    public static boolean isJdbcInterface(Class<?> clazz)

Recursively checks for interfaces on the given class to determine if it implements a java.sql, javax.sql or com.mysql.cj.jdbc interface.

Parameters:
  `clazz` - The class to investigate.
Returns:
  boolean

#### isJdbcPackage

    public static boolean isJdbcPackage(String packageName)

Check if the package name is a known JDBC package.

Parameters:
  `packageName` - The package name to check.
Returns:
  boolean

#### getImplementedInterfaces

    public static Class<?>[] getImplementedInterfaces(Class<?> clazz)

Retrieves a list with all interfaces implemented by the given class. If possible gets this information from a cache instead of navigating through the object hierarchy. Results are stored in a cache for future reference.

Parameters:
  `clazz` - The class from which the interface list will be retrieved.
Returns:
  An array with all the interfaces for the given class.

#### secondsSinceMillis

    public static long secondsSinceMillis(long timeInMillis)

Computes the number of seconds elapsed since the given time in milliseconds.

Parameters:
  `timeInMillis` - The past instant in milliseconds.
Returns:
  The number of seconds, truncated, elapsed since timeInMillis.

#### truncateAndConvertToInt

    public static int truncateAndConvertToInt(long longValue)

Converts long to int, truncating to maximum/minimum value if needed.

Parameters:
  `longValue` - long value
Returns:
  int value

#### truncateAndConvertToInt

    public static int[] truncateAndConvertToInt(long[] longArray)

Converts long[] to int[], truncating to maximum/minimum value if needed.

Parameters:
  `longArray` - log values
Returns:
  int values

#### getPackageName

    public static String getPackageName(Class<?> clazz)

Returns the package name of the given class. Using clazz.getPackage().getName() is not an alternative because under some class loaders the method getPackage() just returns null.

Parameters:
  `clazz` - the Class from which to get the package name
Returns:
  the package name

#### isRunningOnWindows

    public static boolean isRunningOnWindows()

Checks if the JVM is running on Windows Operating System.

Returns:
  `true` if currently running on Windows, `false` otherwise.

#### readFully

    public static int readFully(Reader reader,
                                char[] buf,
                                int length)
                         throws IOException

Reads length bytes from reader into buf. Blocks until enough input is available

Parameters:
  `reader` - `Reader`
  `buf` - char array to read into
  `length` - number of chars to read
Returns:
  the actual number of chars read
Throws:
  `IOException` - if an error occurs

#### readBlock

    public static final int readBlock(InputStream i,
                                      byte[] b,
                                      ExceptionInterceptor exceptionInterceptor)

#### readBlock

    public static final int readBlock(InputStream i,
                                      byte[] b,
                                      int length,
                                      ExceptionInterceptor exceptionInterceptor)

Copyright © 2016, 2024, Oracle and/or its affiliates.