Java Code Examples for java.nio.charset.CoderResult#isError()

The following examples show how to use java.nio.charset.CoderResult#isError() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XmlUtils.java    From JobSchedulerCompat with MIT License 6 votes vote down vote up
public void flush() throws IOException {
    if (pos > 0) {
        if (out != null) {
            CharBuffer charBuffer = CharBuffer.wrap(text, 0, pos);
            CoderResult result = charset.encode(charBuffer, bytes, true);
            while (true) {
                if (result.isError()) {
                    throw new IOException(result.toString());
                } else if (result.isOverflow()) {
                    flushBytes();
                    result = charset.encode(charBuffer, bytes, true);
                    continue;
                }
                break;
            }
            flushBytes();
            out.flush();
        } else {
            writer.write(text, 0, pos);
            writer.flush();
        }
        pos = 0;
    }
}
 
Example 2
Source File: FastXmlSerializer.java    From MyBookshelf with GNU General Public License v3.0 6 votes vote down vote up
public void flush() throws IOException {
    //Log.i("PackageManager", "flush mPos=" + mPos);
    if (mPos > 0) {
        if (mOutputStream != null) {
            CharBuffer charBuffer = CharBuffer.wrap(mText, 0, mPos);
            CoderResult result = mCharset.encode(charBuffer, mBytes, true);
            while (true) {
                if (result.isError()) {
                    throw new IOException(result.toString());
                } else if (result.isOverflow()) {
                    flushBytes();
                    result = mCharset.encode(charBuffer, mBytes, true);
                    continue;
                }
                break;
            }
            flushBytes();
            mOutputStream.flush();
        } else {
            mWriter.write(mText, 0, mPos);
            mWriter.flush();
        }
        mPos = 0;
    }
}
 
Example 3
Source File: UnicodeHelper.java    From p4ic4idea with Apache License 2.0 6 votes vote down vote up
/**
 * Try to determine whether a byte buffer's character encoding is that of the
 * passed-in charset. Uses inefficient
 * heuristics that will be revisited when we're more familiar with likely
 * usage patterns.
 * 
 * Note this has been heavily changed since inception and will
 * almost certainly disappear in the 10.x timeframe -- HR.
 */
public static boolean inferCharset(byte[] bytes, int bytesRead, Charset clientCharset) {
	ByteBuffer byteBuf = ByteBuffer.wrap(bytes, 0, bytesRead);
	CharBuffer charBuf = CharBuffer.allocate(byteBuf.capacity() * 2);
	
	if (clientCharset != null) {
		CharsetDecoder decoder = clientCharset.newDecoder();
		decoder.onMalformedInput(CodingErrorAction.REPORT);
		decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
		CoderResult coderResult = decoder.decode(byteBuf, charBuf, false);
		if (coderResult != null) {
			if (coderResult.isError()) {
				// Wasn't this one...
				return false;
			} else {
				return true;	// Still only *probably* true, dammit...
			}
		}
	}
	
	return true;
}
 
Example 4
Source File: FastXmlSerializer.java    From container with GNU General Public License v3.0 6 votes vote down vote up
public void flush() throws IOException {
    //Log.i("PackageManager", "flush mPos=" + mPos);
    if (mPos > 0) {
        if (mOutputStream != null) {
            CharBuffer charBuffer = CharBuffer.wrap(mText, 0, mPos);
            CoderResult result = mCharset.encode(charBuffer, mBytes, true);
            while (true) {
                if (result.isError()) {
                    throw new IOException(result.toString());
                } else if (result.isOverflow()) {
                    flushBytes();
                    result = mCharset.encode(charBuffer, mBytes, true);
                    continue;
                }
                break;
            }
            flushBytes();
            mOutputStream.flush();
        } else {
            mWriter.write(mText, 0, mPos);
            mWriter.flush();
        }
        mPos = 0;
    }
}
 
