private Map<String, Object> buildStringKeyMap(ByteBuffer input) { int payloadSize = input.remaining(); if (chars == null || chars.remaining() < payloadSize) { chars = CharBuffer.allocate(payloadSize); } final CoderResult coderResult = charset.newDecoder() .onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE) .decode(input, chars, true); Map<String, Object> theMap; if (coderResult.isUnderflow()) { chars.flip(); try { theMap = parseString(chars.toString()); } finally { chars.clear(); } } else { throw new ParseException("Failed with CoderResult[%s]", coderResult); } return theMap; }
private byte[] toBytes(char[] chars) { CharBuffer charBuffer = CharBuffer.wrap(chars); ByteBuffer byteBuffer = Charset.forName("UTF-8").encode(charBuffer); byte[] bytes = Arrays.copyOfRange(byteBuffer.array(), byteBuffer.position(), byteBuffer.limit()); Arrays.fill(charBuffer.array(), '\u0000'); // clear sensitive data Arrays.fill(byteBuffer.array(), (byte) 0); // clear sensitive data return bytes; }
@Override public synchronized int read(CharBuffer target) throws IOException { checkNotNull(target); checkOpen(); if (!hasRemaining()) { return -1; } int charsToRead = Math.min(target.remaining(), remaining()); for (int i = 0; i < charsToRead; i++) { target.put(seq.charAt(pos++)); } return charsToRead; }
/** Returns a new CharBuffer identical to buf, except twice the capacity. */ private static CharBuffer grow(CharBuffer buf) { char[] copy = Arrays.copyOf(buf.array(), buf.capacity() * 2); CharBuffer bigger = CharBuffer.wrap(copy); bigger.position(buf.position()); bigger.limit(buf.limit()); return bigger; }
public String decode(String encodedFolderName) throws CharacterCodingException { CharsetDecoder decoder = modifiedUtf7Charset.newDecoder().onMalformedInput(CodingErrorAction.REPORT); ByteBuffer byteBuffer = ByteBuffer.wrap(encodedFolderName.getBytes(asciiCharset)); CharBuffer charBuffer = decoder.decode(byteBuffer); return charBuffer.toString(); } }
// Create the encoder and decoder for ISO-8859-1 Charset charset = Charset.forName("ISO-8859-1"); CharsetDecoder decoder = charset.newDecoder(); CharsetEncoder encoder = charset.newEncoder(); try { // Convert a string to ISO-LATIN-1 bytes in a ByteBuffer // The new ByteBuffer is ready to be read. ByteBuffer bbuf = encoder.encode(CharBuffer.wrap("a string")); // Convert ISO-LATIN-1 bytes in a ByteBuffer to a character ByteBuffer and then to a string. // The new ByteBuffer is ready to be read. CharBuffer cbuf = decoder.decode(bbuf); String s = cbuf.toString(); } catch (CharacterCodingException e) { }
public static int truncateUtf8(String input, byte[] output) { ByteBuffer outBuf = ByteBuffer.wrap(output); CharBuffer inBuf = CharBuffer.wrap(input.toCharArray()); Charset utf8 = Charset.forName("UTF-8"); utf8.newEncoder().encode(inBuf, outBuf, true); System.out.println("encoded " + inBuf.position() + " chars of " + input.length() + ", result: " + outBuf.position() + " bytes"); return outBuf.position(); }
/** * Fills the internal char buffer from the reader. * * @throws IOException * If an I/O error occurs */ private void fillBuffer() throws IOException { if (!endOfInput && (lastCoderResult == null || lastCoderResult.isUnderflow())) { encoderIn.compact(); final int position = encoderIn.position(); // We don't use Reader#read(CharBuffer) here because it is more efficient // to write directly to the underlying char array (the default implementation // copies data to a temporary char array). final int c = reader.read(encoderIn.array(), position, encoderIn.remaining()); if (c == EOF) { endOfInput = true; } else { encoderIn.position(position+c); } encoderIn.flip(); } encoderOut.compact(); lastCoderResult = encoder.encode(encoderIn, encoderOut, endOfInput); encoderOut.flip(); }
private void fillBuffer() throws IOException { int len = (int) Math.min(charBuffer.capacity() - charBuffer.position(), remaining); if (len > 0) { len = reader.read(charBuffer.array(), charBuffer.position(), len); } if (len > 0) { remaining -= len; } else { len = 0; remaining = 0; } length += len; charBuffer.limit(charBuffer.position() + len); charBuffer.rewind(); byteBuffer = ByteBuffer.allocate(Constants.IO_BUFFER_SIZE); boolean end = remaining == 0; encoder.encode(charBuffer, byteBuffer, end); if (end && byteBuffer.position() == 0) { // EOF byteBuffer = null; return; } byteBuffer.flip(); charBuffer.compact(); charBuffer.flip(); charBuffer.position(charBuffer.limit()); }
public void printFileOn (File file, PrintStream ps) { if (file.isFile()) { try { CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder(); ByteBuffer in = ByteBuffer.wrap(Files.readAllBytes(file.toPath())); char[] data = decoder.decode(in).array(); initialize(data); printOn(ps); } catch (Throwable t) { t.printStackTrace(); } } }
public String body() { prepareByteData(); // charset gets set from header on execute, and from meta-equiv on parse. parse may not have happened yet String body; if (charset == null) body = Charset.forName(DataUtil.defaultCharset).decode(byteData).toString(); else body = Charset.forName(charset).decode(byteData).toString(); ((Buffer)byteData).rewind(); // cast to avoid covariant return type change in jdk9 return body; }
private String readFile(String path) throws IOException { byte[] encoded = Files.readAllBytes(Paths.get(path)); return Charset.forName("UTF-8").decode(ByteBuffer.wrap(encoded)).toString(); } }
private void advance() throws IOException { assert !endOfInput; assert !bbuf.hasRemaining() : "advance() should be called when output byte buffer is empty. bbuf: " + bbuf + ", as string: " + bbuf.asCharBuffer().toString(); assert cbuf.remaining() < 2; // given that bbuf.capacity = 3 x cbuf.capacity, the only time that we should have a // remaining char is if the last char read was the 1st half of a surrogate pair if (cbuf.remaining() == 0) { cbuf.clear(); } else { cbuf.compact(); } int n = reader.read(cbuf); // read #1 cbuf.flip(); CoderResult result; endOfInput = n == -1; bbuf.clear(); result = encoder.encode(cbuf, bbuf, endOfInput); checkEncodeResult(result); if (endOfInput) { result = encoder.flush(bbuf); checkEncodeResult(result); } bbuf.flip(); }
private static String getTextFromFilePath(Path file) throws IOException { byte[] bytes = Files.readAllBytes(file); CharBuffer chars = Charset.defaultCharset().decode(ByteBuffer.wrap(bytes)); return chars.toString(); } }
/** Handle the case of underflow caused by needing more input characters. */ private void readMoreChars() throws IOException { // Possibilities: // 1) array has space available on right hand side (between limit and capacity) // 2) array has space available on left hand side (before position) // 3) array has no space available // // In case 2 we shift the existing chars to the left, and in case 3 we create a bigger // array, then they both become case 1. if (availableCapacity(charBuffer) == 0) { if (charBuffer.position() > 0) { // (2) There is room in the buffer. Move existing bytes to the beginning. charBuffer.compact().flip(); } else { // (3) Entire buffer is full, need bigger buffer. charBuffer = grow(charBuffer); } } // (1) Read more characters into free space at end of array. int limit = charBuffer.limit(); int numChars = reader.read(charBuffer.array(), limit, availableCapacity(charBuffer)); if (numChars == -1) { endOfInput = true; } else { charBuffer.limit(limit + numChars); } }
try { while (byteBuffer.hasRemaining()) { while (charBuffer.hasRemaining()) { final CoderResult result = encoder.encode(charBuffer, byteBuffer, false); if (result.isOverflow()) { return true; if (result.isUnderflow()) { filled = true; break; if (result.isError()) { if (result.isMalformed()) { throw msg.malformedInput(); charBuffer.compact(); try { final int cnt = reader.read(charBuffer); if (cnt == -1) { return filled; charBuffer.flip();
Assert.notNull(charset, "Charset must not be null"); if (charSequence.length() != 0) { CharsetEncoder charsetEncoder = charset.newEncoder() .onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE); CharBuffer inBuffer = CharBuffer.wrap(charSequence); int estimatedSize = (int) (inBuffer.remaining() * charsetEncoder.averageBytesPerChar()); ByteBuffer outBuffer = ensureCapacity(estimatedSize) .asByteBuffer(writePosition(), writableByteCount()); while (true) { CoderResult cr = (inBuffer.hasRemaining() ? charsetEncoder.encode(inBuffer, outBuffer, true) : CoderResult.UNDERFLOW); if (cr.isUnderflow()) { cr = charsetEncoder.flush(outBuffer); if (cr.isUnderflow()) { break; if (cr.isOverflow()) { writePosition(outBuffer.position()); int maximumSize = (int) (inBuffer.remaining() * charsetEncoder.maxBytesPerChar()); ensureCapacity(maximumSize); outBuffer = asByteBuffer(writePosition(), writableByteCount());