Package java.nio.charset

Examples of java.nio.charset.CharsetDecoder


            normalizeBlames(myBlameChunks, myMergeBlameChunks);
            mergedCount = 0;
        }
       
        inputEncoding = inputEncoding == null ? System.getProperty("file.encoding") : inputEncoding;
        CharsetDecoder decoder = Charset.forName(inputEncoding).newDecoder();

        InputStream stream = null;
        try {
            stream = new SVNTranslatorInputStream(SVNFileUtil.openFileForReading(myPreviousFile),
                                                  SVNProperty.EOL_LF_BYTES, true, null, false);
View Full Code Here


        Assert.assertNotSame(nioBuf, ByteBuffer.allocate(16).buf());
    }

    public void testGetString() throws Exception {
        ByteBuffer buf = ByteBuffer.allocate(16);
        CharsetDecoder decoder;

        Charset charset = Charset.forName("UTF-8");
        buf.clear();
        buf.putString("hello", charset.newEncoder());
        buf.put((byte) 0);
View Full Code Here

    }

    public void testGetPrefixedString() throws Exception {
        ByteBuffer buf = ByteBuffer.allocate(16);
        CharsetEncoder encoder;
        CharsetDecoder decoder;
        encoder = Charset.forName("ISO-8859-1").newEncoder();
        decoder = Charset.forName("ISO-8859-1").newDecoder();

        buf.putShort((short) 3);
        buf.putString("ABCD", encoder);
View Full Code Here

    // to the out ContentHandler, if we are in the body,
    // else appends the characters to the pendingBuffer
    private void pushBytes() throws IOException, SAXException, TikaException {
        if (pendingByteCount > 0 && (!groupState.ignore || nextMetaData != null)) {

            final CharsetDecoder decoder = getDecoder();
            pendingByteBuffer.limit(pendingByteCount);
            assert pendingByteBuffer.position() == 0;
            assert outputBuffer.position() == 0;

            while (true) {
                // We pass true for endOfInput because, when
                // we are called, we should have seen a
                // complete sequence of characters for this
                // charset:
                final CoderResult result = decoder.decode(pendingByteBuffer, outputBuffer, true);

                final int pos = outputBuffer.position();
                if (pos > 0) {
                    if (inHeader || fieldState == 1) {
                        pendingBuffer.append(outputArray, 0, pos);
                    } else {
                        lazyStartParagraph();
                        out.characters(outputArray, 0, pos);
                    }
                    outputBuffer.position(0);
                }

                if (result == CoderResult.UNDERFLOW) {
                    break;
                }
            }

            while (true) {
                final CoderResult result = decoder.flush(outputBuffer);

                final int pos = outputBuffer.position();
                if (pos > 0) {
                    if (inHeader || fieldState == 1) {
                        pendingBuffer.append(outputArray, 0, pos);
                    } else {
                        lazyStartParagraph();
                        out.characters(outputArray, 0, pos);
                    }
                    outputBuffer.position(0);
                }

                if (result == CoderResult.UNDERFLOW) {
                    break;
                }
            }

            // Reset for next decode
            decoder.reset();
            pendingByteBuffer.position(0);
        }

        pendingByteCount = 0;
    }
View Full Code Here

                    in.limit(posEnd);
                }

                try {
                    // convert to string using the charset decoder
                    CharsetDecoder decoder = (CharsetDecoder)session.getAttribute(CHARSET_DECODER);
                    if (decoder == null) {
                        decoder = charset.newDecoder();
                        session.setAttribute(CHARSET_DECODER, decoder);
                    }
                    String body = in.getString(decoder);
View Full Code Here

        InputStreamReader isr2 = new InputStreamReader(new ByteArrayInputStream(
                new byte[] { -32, -96 }), Charset.forName("UTF-8"));
        assertEquals("read() return incorrect value", 65533, isr2.read());
       
        // if the decoder is passed in, keep its status intacted
        CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
        decoder.onMalformedInput(CodingErrorAction.REPORT);
        InputStreamReader isr3 = new InputStreamReader(new ByteArrayInputStream(
                new byte[] { -32, -96 }), decoder);
        try{
           isr3.read();
           fail("Should throw MalformedInputException");
        }catch(MalformedInputException e){
            //expected
        }
       
        CharsetDecoder decoder2 = Charset.forName("UTF-8").newDecoder();
        decoder2.onMalformedInput(CodingErrorAction.IGNORE);
        InputStreamReader isr4 = new InputStreamReader(new ByteArrayInputStream(
                new byte[] { -32, -96 }), decoder2);
        assertEquals("read() return incorrect value", -1, isr4.read());
       
        CharsetDecoder decoder3 = Charset.forName("UTF-8").newDecoder();
        decoder3.onMalformedInput(CodingErrorAction.REPLACE);
        InputStreamReader isr5 = new InputStreamReader(new ByteArrayInputStream(
                new byte[] { -32, -96 }), decoder3);
        assertEquals("read() return incorrect value", 65533, isr5.read());
    }
View Full Code Here

  /*
   * Class under test for void InputStreamReader(InputStream, CharsetDecoder)
   */
  public void testInputStreamReaderInputStreamCharsetDecoder()
      throws Exception {
    CharsetDecoder decoder = Charset.forName("utf-8").newDecoder();
    try {
      reader = new InputStreamReader(null, decoder);
      fail();
    } catch (NullPointerException e) {
    }
    try {
      reader = new InputStreamReader(in, (CharsetDecoder) null);
      fail();
    } catch (NullPointerException e) {
    }
        InputStreamReader reader2 = new InputStreamReader(in, decoder);
    assertEquals(Charset.forName(reader2.getEncoding()), decoder.charset());
    reader2.close();
  }
View Full Code Here

    }
   
    public void testGetString() throws Exception
    {
        ByteBuffer buf = ByteBuffer.allocate( 16 );
        CharsetDecoder decoder;

        decoder = Charset.forName( "ISO-8859-1" ).newDecoder();
        buf.put( (byte) 'A' );
        buf.put( (byte) 'B' );
        buf.put( (byte) 'C' );
View Full Code Here

     * @param encoding Encoding
     * @throws IOException If some output failed
     */
    public void writeTo(Writer out, String encoding) throws IOException {
        if (encoding != null) {
            CharsetDecoder decoder = getDecoder(encoding);
            // Create buffer for characters decoding
            CharBuffer charBuffer = CharBuffer.allocate(buffer.length);
            // Create buffer for bytes
            float bytesPerChar = decoder.charset().newEncoder().maxBytesPerChar();
            ByteBuffer byteBuffer = ByteBuffer.allocate((int) (buffer.length + bytesPerChar));
            if (buffers != null) {
                for (byte[] bytes : buffers) {
                    decodeAndWriteOut(out, bytes, bytes.length, byteBuffer, charBuffer, decoder, false);
                }
View Full Code Here

    }

    @Test
    public void testGetString() throws Exception {
        IoBuffer buf = IoBuffer.allocate(16);
        CharsetDecoder decoder;

        Charset charset = Charset.forName("UTF-8");
        buf.clear();
        buf.putString("hello", charset.newEncoder());
        buf.put((byte) 0);
View Full Code Here

TOP

Related Classes of java.nio.charset.CharsetDecoder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.