Example 5
Source File: BshScriptEngine.java    From beanshell with Apache License 2.0 6 votes vote down vote up
@Override
public void write(byte[] buffer, int offset, int length) throws IOException {
    while (length > 0) {
        final int done = Math.min(length, this.input.remaining());
        this.input.put(buffer, offset, done);
        this.input.flip();
        while (true) {
            final CoderResult result = this.decoder.decode(
                this.input, this.output, false);
            if (result.isError())
                result.throwException();
            this.writer.write(
                this.output.array(), 0, this.output.position()
            );
            this.writer.flush();
            this.output.rewind();
            if (result.isUnderflow())
                break;
        }
        this.input.compact();
        offset += done;
        length -= done;
    }
}
 
Example 6
Source File: CharSequenceWrapper.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Decode the buffer using the CharsetDecoder.
 * @param byteBuf
 * @return false if failed because the charbuffer was not big enough
 * @throws RuntimeException if it fails for encoding errors
 */
private boolean decodeUT8(ByteBuffer byteBuf) {
  // We give it all of the input data in call.
  boolean endOfInput = true;
  decoder.reset();
  charBuffer.rewind();
  // Convert utf-8 bytes to sequence of chars
  CoderResult result = decoder.decode(byteBuf, charBuffer, endOfInput);
  if (result.isOverflow()) {
    // Not enough space in the charBuffer.
    return false;
  } else if (result.isError()) {
    // Any other error
    try {
      result.throwException();
    } catch (CharacterCodingException e) {
      throw new RuntimeException(e);
    }
  }
  return true;
}
 
Example 7
Source File: FastXmlSerializer.java    From AcDisplay with GNU General Public License v2.0 6 votes vote down vote up
public void flush() throws IOException {
    //Log.i("PackageManager", "flush mPos=" + mPos);
    if (mPos > 0) {
        if (mOutputStream != null) {
            CharBuffer charBuffer = CharBuffer.wrap(mText, 0, mPos);
            CoderResult result = mCharset.encode(charBuffer, mBytes, true);
            while (true) {
                if (result.isError()) {
                    throw new IOException(result.toString());
                } else if (result.isOverflow()) {
                    flushBytes();
                    result = mCharset.encode(charBuffer, mBytes, true);
                    continue;
                }
                break;
            }
            flushBytes();
            mOutputStream.flush();
        } else {
            mWriter.write(mText, 0, mPos);
            mWriter.flush();
        }
        mPos = 0;
    }
}
 
Example 8
Source File: UnicodeHelper.java    From p4ic4idea with Apache License 2.0 6 votes vote down vote up
/**
 * Try to determine whether a byte buffer's character encoding is that of the
 * passed-in charset. Uses inefficient
 * heuristics that will be revisited when we're more familiar with likely
 * usage patterns.
 * 
 * Note this has been heavily changed since inception and will
 * almost certainly disappear in the 10.x timeframe -- HR.
 */
public static boolean inferCharset(byte[] bytes, int bytesRead, Charset clientCharset) {
	ByteBuffer byteBuf = ByteBuffer.wrap(bytes, 0, bytesRead);
	CharBuffer charBuf = CharBuffer.allocate(byteBuf.capacity() * 2);
	
	if (clientCharset != null) {
		CharsetDecoder decoder = clientCharset.newDecoder();
		decoder.onMalformedInput(CodingErrorAction.REPORT);
		decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
		CoderResult coderResult = decoder.decode(byteBuf, charBuf, false);
		if (coderResult != null) {
			if (coderResult.isError()) {
				// Wasn't this one...
				return false;
			} else {
				return true;	// Still only *probably* true, dammit...
			}
		}
	}
	
	return true;
}
 
