All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.rt.storage.api.client.util.StringUtils Maven / Gradle / Ivy

package com.rt.storage.api.client.util;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

/**
 * Utilities for strings.
 *
 * @since 1.8
 * @author Yaniv Inbar
 */
public class StringUtils {

  /**
   * Line separator to use for this OS, i.e. {@code "\n"} or {@code "\r\n"}.
   *
   * @since 1.8
   */
  public static final String LINE_SEPARATOR = System.getProperty("line.separator");

  /**
   * Encodes the given string into a sequence of bytes using the UTF-8 charset, storing the result
   * into a new byte array.
   *
   * @param string the String to encode, may be null
   * @return encoded bytes, or null if the input string was null
   * @throws IllegalStateException Thrown when the charset is missing, which should be never
   *     according the Java specification.
   * @see Standard charsets
   * @since 1.8
   */
  public static byte[] getBytesUtf8(String string) {
    if (string == null) {
      return null;
    }
    return string.getBytes(StandardCharsets.UTF_8);
  }

  /**
   * Constructs a new String by decoding the specified array of bytes using the UTF-8
   * charset.
   *
   * @param bytes The bytes to be decoded into characters
   * @return A new String decoded from the specified array of bytes using the UTF-8
   *     charset, or null if the input byte array was null.
   * @throws IllegalStateException Thrown when a {@link UnsupportedEncodingException} is caught,
   *     which should never happen since the charset is required.
   * @since 1.8
   */
  public static String newStringUtf8(byte[] bytes) {
    if (bytes == null) {
      return null;
    }
    return new String(bytes, StandardCharsets.UTF_8);
  }

  private StringUtils() {}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy