/** * Attempt to read a file as a string * @throws IOException */ public static String readFileAsString(String path, Charset charset) throws IOException { if (charset == null) charset = Charset.defaultCharset(); try (FileChannel fc = FileChannel.open(Paths.get(path))) { MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); return charset.decode(bb).toString(); } }
Charset utf8charset = Charset.forName("UTF-8"); Charset iso88591charset = Charset.forName("ISO-8859-1"); ByteBuffer inputBuffer = ByteBuffer.wrap(new byte[]{(byte)0xC3, (byte)0xA2}); // decode UTF-8 CharBuffer data = utf8charset.decode(inputBuffer); // encode ISO-8559-1 ByteBuffer outputBuffer = iso88591charset.encode(data); byte[] outputData = outputBuffer.array();
/** * Reads a string encoded with UTF_16LE of a given length * * @param length the number of characters * @return the string */ public String readWString(int length) { int numBytes = length * 2; String result = StandardCharsets.UTF_16LE.decode(ByteBuffer.wrap(bytes, position, numBytes)).toString(); position += numBytes; return result; }
private static String getTextFromFilePath(Path file) throws IOException { byte[] bytes = Files.readAllBytes(file); CharBuffer chars = Charset.defaultCharset().decode(ByteBuffer.wrap(bytes)); return chars.toString(); } }
System.out.println(Charset.defaultCharset().name()); ByteBuffer bybf = ByteBuffer.wrap("Olé".getBytes()); Charset utf16 = Charset.forName("UTF-16"); CharBuffer chbf = utf16.decode(bybf); System.out.println(chbf); bybf = ByteBuffer.wrap("Olé".getBytes(utf16)); chbf = utf16.decode(bybf); System.out.println(chbf);
/** * Decodes octets to characters using the UTF-8 decoding and appends * the characters to a StringBuffer. * @return the index to the next unchecked character in the string to decode */ private static int decodeOctets(int i, ByteBuffer bb, StringBuilder sb) { // If there is only one octet and is an ASCII character if (bb.limit() == 1 && (bb.get(0) & 0xFF) < 0x80) { // Octet can be appended directly sb.append((char) bb.get(0)); return i + 2; } else { // CharBuffer cb = UTF_8_CHARSET.decode(bb); sb.append(cb.toString()); return i + bb.limit() * 3 - 1; } }
@Test public void testAppendTakingTwoIntsWithIndexOutOfBoundsThrowsStringIndexOutOfBoundsExceptionTwo() { assertThatExceptionOfType(StringIndexOutOfBoundsException.class).isThrownBy(() -> { final Charset charset = Charset.defaultCharset(); final ByteBuffer byteBuffer = charset.encode("asdf"); final CharBuffer charBuffer = charset.decode(byteBuffer); new StrBuilder().append(charBuffer, 933, 654); }); }
private static String getTextFromFilePath(Path file) throws IOException { byte[] bytes = Files.readAllBytes(file); CharBuffer chars = Charset.defaultCharset().decode(ByteBuffer.wrap(bytes)); return chars.toString(); } }
/** * 将编码的byteBuffer数据转换为字符串 * * @param data 数据 * @param charset 字符集,如果为空使用当前系统字符集 * @return 字符串 */ public static String str(ByteBuffer data, Charset charset) { if (null == charset) { charset = Charset.defaultCharset(); } return charset.decode(data).toString(); }
/** * Decodes octets to characters using the UTF-8 decoding and appends * the characters to a StringBuffer. * * @return the index to the next unchecked character in the string to decode */ private static int decodeOctets(final int i, final ByteBuffer bb, final StringBuilder sb) { // If there is only one octet and is an ASCII character if (bb.limit() == 1 && (bb.get(0) & 0xFF) < 0x80) { // Octet can be appended directly sb.append((char) bb.get(0)); return i + 2; } else { // final CharBuffer cb = UTF_8_CHARSET.decode(bb); sb.append(cb.toString()); return i + bb.limit() * 3 - 1; } }
@Test public void testAppendTakingTwoIntsWithZeroThrowsStringIndexOutOfBoundsException() { assertThatExceptionOfType(StringIndexOutOfBoundsException.class).isThrownBy(() -> { final Charset charset = Charset.defaultCharset(); final ByteBuffer byteBuffer = charset.encode("end < start"); final CharBuffer charBuffer = charset.decode(byteBuffer); new StrBuilder(630).append(charBuffer, 0, 630); }); }
String content = readFile(jsonFilePath, Charset.defaultCharset()); static String readFile(String path, Charset encoding) throws IOException { byte[] encoded = Files.readAllBytes(Paths.get(path)); return encoding.decode(ByteBuffer.wrap(encoded)).toString(); }
static char[] toChars(byte[] bytes) { Charset charset=Charset.forName("UTF-8"); ByteBuffer byteBuffer=ByteBuffer.wrap(bytes); CharBuffer charBuffer=charset.decode(byteBuffer); char[] chars=Arrays.copyOf(charBuffer.array(), charBuffer.limit()); Arrays.fill(charBuffer.array(), '\u0000'); // clear the cleartext Arrays.fill(byteBuffer.array(), (byte) 0); // clear the ciphertext return chars; } }
/** * 将编码的byteBuffer数据转换为字符串 * * @param data 数据 * @param charset 字符集,如果为空使用当前系统字符集 * @return 字符串 */ public static String str(ByteBuffer data, Charset charset) { if (null == charset) { charset = Charset.defaultCharset(); } return charset.decode(data).toString(); }
/** * Decodes octets to characters using the UTF-8 decoding and appends * the characters to a StringBuffer. * * @return the index to the next unchecked character in the string to decode */ private static int decodeOctets(final int i, final ByteBuffer bb, final StringBuilder sb) { // If there is only one octet and is an ASCII character if (bb.limit() == 1 && (bb.get(0) & 0xFF) < 0x80) { // Octet can be appended directly sb.append((char) bb.get(0)); return i + 2; } else { // final CharBuffer cb = UTF_8_CHARSET.decode(bb); sb.append(cb.toString()); return i + bb.limit() * 3 - 1; } }
@Test public void testAppendTakingTwoIntsWithZeroThrowsStringIndexOutOfBoundsException() { assertThatExceptionOfType(StringIndexOutOfBoundsException.class).isThrownBy(() -> { final Charset charset = Charset.defaultCharset(); final ByteBuffer byteBuffer = charset.encode("end < start"); final CharBuffer charBuffer = charset.decode(byteBuffer); new TextStringBuilder(630).append(charBuffer, 0, 630); }); }
@Override public void write(int val) throws IOException { if (System.currentTimeMillis() > deadline) { throw new IOException("timed out trying to succeed"); } lineBuffer[counter] = (byte) val; counter++; if (val != 10) { return; } final String line = Charsets.UTF_8.decode( ByteBuffer.wrap(lineBuffer, 0, counter)).toString(); outputLines.add(line); counter = 0; if (line.contains(abbreviatedTestHost) && !line.contains("UNKNOWN")) { success.set(true); throw new IOException("output closed"); } } };