Example 9
Source File: ContentHandlerBase64Encoder.java    From edireader with GNU General Public License v3.0 5 votes vote down vote up
public void encode(char[] dataObject, ContentHandler contentHandler) {
    this.contentHandler = contentHandler;
    CharBuffer charBuffer = CharBuffer.wrap(dataObject);

    // Allocate a modest sized non-direct ByteBuffer to receive the
    // bytes as they are encoded from the chars
    ByteBuffer byteBuffer = ByteBuffer.allocate(100);

    // Allocate a similar ByteBuffer to receive the bytes as they are emitted
    // by the Base 64 encoder.
    base64Bytes = ByteBuffer.allocate(100);

    // Use an encoder repeatedly until all of the chars have been encoded as bytes
    // and presented as input for base 64 encoding.
    CharsetEncoder encoder = charset.newEncoder();
    while (true) {
        CoderResult coderResult = encoder.encode(charBuffer, byteBuffer, true);
        if (coderResult.isError())
            throw new RuntimeException("Unrecoverable failure in Base64 encoding");
        ((Buffer) byteBuffer).flip();
        while (byteBuffer.hasRemaining())
            consume(byteBuffer.get());
        if (coderResult.isUnderflow()) break;
        ((Buffer) byteBuffer).clear();
    }
    endOfData();
    feedContentHandler();
}
 
Example 10
Source File: MboxIterator.java    From sling-samples with Apache License 2.0 5 votes vote down vote up
private void decodeNextCharBuffer() throws CharConversionException {
    CoderResult coderResult = DECODER.decode(byteBuffer, mboxCharBuffer, endOfInputFlag);
    updateEndOfInputFlag();
    mboxCharBuffer.flip();
    if (coderResult.isError()) {
        if (coderResult.isMalformed()) {
            throw new CharConversionException("Malformed input!");
        } else if (coderResult.isUnmappable()) {
            throw new CharConversionException("Unmappable character!");
        }
    }
}
 
Example 11
Source File: B2CConverter.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
/**
 * Convert the given bytes to characters.
 * 
 * @param bc byte input
 * @param cc char output
 * @param endOfInput    Is this all of the available data
 */
public void convert(ByteChunk bc, CharChunk cc, boolean endOfInput)
        throws IOException {
    if ((bb == null) || (bb.array() != bc.getBuffer())) {
        // Create a new byte buffer if anything changed
        bb = ByteBuffer.wrap(bc.getBuffer(), bc.getStart(), bc.getLength());
    } else {
        // Initialize the byte buffer
        bb.limit(bc.getEnd());
        bb.position(bc.getStart());
    }
    if ((cb == null) || (cb.array() != cc.getBuffer())) {
        // Create a new char buffer if anything changed
        cb = CharBuffer.wrap(cc.getBuffer(), cc.getEnd(), 
                cc.getBuffer().length - cc.getEnd());
    } else {
        // Initialize the char buffer
        cb.limit(cc.getBuffer().length);
        cb.position(cc.getEnd());
    }
    CoderResult result = null;
    // Parse leftover if any are present
    if (leftovers.position() > 0) {
        int pos = cb.position();
        // Loop until one char is decoded or there is a decoder error
        do {
            leftovers.put(bc.substractB());
            leftovers.flip();
            result = decoder.decode(leftovers, cb, endOfInput);
            leftovers.position(leftovers.limit());
            leftovers.limit(leftovers.array().length);
        } while (result.isUnderflow() && (cb.position() == pos));
        if (result.isError() || result.isMalformed()) {
            result.throwException();
        }
        bb.position(bc.getStart());
        leftovers.position(0);
    }
    // Do the decoding and get the results into the byte chunk and the char
    // chunk
    result = decoder.decode(bb, cb, endOfInput);
    if (result.isError() || result.isMalformed()) {
        result.throwException();
    } else if (result.isOverflow()) {
        // Propagate current positions to the byte chunk and char chunk, if
        // this continues the char buffer will get resized
        bc.setOffset(bb.position());
        cc.setEnd(cb.position());
    } else if (result.isUnderflow()) {
        // Propagate current positions to the byte chunk and char chunk
        bc.setOffset(bb.position());
        cc.setEnd(cb.position());
        // Put leftovers in the leftovers byte buffer
        if (bc.getLength() > 0) {
            leftovers.limit(leftovers.array().length);
            leftovers.position(bc.getLength());
            bc.substract(leftovers.array(), 0, bc.getLength());
        }
    }
}
 
