Example usage for com.google.common.base Utf8 isWellFormed

List of usage examples for com.google.common.base Utf8 isWellFormed

Introduction

In this page you can find the example usage for com.google.common.base Utf8 isWellFormed.

Prototype

@CheckReturnValue
public static boolean isWellFormed(byte[] bytes) 

Source Link

Document

Returns true if bytes is a well-formed UTF-8 byte sequence according to Unicode 6.0.

Usage

From source file:org.dcache.nfs.v4.NameFilter.java

/**
 * Validate path and convert into UTB8 {@link String}.
 *
 * @param bytes to convert//w w  w  .j  a va  2 s. com
 * @return string
 * @throws ChimeraNFSException if provided {@code bytes} are not a UTF8
 * encoded.
 */
public static String convertPath(byte[] bytes) throws ChimeraNFSException {

    if (!Utf8.isWellFormed(bytes)) {
        throw new InvalException("invalid utf8 name");
    }

    String name = new String(bytes, UTF8);

    if (name.length() == 0) {
        throw new InvalException("bad path name");
    }

    if (name.indexOf('\0') != -1) {
        throw new BadNameException("name with null");
    }

    return name;
}

From source file:org.lenskit.util.Text.java

/**
 * Create a new Text instance from bytes.
 * @param bytes The bytes (must be valid UTF-8).  The array of bytes is copied.
 *//*from   w  w w.  jav a2s .  c o m*/
public Text(byte[] bytes) {
    Preconditions.checkArgument(Utf8.isWellFormed(bytes), "input bytes must be UTF-8");
    data = Arrays.copyOf(bytes, bytes.length);
}