static ByteString wrap(byte[] bytes) { return new ByteString(bytes); }
/** * Copies the given bytes into a {@code ByteString}. */ public static ByteString copyFrom(final byte[] bytes, final int offset, final int size) { final byte[] copy = new byte[size]; System.arraycopy(bytes, offset, copy, 0, size); return new ByteString(copy); }
/** * Encodes {@code text} into a sequence of bytes using the named charset and returns the result as a * {@code ByteString}. */ public static ByteString copyFrom(final String text, final String charsetName) { try { return new ByteString(text.getBytes(charsetName)); } catch (UnsupportedEncodingException e) { throw new RuntimeException(charsetName + " not supported?", e); } }
/** * Helper called by generated code to construct default values for bytes fields. * <p> * This is a lot like {@link #stringDefaultValue}, but for bytes fields. In this case we only need the second of the * two hacks -- allowing us to embed raw bytes as a string literal with ISO-8859-1 encoding. */ public static ByteString bytesDefaultValue(String bytes) { return new ByteString(byteArrayDefaultValue(bytes)); }
/** * Encodes {@code text} into a sequence of UTF-8 bytes and returns the result as a {@code ByteString}. */ public static ByteString copyFromUtf8(final String text) { return new ByteString(STRING.ser(text)); /* * @try { return new ByteString(text.getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { throw new * RuntimeException("UTF-8 not supported?", e); } */ }