Example 12
Source File: LocaleUtilDecoderReal.java    From BiglyBT with GNU General Public License v2.0 4 votes vote down vote up
@Override
public String decodeString(byte[] bytes) {
	if ( bytes == null ){

		return( null );
	}

	try{
		ByteBuffer bb = ByteBuffer.wrap(bytes);

		CharBuffer cb = CharBuffer.allocate(bytes.length);

		CoderResult cr;
		this_mon.enter();
		try {
			cr = decoder.decode(bb, cb, true);
		} finally {
			this_mon.exit();
		}

		if ( !cr.isError() ){

			cb.flip();

			String	str = cb.toString();

			byte[]	b2 = str.getBytes(decoder.charset().name());

				// make sure the conversion is symetric (there are cases where it appears
				// to work but in fact converting back to bytes leads to a different
				// result

			/*
			for (int k=0;k<str.length();k++){
				System.out.print( Integer.toHexString(str.charAt(k)));
			}
			System.out.println("");
			*/

			if ( Arrays.equals( bytes, b2 )){

				return( str );
			}
		}
	}catch( Throwable e ){

		// Throwable here as we can get "classdefnotfound" + others if the decoder
		// isn't available

		// ignore
	}

	// no joy, default
	return new String(bytes, Constants.DEFAULT_ENCODING_CHARSET);
}
 
Example 13
Source File: JISAutoDetect.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {
    if (detectedDecoder == null) {
        copyLeadingASCII(src, dst);

        // All ASCII?
        if (! src.hasRemaining())
            return CoderResult.UNDERFLOW;
        if (! dst.hasRemaining())
            return CoderResult.OVERFLOW;

        // We need to perform double, not float, arithmetic; otherwise
        // we lose low order bits when src is larger than 2**24.
        int cbufsiz = (int)(src.limit() * (double)maxCharsPerByte());
        CharBuffer sandbox = CharBuffer.allocate(cbufsiz);

        // First try ISO-2022-JP, since there is no ambiguity
        Charset cs2022 = Charset.forName("ISO-2022-JP");
        DelegatableDecoder dd2022
            = (DelegatableDecoder) cs2022.newDecoder();
        ByteBuffer src2022 = src.asReadOnlyBuffer();
        CoderResult res2022 = dd2022.decodeLoop(src2022, sandbox);
        if (! res2022.isError())
            return decodeLoop(cs2022, src, dst);

        // We must choose between EUC and SJIS
        Charset csEUCJ = Charset.forName(EUCJPName);
        Charset csSJIS = Charset.forName(SJISName);

        DelegatableDecoder ddEUCJ
            = (DelegatableDecoder) csEUCJ.newDecoder();
        ByteBuffer srcEUCJ = src.asReadOnlyBuffer();
        sandbox.clear();
        CoderResult resEUCJ = ddEUCJ.decodeLoop(srcEUCJ, sandbox);
        // If EUC decoding fails, must be SJIS
        if (resEUCJ.isError())
            return decodeLoop(csSJIS, src, dst);

        DelegatableDecoder ddSJIS
            = (DelegatableDecoder) csSJIS.newDecoder();
        ByteBuffer srcSJIS = src.asReadOnlyBuffer();
        CharBuffer sandboxSJIS = CharBuffer.allocate(cbufsiz);
        CoderResult resSJIS = ddSJIS.decodeLoop(srcSJIS, sandboxSJIS);
        // If SJIS decoding fails, must be EUC
        if (resSJIS.isError())
            return decodeLoop(csEUCJ, src, dst);

        // From here on, we have some ambiguity, and must guess.

        // We prefer input that does not appear to end mid-character.
        if (srcEUCJ.position() > srcSJIS.position())
            return decodeLoop(csEUCJ, src, dst);

        if (srcEUCJ.position() < srcSJIS.position())
            return decodeLoop(csSJIS, src, dst);

        // end-of-input is after the first byte of the first char?
        if (src.position() == srcEUCJ.position())
            return CoderResult.UNDERFLOW;

        // Use heuristic knowledge of typical Japanese text
        sandbox.flip();
        Charset guess = looksLikeJapanese(sandbox) ? csEUCJ : csSJIS;
        return decodeLoop(guess, src, dst);
    }

    return detectedDecoder.decodeLoop(src, dst);
}
 
