/** * Get a byte string from the bytes of a character string. * * @param str the character string * @param charset the character set to use * @return the byte string * @throws UnsupportedEncodingException if the encoding is not supported */ public static ByteString getBytes(String str, String charset) throws UnsupportedEncodingException { return new ByteString(str.getBytes(charset)); }
/** * Create a byte string from the given array segment. * * @param b the byte array * @param offs the offset into the array * @param len the number of bytes to copy * @return the new byte string */ public static ByteString copyOf(byte[] b, int offs, int len) { return new ByteString(Arrays.copyOfRange(b, offs, offs + len)); }
/** * Get a byte string from the bytes of a character string. * * @param str the character string * @param charset the character set to use * @return the byte string */ public static ByteString getBytes(String str, Charset charset) { return new ByteString(str.getBytes(charset)); }
/** * Get the substring of this string starting at the given offset. * * @param offs the offset * @param len the substring length * @return the substring */ public ByteString substring(int offs, int len) { if (this.len - offs < len) { throw new IndexOutOfBoundsException(); } return new ByteString(bytes, this.offs + offs, len); }
public ByteString concat(byte[] suffixBytes, int offs, int len) { if (len <= 0) { return this; } final int length = this.len; byte[] newBytes = Arrays.copyOfRange(bytes, this.offs, length + len); System.arraycopy(suffixBytes, offs, newBytes, length, len); return new ByteString(newBytes); }
/** * Create a byte string of the given literal bytes. The given array is copied. * * @param bytes the bytes * @return the byte string */ public static ByteString of(byte... bytes) { return new ByteString(bytes.clone()); }
/** * Get a byte string from a ByteBuffer. * * @param buffer the buffer * @param length the number of bytes to get * @return the byte string */ public static ByteString getBytes(ByteBuffer buffer, int length) { return new ByteString(Buffers.take(buffer, length)); }
public ByteString concat(String suffix, int offs, int len) { if (len <= 0) { return this; } final byte[] bytes = this.bytes; final int length = this.len; byte[] newBytes = Arrays.copyOfRange(bytes, offs, offs + length + len); getStringBytes(false, newBytes, length, suffix, offs, len); return new ByteString(newBytes); }
/** * Get a byte string from the bytes of the character string. The string must be a Latin-1 string. * * @param str the character string * @return the byte string */ public static ByteString getBytes(String str) { final int length = str.length(); return new ByteString(getStringBytes(false, new byte[length], 0, str, 0, length), 0, length); }
public static ByteString concat(String prefix, ByteString suffix) { final int prefixLength = prefix.length(); final byte[] suffixBytes = suffix.bytes; final int suffixLength = suffixBytes.length; final byte[] newBytes = new byte[prefixLength + suffixLength]; getStringBytes(false, newBytes, 0, prefix, 0, prefixLength); System.arraycopy(suffixBytes, suffix.offs, newBytes, prefixLength, suffixLength); return new ByteString(newBytes); }
/** * Get a byte string from all remaining bytes of a ByteBuffer. * * @param buffer the buffer * @return the byte string */ public static ByteString getBytes(ByteBuffer buffer) { return new ByteString(Buffers.take(buffer)); }
public static ByteString concat(String prefix, String suffix) { final int prefixLength = prefix.length(); final int suffixLength = suffix.length(); final byte[] newBytes = new byte[prefixLength + suffixLength]; getStringBytes(false, newBytes, 0, prefix, 0, prefixLength); getStringBytes(false, newBytes, prefixLength, suffix, 0, suffixLength); return new ByteString(newBytes); }
/** * Get a string version of the given value. * * @param val the value * @return the string */ public static ByteString fromInt(int val) { if (val == 0) return ZERO; // afaik no faster way exists to do this int i = decimalCount(abs(val)); final byte[] b; if (val < 0) { b = new byte[++i]; b[0] = '-'; } else { b = new byte[i]; } int quo; // modulus int mod; do { quo = val / 10; mod = val - ((quo << 3) + (quo << 1)); b[--i] = (byte) (mod + '0'); val = quo; } while (i > 0); return new ByteString(b); }
/** * Get a string version of the given value. * * @param val the value * @return the string */ public static ByteString fromLong(long val) { if (val == 0) return ZERO; // afaik no faster way exists to do this int i = decimalCount(abs(val)); final byte[] b; if (val < 0) { b = new byte[++i]; b[0] = '-'; } else { b = new byte[i]; } long quo; // modulus int mod; do { quo = val / 10; mod = (int) (val - ((quo << 3) + (quo << 1))); b[--i] = (byte) (mod + '0'); val = quo; } while (i > 0); return new ByteString(b); }
/** * Get a byte string from the bytes of a character string. * * @param str the character string * @param charset the character set to use * @return the byte string * @throws UnsupportedEncodingException if the encoding is not supported */ public static ByteString getBytes(String str, String charset) throws UnsupportedEncodingException { return new ByteString(str.getBytes(charset)); }
/** * Create a byte string from the given array segment. * * @param b the byte array * @param offs the offset into the array * @param len the number of bytes to copy * @return the new byte string */ public static ByteString copyOf(byte[] b, int offs, int len) { return new ByteString(Arrays.copyOfRange(b, offs, offs + len)); }
public ByteString concat(byte[] suffixBytes, int offs, int len) { if (len <= 0) { return this; } final int length = this.len; byte[] newBytes = Arrays.copyOfRange(bytes, this.offs, length + len); System.arraycopy(suffixBytes, offs, newBytes, length, len); return new ByteString(newBytes); }
/** * Get a byte string from a ByteBuffer. * * @param buffer the buffer * @param length the number of bytes to get * @return the byte string */ public static ByteString getBytes(ByteBuffer buffer, int length) { return new ByteString(Buffers.take(buffer, length)); }
/** * Get a byte string from the bytes of the character string. The string must be a Latin-1 string. * * @param str the character string * @return the byte string */ public static ByteString getBytes(String str) { final int length = str.length(); return new ByteString(getStringBytes(false, new byte[length], 0, str, 0, length), 0, length); }
public static ByteString concat(String prefix, String suffix) { final int prefixLength = prefix.length(); final int suffixLength = suffix.length(); final byte[] newBytes = new byte[prefixLength + suffixLength]; getStringBytes(false, newBytes, 0, prefix, 0, prefixLength); getStringBytes(false, newBytes, prefixLength, suffix, 0, suffixLength); return new ByteString(newBytes); }