/** * Reads a specified number of bytes to form a string. The length of the string (number of characters) is required * to notify when reading should stop. The index is increased the number of characters read. * * @param size * the length of the string to read. * @param charset * the charset used to convert the bytes to a string. * @return the string read. * @throws IOException * if EOS/EOF is reached before "size" number of bytes are read. */ public String readString(final int size, final Charset charset) throws IOException { final int start = position(); final byte[] content = new byte[size]; readFully(content); seek(start + size); int indexOfNullByte = size; // Look for zero terminated string from byte array for (int i = 0; i < size; i++) { if (content[i] == 0) { indexOfNullByte = i; break; } } final String s = new String(content, 0, indexOfNullByte, charset); return s; }
/** * Reads a specified number of bytes to form a string. The length of the string (number of characters) is required * to notify when reading should stop. The index is increased the number of characters read. * * @param size * the length of the string to read. * @param charset * the charset used to convert the bytes to a string. * @return the string read. * @throws IOException * if EOS/EOF is reached before "size" number of bytes are read. */ public String readString(final int size, final Charset charset) throws IOException { final int start = position(); final byte[] content = new byte[size]; readFully(content); seek(start + size); int indexOfNullByte = size; // Look for zero terminated string from byte array for (int i = 0; i < size; i++) { if (content[i] == 0) { indexOfNullByte = i; break; } } final String s = new String(content, 0, indexOfNullByte, charset); return s; }