Example 14
Source File: JISAutoDetect.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {
    if (detectedDecoder == null) {
        copyLeadingASCII(src, dst);

        // All ASCII?
        if (! src.hasRemaining())
            return CoderResult.UNDERFLOW;
        if (! dst.hasRemaining())
            return CoderResult.OVERFLOW;

        // We need to perform double, not float, arithmetic; otherwise
        // we lose low order bits when src is larger than 2**24.
        int cbufsiz = (int)(src.limit() * (double)maxCharsPerByte());
        CharBuffer sandbox = CharBuffer.allocate(cbufsiz);

        // First try ISO-2022-JP, since there is no ambiguity
        Charset cs2022 = Charset.forName("ISO-2022-JP");
        DelegatableDecoder dd2022
            = (DelegatableDecoder) cs2022.newDecoder();
        ByteBuffer src2022 = src.asReadOnlyBuffer();
        CoderResult res2022 = dd2022.decodeLoop(src2022, sandbox);
        if (! res2022.isError())
            return decodeLoop(cs2022, src, dst);

        // We must choose between EUC and SJIS
        Charset csEUCJ = Charset.forName(EUCJPName);
        Charset csSJIS = Charset.forName(SJISName);

        DelegatableDecoder ddEUCJ
            = (DelegatableDecoder) csEUCJ.newDecoder();
        ByteBuffer srcEUCJ = src.asReadOnlyBuffer();
        sandbox.clear();
        CoderResult resEUCJ = ddEUCJ.decodeLoop(srcEUCJ, sandbox);
        // If EUC decoding fails, must be SJIS
        if (resEUCJ.isError())
            return decodeLoop(csSJIS, src, dst);

        DelegatableDecoder ddSJIS
            = (DelegatableDecoder) csSJIS.newDecoder();
        ByteBuffer srcSJIS = src.asReadOnlyBuffer();
        CharBuffer sandboxSJIS = CharBuffer.allocate(cbufsiz);
        CoderResult resSJIS = ddSJIS.decodeLoop(srcSJIS, sandboxSJIS);
        // If SJIS decoding fails, must be EUC
        if (resSJIS.isError())
            return decodeLoop(csEUCJ, src, dst);

        // From here on, we have some ambiguity, and must guess.

        // We prefer input that does not appear to end mid-character.
        if (srcEUCJ.position() > srcSJIS.position())
            return decodeLoop(csEUCJ, src, dst);

        if (srcEUCJ.position() < srcSJIS.position())
            return decodeLoop(csSJIS, src, dst);

        // end-of-input is after the first byte of the first char?
        if (src.position() == srcEUCJ.position())
            return CoderResult.UNDERFLOW;

        // Use heuristic knowledge of typical Japanese text
        sandbox.flip();
        Charset guess = looksLikeJapanese(sandbox) ? csEUCJ : csSJIS;
        return decodeLoop(guess, src, dst);
    }

    return detectedDecoder.decodeLoop(src, dst);
}
 
