Package org.apache.solr.common.util

# Class ByteUtils

java.lang.Object
org.apache.solr.common.util.ByteUtils

public class ByteUtils extends Object

## Field Summary

Fields
Modifier and Type
Field
Description
`static final int`
`MAX_UTF8_BYTES_PER_CHAR`
Maximum number of UTF8 bytes per UTF16 character.

## Constructor Summary

Constructors
Constructor
Description
`ByteUtils()`

## Method Summary

Modifier and Type
Method
Description
`static int`
`calcUTF16toUTF8Length(CharSequence s, int offset, int len)`
Calculates the number of UTF8 bytes necessary to write a UTF16 string.
`static int`
`UTF16toUTF8(CharSequence s, int offset, int len, byte[] result, int resultOffset)`
Writes UTF8 into the byte array, starting at offset.
`static String`
`UTF8toUTF16(byte[] utf8, int offset, int len)`
Convert UTF8 bytes into a String
`static int`
`UTF8toUTF16(byte[] utf8, int offset, int len, char[] out, int out_offset)`
Converts utf8 to utf16 and returns the number of 16-bit Java chars written.
`static void`
`UTF8toUTF16(byte[] utf8, int offset, int len, org.noggit.CharArr out)`
Convert UTF8 bytes into UTF16 characters.
`static int`
`writeUTF16toUTF8(CharSequence s, int offset, int len, OutputStream fos, byte[] scratch)`
Writes UTF8 into the given OutputStream by first writing to the given scratch array and then writing the contents of the scratch array to the OutputStream.

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

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

## Field Details

### MAX_UTF8_BYTES_PER_CHAR

public static final int MAX_UTF8_BYTES_PER_CHAR
Maximum number of UTF8 bytes per UTF16 character.

See Also:
  - Constant Field Values

## Constructor Details

### ByteUtils

public ByteUtils()

## Method Details

### UTF8toUTF16

public static int UTF8toUTF16(byte[] utf8, int offset, int len, char[] out, int out_offset)
Converts utf8 to utf16 and returns the number of 16-bit Java chars written. Full characters are read, even if this reads past the length passed (and can result in an ArrayOutOfBoundsException if invalid UTF8 is passed). Explicit checks for valid UTF8 are not performed. The char[] out should probably have enough room to hold the worst case of each byte becoming a Java char.

### UTF8toUTF16

public static void UTF8toUTF16(byte[] utf8, int offset, int len, org.noggit.CharArr out)
Convert UTF8 bytes into UTF16 characters.

### UTF8toUTF16

public static String UTF8toUTF16(byte[] utf8, int offset, int len)
Convert UTF8 bytes into a String

### UTF16toUTF8

public static int UTF16toUTF8(CharSequence s, int offset, int len, byte[] result, int resultOffset)
Writes UTF8 into the byte array, starting at offset. The caller should ensure that there is enough space for the worst-case scenario.

Returns:
  the number of bytes written

### writeUTF16toUTF8

public static int writeUTF16toUTF8(CharSequence s, int offset, int len, OutputStream fos, byte[] scratch) throws IOException
Writes UTF8 into the given OutputStream by first writing to the given scratch array and then writing the contents of the scratch array to the OutputStream. The given scratch byte array is used to buffer intermediate data before it is written to the output stream.

Returns:
  the number of bytes written
Throws:
  `IOException`

### calcUTF16toUTF8Length

public static int calcUTF16toUTF8Length(CharSequence s, int offset, int len)
Calculates the number of UTF8 bytes necessary to write a UTF16 string.

Returns:
  the number of bytes written