Example 15
Source File: ReaderInputStream.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public int read(byte[] b, int off, int len) throws IOException {
  // Obey InputStream contract.
  checkPositionIndexes(off, off + len, b.length);
  if (len == 0) {
    return 0;
  }

  // The rest of this method implements the process described by the CharsetEncoder javadoc.
  int totalBytesRead = 0;
  boolean doneEncoding = endOfInput;
  DRAINING:
  while (true) {
    // We stay in draining mode until there are no bytes left in the output buffer. Then we go
    // back to encoding/flushing.
    if (draining) {
      totalBytesRead += drain(b, off + totalBytesRead, len - totalBytesRead);
      if (totalBytesRead == len || doneFlushing) {
        return (totalBytesRead > 0) ? totalBytesRead : -1;
      }
      draining = false;
      byteBuffer.clear();
    }

    while (true) {
      // We call encode until there is no more input. The last call to encode will have endOfInput
      // == true. Then there is a final call to flush.
      CoderResult result;
      if (doneFlushing) {
        result = CoderResult.UNDERFLOW;
      } else if (doneEncoding) {
        result = encoder.flush(byteBuffer);
      } else {
        result = encoder.encode(charBuffer, byteBuffer, endOfInput);
      }
      if (result.isOverflow()) {
        // Not enough room in output buffer--drain it, creating a bigger buffer if necessary.
        startDraining(true);
        continue DRAINING;
      } else if (result.isUnderflow()) {
        // If encoder underflows, it means either:
        // a) the final flush() succeeded; next drain (then done)
        // b) we encoded all of the input; next flush
        // c) we ran of out input to encode; next read more input
        if (doneEncoding) { // (a)
          doneFlushing = true;
          startDraining(false);
          continue DRAINING;
        } else if (endOfInput) { // (b)
          doneEncoding = true;
        } else { // (c)
          readMoreChars();
        }
      } else if (result.isError()) {
        // Only reach here if a CharsetEncoder with non-REPLACE settings is used.
        result.throwException();
        return 0; // Not called.
      }
    }
  }
}
 
Example 16
Source File: WsFrameBase.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
private boolean processDataControl() throws IOException {
    TransformationResult tr = transformation.getMoreData(opCode, fin, rsv, controlBufferBinary);
    if (TransformationResult.UNDERFLOW.equals(tr)) {
        return false;
    }
    // Control messages have fixed message size so
    // TransformationResult.OVERFLOW is not possible here

    controlBufferBinary.flip();
    if (opCode == Constants.OPCODE_CLOSE) {
        open = false;
        String reason = null;
        int code = CloseCodes.NORMAL_CLOSURE.getCode();
        if (controlBufferBinary.remaining() == 1) {
            controlBufferBinary.clear();
            // Payload must be zero or 2+ bytes long
            throw new WsIOException(new CloseReason(
                    CloseCodes.PROTOCOL_ERROR,
                    sm.getString("wsFrame.oneByteCloseCode")));
        }
        if (controlBufferBinary.remaining() > 1) {
            code = controlBufferBinary.getShort();
            if (controlBufferBinary.remaining() > 0) {
                CoderResult cr = utf8DecoderControl.decode(controlBufferBinary,
                        controlBufferText, true);
                if (cr.isError()) {
                    controlBufferBinary.clear();
                    controlBufferText.clear();
                    throw new WsIOException(new CloseReason(
                            CloseCodes.PROTOCOL_ERROR,
                            sm.getString("wsFrame.invalidUtf8Close")));
                }
                // There will be no overflow as the output buffer is big
                // enough. There will be no underflow as all the data is
                // passed to the decoder in a single call.
                controlBufferText.flip();
                reason = controlBufferText.toString();
            }
        }
        wsSession.onClose(new CloseReason(Util.getCloseCode(code), reason));
    } else if (opCode == Constants.OPCODE_PING) {
        if (wsSession.isOpen()) {
            wsSession.getBasicRemote().sendPong(controlBufferBinary);
        }
    } else if (opCode == Constants.OPCODE_PONG) {
        MessageHandler.Whole<PongMessage> mhPong = wsSession.getPongMessageHandler();
        if (mhPong != null) {
            try {
                mhPong.onMessage(new WsPongMessage(controlBufferBinary));
            } catch (Throwable t) {
                handleThrowableOnSend(t);
            } finally {
                controlBufferBinary.clear();
            }
        }
    } else {
        // Should have caught this earlier but just in case...
        controlBufferBinary.clear();
        throw new WsIOException(new CloseReason(
                CloseCodes.PROTOCOL_ERROR,
                sm.getString("wsFrame.invalidOpCode", Integer.valueOf(opCode))));
    }
    controlBufferBinary.clear();
    newFrame();
    return true;
}
 
Example 17
Source File: C2BConverter.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
/**
 * Convert the given characters to bytes.
 *
 * @param cc char input
 * @param bc byte output
 * @throws IOException An encoding error occurred
 */
public void convert(CharBuffer cc, ByteBuffer bc) throws IOException {
    if ((bb == null) || (bb.array() != bc.array())) {
        // Create a new byte buffer if anything changed
        bb = ByteBuffer.wrap(bc.array(), bc.limit(), bc.capacity() - bc.limit());
    } else {
        // Initialize the byte buffer
        bb.limit(bc.capacity());
        bb.position(bc.limit());
    }
    if ((cb == null) || (cb.array() != cc.array())) {
        // Create a new char buffer if anything changed
        cb = CharBuffer.wrap(cc.array(), cc.arrayOffset() + cc.position(), cc.remaining());
    } else {
        // Initialize the char buffer
        cb.limit(cc.limit());
        cb.position(cc.position());
    }
    CoderResult result = null;
    // Parse leftover if any are present
    if (leftovers.position() > 0) {
        int pos = bb.position();
        // Loop until one char is encoded or there is a encoder error
        do {
            leftovers.put(cc.get());
            leftovers.flip();
            result = encoder.encode(leftovers, bb, false);
            leftovers.position(leftovers.limit());
            leftovers.limit(leftovers.array().length);
        } while (result.isUnderflow() && (bb.position() == pos));
        if (result.isError() || result.isMalformed()) {
            result.throwException();
        }
        cb.position(cc.position());
        leftovers.position(0);
    }
    // Do the decoding and get the results into the byte chunk and the char
    // chunk
    result = encoder.encode(cb, bb, false);
    if (result.isError() || result.isMalformed()) {
        result.throwException();
    } else if (result.isOverflow()) {
        // Propagate current positions to the byte chunk and char chunk
        bc.limit(bb.position());
        cc.position(cb.position());
    } else if (result.isUnderflow()) {
        // Propagate current positions to the byte chunk and char chunk
        bc.limit(bb.position());
        cc.position(cb.position());
        // Put leftovers in the leftovers char buffer
        if (cc.remaining() > 0) {
            leftovers.limit(leftovers.array().length);
            leftovers.position(cc.remaining());
            cc.get(leftovers.array(), 0, cc.remaining());
        }
    }
}
 
Example 18
Source File: TextEncoder.java    From yajsync with GNU General Public License v3.0 4 votes vote down vote up
/**
 * @throws TextConversionException 
 */
private byte[] encode(CharBuffer input,
                      ErrorPolicy errorPolicy,
                      MemoryPolicy memoryPolicy)
{
    _encoder.reset();
    ByteBuffer output = ByteBuffer.allocate(
                            (int) Math.ceil(input.capacity() *
                                            _encoder.averageBytesPerChar()));
    try {
        CoderResult result;
        while (true) {
            result = _encoder.encode(input, output, true);
            if (result.isOverflow()) {
                output = Util.enlargeByteBuffer(output, memoryPolicy,
                                                Consts.MAX_BUF_SIZE);
            } else {
                break;
            }
        }

        while (!result.isError()) {
            result = _encoder.flush(output);
            if (result.isOverflow()) {
                output = Util.enlargeByteBuffer(output, memoryPolicy,
                                                Consts.MAX_BUF_SIZE);
            } else {
                break;
            }
        }

        if (result.isUnderflow()) {
            return Arrays.copyOfRange(output.array(),
                                      output.arrayOffset(),
                                      output.position());
        }

        if (errorPolicy == ErrorPolicy.THROW) { // NOTE: in some circumstances we should avoid printing the contents
            input.limit(input.position() + result.length());
            throw new TextConversionException(String.format(
                "failed to encode %d bytes after %s (using %s): %s -> %s",
                result.length(), output.flip().toString(),
                _encoder.charset(), Text.charBufferToString(input),
                result));
        }
        return null;
    } catch (OverflowException e) {
        if (errorPolicy == ErrorPolicy.THROW) {
            throw new TextConversionException(e);
        }
        return null;
    } finally {
        if (memoryPolicy == MemoryPolicy.ZERO) {
            Util.zeroByteBuffer(output);
        }
    }
}
 
Example 19
Source File: LocaleUtilDecoderReal.java    From BiglyBT with GNU General Public License v2.0 4 votes vote down vote up
@Override
public String
tryDecode(
	byte[]		array,
	boolean		lax )
{
	try{
		ByteBuffer bb = ByteBuffer.wrap(array);

		CharBuffer cb = CharBuffer.allocate(array.length);

		CoderResult cr;
		this_mon.enter();
		try {
			cr = decoder.decode(bb, cb, true);
		} finally {
			this_mon.exit();
		}

		if ( !cr.isError() ){

			cb.flip();

			String	str = cb.toString();

				// lax means that as long as the conversion works we consider it usable
				// as opposed to strict which requires reverse-conversion equivalence

			if ( lax ){

				return( str );
			}

			byte[]	b2 = str.getBytes( getName() );

				// make sure the conversion is symetric (there are cases where it appears
				// to work but in fact converting back to bytes leads to a different
				// result

			/*
			for (int k=0;k<str.length();k++){
				System.out.print( Integer.toHexString(str.charAt(k)));
			}
			System.out.println("");
			*/

			if ( Arrays.equals( array, b2 )){

				return( str );
			}
		}

		return( null );

	}catch( Throwable e ){

			// Throwable here as we can get "classdefnotfound" + others if the decoder
			// isn't available

		return( null );
	}
}
 
Example 20
Source File: ReaderInputStream.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public int read(byte[] b, int off, int len) throws IOException {
  // Obey InputStream contract.
  checkPositionIndexes(off, off + len, b.length);
  if (len == 0) {
    return 0;
  }

  // The rest of this method implements the process described by the CharsetEncoder javadoc.
  int totalBytesRead = 0;
  boolean doneEncoding = endOfInput;

  DRAINING:
  while (true) {
    // We stay in draining mode until there are no bytes left in the output buffer. Then we go
    // back to encoding/flushing.
    if (draining) {
      totalBytesRead += drain(b, off + totalBytesRead, len - totalBytesRead);
      if (totalBytesRead == len || doneFlushing) {
        return (totalBytesRead > 0) ? totalBytesRead : -1;
      }
      draining = false;
      byteBuffer.clear();
    }

    while (true) {
      // We call encode until there is no more input. The last call to encode will have endOfInput
      // == true. Then there is a final call to flush.
      CoderResult result;
      if (doneFlushing) {
        result = CoderResult.UNDERFLOW;
      } else if (doneEncoding) {
        result = encoder.flush(byteBuffer);
      } else {
        result = encoder.encode(charBuffer, byteBuffer, endOfInput);
      }

      if (result.isOverflow()) {
        // Not enough room in output buffer--drain it, creating a bigger buffer if necessary.
        startDraining(true);
        continue DRAINING;
      } else if (result.isUnderflow()) {
        // If encoder underflows, it means either:
        // a) the final flush() succeeded; next drain (then done)
        // b) we encoded all of the input; next flush
        // c) we ran of out input to encode; next read more input
        if (doneEncoding) { // (a)
          doneFlushing = true;
          startDraining(false);
          continue DRAINING;
        } else if (endOfInput) { // (b)
          doneEncoding = true;
        } else { // (c)
          readMoreChars();
        }
      } else if (result.isError()) {
        // Only reach here if a CharsetEncoder with non-REPLACE settings is used.
        result.throwException();
        return 0; // Not called.
      }
    }
  }
}