Java Code Examples for java.nio.charset.CharsetEncoder#reset()

The following examples show how to use java.nio.charset.CharsetEncoder#reset() . 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: StrictJarManifest.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private static void writeEntry(OutputStream os, Attributes.Name name,
        String value, CharsetEncoder encoder, ByteBuffer bBuf) throws IOException {
    String nameString = name.toString();
    os.write(nameString.getBytes(StandardCharsets.US_ASCII));
    os.write(VALUE_SEPARATOR);

    encoder.reset();
    bBuf.clear().limit(LINE_LENGTH_LIMIT - nameString.length() - 2);

    CharBuffer cBuf = CharBuffer.wrap(value);

    while (true) {
        CoderResult r = encoder.encode(cBuf, bBuf, true);
        if (CoderResult.UNDERFLOW == r) {
            r = encoder.flush(bBuf);
        }
        os.write(bBuf.array(), bBuf.arrayOffset(), bBuf.position());
        os.write(LINE_SEPARATOR);
        if (CoderResult.UNDERFLOW == r) {
            break;
        }
        os.write(' ');
        bBuf.clear().limit(LINE_LENGTH_LIMIT - 1);
    }
}
 
Example 2
Source File: CharsetUtil.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a cached thread-local {@link CharsetEncoder} for the specified
 * <tt>charset</tt>.
 */
public static CharsetEncoder getEncoder(Charset charset) {
    if (charset == null) {
        throw new NullPointerException("charset");
    }

    Map<Charset, CharsetEncoder> map = InternalThreadLocalMap.get().charsetEncoderCache();
    CharsetEncoder e = map.get(charset);
    if (e != null) {
        e.reset();
        e.onMalformedInput(CodingErrorAction.REPLACE);
        e.onUnmappableCharacter(CodingErrorAction.REPLACE);
        return e;
    }

    e = charset.newEncoder();
    e.onMalformedInput(CodingErrorAction.REPLACE);
    e.onUnmappableCharacter(CodingErrorAction.REPLACE);
    map.put(charset, e);
    return e;
}
 
Example 3
Source File: Manifest.java    From jtransc with Apache License 2.0 6 votes vote down vote up
private static void writeEntry(OutputStream os, Attributes.Name name,
				   String value, CharsetEncoder encoder, ByteBuffer bBuf) throws IOException {
    String nameString = name.getName();
    os.write(nameString.getBytes(StandardCharsets.US_ASCII));
    os.write(VALUE_SEPARATOR);

    encoder.reset();
    bBuf.clear().limit(LINE_LENGTH_LIMIT - nameString.length() - 2);

    CharBuffer cBuf = CharBuffer.wrap(value);

    while (true) {
        CoderResult r = encoder.encode(cBuf, bBuf, true);
        if (CoderResult.UNDERFLOW == r) {
            r = encoder.flush(bBuf);
        }
        os.write(bBuf.array(), bBuf.arrayOffset(), bBuf.position());
        os.write(LINE_SEPARATOR);
        if (CoderResult.UNDERFLOW == r) {
            break;
        }
        os.write(' ');
        bBuf.clear().limit(LINE_LENGTH_LIMIT - 1);
    }
}
 
Example 4
Source File: Utf7ImeHelper.java    From uiautomator-unicode-input-helper with Apache License 2.0 6 votes vote down vote up
private static ByteBuffer encode(CharBuffer in, CharsetEncoder encoder) {
    int length = (int) (in.remaining() * (double) encoder.averageBytesPerChar());
    ByteBuffer out = ByteBuffer.allocate(length);

    encoder.reset();
    CoderResult flushResult = null;

    while (flushResult != CoderResult.UNDERFLOW) {
        CoderResult encodeResult = encoder.encode(in, out, true);
        if (encodeResult == CoderResult.OVERFLOW) {
            out = allocateMore(out);
            continue;
        }

        flushResult = encoder.flush(out);
        if (flushResult == CoderResult.OVERFLOW) {
            out = allocateMore(out);
        }
    }

    out.flip();
    return out;
}
 
Example 5
Source File: HttpUtilTest.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
/**
 * https://www.securecoding.cert.org/confluence/display/java/IDS12-J.+Perform+lossless+conversion+
 * of+String+data+between+differing+character+encodings
 *
 * @param in
 *          string to encode
 * @return
 * @throws IOException
 */
private String canonicalBase64Encode( String in ) throws IOException {
  Charset charset = Charset.forName( DEFAULT_ENCODING );
  CharsetEncoder encoder = charset.newEncoder();
  encoder.reset();
  ByteBuffer baosbf = encoder.encode( CharBuffer.wrap( in ) );
  byte[] bytes = new byte[baosbf.limit()];
  baosbf.get( bytes );

  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  GZIPOutputStream gzos = new GZIPOutputStream( baos );
  gzos.write( bytes );
  gzos.close();
  String encoded = new String( Base64.encodeBase64( baos.toByteArray() ) );

  return encoded;
}
 
Example 6
Source File: CharsetUtil.java    From simple-netty-source with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a cached thread-local {@link CharsetEncoder} for the specified
 * <tt>charset</tt>.
 */
public static CharsetEncoder getEncoder(Charset charset) {
    if (charset == null) {
        throw new NullPointerException("charset");
    }

    Map<Charset, CharsetEncoder> map = encoders.get();
    CharsetEncoder e = map.get(charset);
    if (e != null) {
        e.reset();
        e.onMalformedInput(CodingErrorAction.REPLACE);
        e.onUnmappableCharacter(CodingErrorAction.REPLACE);
        return e;
    }

    e = charset.newEncoder();
    e.onMalformedInput(CodingErrorAction.REPLACE);
    e.onUnmappableCharacter(CodingErrorAction.REPLACE);
    map.put(charset, e);
    return e;
}
 
Example 7
Source File: Slices.java    From aion with MIT License 6 votes vote down vote up
/** Returns a cached thread-local {@link CharsetEncoder} for the specified <tt>charset</tt>. */
private static CharsetEncoder getEncoder(Charset charset) {
    if (charset == null) {
        throw new NullPointerException("charset");
    }

    Map<Charset, CharsetEncoder> map = encoders.get();
    CharsetEncoder e = map.get(charset);
    if (e != null) {
        e.reset();
        e.onMalformedInput(CodingErrorAction.REPLACE);
        e.onUnmappableCharacter(CodingErrorAction.REPLACE);
        return e;
    }

    e = charset.newEncoder();
    e.onMalformedInput(CodingErrorAction.REPLACE);
    e.onUnmappableCharacter(CodingErrorAction.REPLACE);
    map.put(charset, e);
    return e;
}
 
Example 8
Source File: Utf7ImeHelper.java    From uiautomator-unicode-input-helper with Apache License 2.0 6 votes vote down vote up
private static ByteBuffer encode(CharBuffer in, CharsetEncoder encoder) {
    int length = (int) (in.remaining() * (double) encoder.averageBytesPerChar());
    ByteBuffer out = ByteBuffer.allocate(length);

    encoder.reset();
    CoderResult flushResult = null;

    while (flushResult != CoderResult.UNDERFLOW) {
        CoderResult encodeResult = encoder.encode(in, out, true);
        if (encodeResult == CoderResult.OVERFLOW) {
            out = allocateMore(out);
            continue;
        }

        flushResult = encoder.flush(out);
        if (flushResult == CoderResult.OVERFLOW) {
            out = allocateMore(out);
        }
    }

    out.flip();
    return out;
}
 
Example 9
Source File: UTF7CharsetModifiedTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetBytes () throws Exception
{
  // simulate what is done in String.getBytes
  // (cannot be used directly since Charset is not installed while testing)
  final String string = "café";
  final CharsetEncoder encoder = tested.newEncoder ();
  final ByteBuffer bb = ByteBuffer.allocate ((int) (encoder.maxBytesPerChar () * string.length ()));
  final CharBuffer cb = CharBuffer.wrap (string);
  encoder.reset ();
  CoderResult cr = encoder.encode (cb, bb, true);
  if (!cr.isUnderflow ())
    cr.throwException ();
  cr = encoder.flush (bb);
  if (!cr.isUnderflow ())
    cr.throwException ();
  bb.flip ();
  assertEquals ("caf&AOk-", CharsetTestHelper.asString (bb));
}
 
Example 10
Source File: WsRemoteEndpointImplBase.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
public TextMessageSendHandler(SendHandler handler, CharBuffer message,
        boolean isLast, CharsetEncoder encoder,
        ByteBuffer encoderBuffer, WsRemoteEndpointImplBase endpoint) {
    this.handler = handler;
    this.message = message;
    this.isLast = isLast;
    this.encoder = encoder.reset();
    this.buffer = encoderBuffer;
    this.endpoint = endpoint;
}
 
Example 11
Source File: ReaderInputStream.java    From FastBootWeixin with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new input stream that will encode the characters from {@code reader} into bytes using
 * the given character set encoder.
 *
 * @param reader     input source
 * @param encoder    character set encoder used for encoding chars to bytes
 * @param bufferSize size of internal input and output buffers
 * @throws IllegalArgumentException if bufferSize is non-positive
 */
ReaderInputStream(Reader reader, CharsetEncoder encoder, int bufferSize) {
    Assert.notNull(reader, "不能为空");
    Assert.notNull(encoder, "不能为空");
    this.reader = reader;
    this.encoder = encoder;
    encoder.reset();

    charBuffer = CharBuffer.allocate(bufferSize);
    charBuffer.flip();

    byteBuffer = ByteBuffer.allocate(bufferSize);
}
 
Example 12
Source File: ReaderInputStream.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Creates a new input stream that will encode the characters from {@code reader} into bytes using
 * the given character set encoder.
 *
 * @param reader input source
 * @param encoder character set encoder used for encoding chars to bytes
 * @param bufferSize size of internal input and output buffers
 * @throws IllegalArgumentException if bufferSize is non-positive
 */

ReaderInputStream(Reader reader, CharsetEncoder encoder, int bufferSize) {
  this.reader = checkNotNull(reader);
  this.encoder = checkNotNull(encoder);
  checkArgument(bufferSize > 0, "bufferSize must be positive: %s", bufferSize);
  encoder.reset();
  charBuffer = CharBuffer.allocate(bufferSize);
  charBuffer.flip();
  byteBuffer = ByteBuffer.allocate(bufferSize);
}
 
Example 13
Source File: ReaderInputStream.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Creates a new input stream that will encode the characters from {@code reader} into bytes using
 * the given character set encoder.
 *
 * @param reader input source
 * @param encoder character set encoder used for encoding chars to bytes
 * @param bufferSize size of internal input and output buffers
 * @throws IllegalArgumentException if bufferSize is non-positive
 */

ReaderInputStream(Reader reader, CharsetEncoder encoder, int bufferSize) {
  this.reader = checkNotNull(reader);
  this.encoder = checkNotNull(encoder);
  checkArgument(bufferSize > 0, "bufferSize must be positive: %s", bufferSize);
  encoder.reset();
  charBuffer = CharBuffer.allocate(bufferSize);
  charBuffer.flip();
  byteBuffer = ByteBuffer.allocate(bufferSize);
}
 
Example 14
Source File: WsRemoteEndpointImplBase.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
public TextMessageSendHandler(SendHandler handler, CharBuffer message,
        boolean isLast, CharsetEncoder encoder,
        ByteBuffer encoderBuffer, WsRemoteEndpointImplBase endpoint) {
    this.handler = handler;
    this.message = message;
    this.isLast = isLast;
    this.encoder = encoder.reset();
    this.buffer = encoderBuffer;
    this.endpoint = endpoint;
}
 
Example 15
Source File: AsciiType.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public ByteBuffer fromString(String source)
{
    // the encoder must be reset each time it's used, hence the thread-local storage
    CharsetEncoder theEncoder = encoder.get();
    theEncoder.reset();

    try
    {
        return theEncoder.encode(CharBuffer.wrap(source));
    }
    catch (CharacterCodingException exc)
    {
        throw new MarshalException(String.format("Invalid ASCII character in string literal: %s", exc));
    }
}
 
Example 16
Source File: KeyHelper.java    From vertexium with Apache License 2.0 5 votes vote down vote up
public static Text getColumnQualifierFromPropertyMetadataColumnQualifier(String propertyName, String propertyKey, String visibilityString, String metadataKey, NameSubstitutionStrategy nameSubstitutionStrategy) {
    String name = nameSubstitutionStrategy.deflate(propertyName);
    String key = nameSubstitutionStrategy.deflate(propertyKey);
    metadataKey = nameSubstitutionStrategy.deflate(metadataKey);
    KeyBase.assertNoValueSeparator(name);
    KeyBase.assertNoValueSeparator(key);
    KeyBase.assertNoValueSeparator(visibilityString);
    KeyBase.assertNoValueSeparator(metadataKey);

    int charCount = name.length() + key.length() + visibilityString.length() + metadataKey.length() + 3;
    CharBuffer qualifierChars = (CharBuffer) CharBuffer.allocate(charCount)
        .put(name).put(KeyBase.VALUE_SEPARATOR)
        .put(key).put(KeyBase.VALUE_SEPARATOR)
        .put(visibilityString).put(KeyBase.VALUE_SEPARATOR)
        .put(metadataKey).flip();

    CharsetEncoder encoder = ENCODER_FACTORY.get();
    encoder.reset();

    try {
        ByteBuffer encodedQualifier = encoder.encode(qualifierChars);
        Text result = new Text();
        result.set(encodedQualifier.array(), 0, encodedQualifier.limit());
        return result;
    } catch (CharacterCodingException cce) {
        throw new RuntimeException("This should never happen", cce);
    }
}
 
Example 17
Source File: TextEncoderHelper.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
/**
 * For testing purposes only.
 */
@Deprecated
public static void encodeText(final CharsetEncoder charsetEncoder, final CharBuffer charBuf,
        final ByteBufferDestination destination) {
    charsetEncoder.reset();
    synchronized (destination) {
        ByteBuffer byteBuf = destination.getByteBuffer();
        byteBuf = encodeAsMuchAsPossible(charsetEncoder, charBuf, true, destination, byteBuf);
        flushRemainingBytes(charsetEncoder, destination, byteBuf);
    }
}
 
Example 18
Source File: AbstractAdaptiveByteBuffer.java    From craft-atom with MIT License 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public AdaptiveByteBuffer putString(CharSequence val, CharsetEncoder encoder) throws CharacterCodingException {
    if (val.length() == 0) {
        return this;
    }

    CharBuffer in = CharBuffer.wrap(val);
    encoder.reset();

    int expandedState = 0;

    for (;;) {
        CoderResult cr;
        if (in.hasRemaining()) {
            cr = encoder.encode(in, buf(), true);
        } else {
            cr = encoder.flush(buf());
        }

        if (cr.isUnderflow()) {
            break;
        }
        if (cr.isOverflow()) {
            if (isAutoExpand()) {
                switch (expandedState) {
                case 0:
                    autoExpand((int) Math.ceil(in.remaining() * encoder.averageBytesPerChar()));
                    expandedState++;
                    break;
                case 1:
                    autoExpand((int) Math.ceil(in.remaining() * encoder.maxBytesPerChar()));
                    expandedState++;
                    break;
                default:
                    throw new RuntimeException("Expanded by "
                            + (int) Math.ceil(in.remaining() * encoder.maxBytesPerChar())
                            + " but that wasn't enough for '" + val + "'");
                }
                continue;
            }
        } else {
            expandedState = 0;
        }
        cr.throwException();
    }
    return this;
}
 
Example 19
Source File: AbstractIoBuffer.java    From neoscada with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public IoBuffer putString(CharSequence val, CharsetEncoder encoder) throws CharacterCodingException {
    if (val.length() == 0) {
        return this;
    }

    CharBuffer in = CharBuffer.wrap(val);
    encoder.reset();

    int expandedState = 0;

    for (;;) {
        CoderResult cr;
        if (in.hasRemaining()) {
            cr = encoder.encode(in, buf(), true);
        } else {
            cr = encoder.flush(buf());
        }

        if (cr.isUnderflow()) {
            break;
        }
        if (cr.isOverflow()) {
            if (isAutoExpand()) {
                switch (expandedState) {
                case 0:
                    autoExpand((int) Math.ceil(in.remaining() * encoder.averageBytesPerChar()));
                    expandedState++;
                    break;
                case 1:
                    autoExpand((int) Math.ceil(in.remaining() * encoder.maxBytesPerChar()));
                    expandedState++;
                    break;
                default:
                    throw new RuntimeException("Expanded by "
                            + (int) Math.ceil(in.remaining() * encoder.maxBytesPerChar())
                            + " but that wasn't enough for '" + val + "'");
                }
                continue;
            }
        } else {
            expandedState = 0;
        }
        cr.throwException();
    }
    return this;
}
 
Example 20
Source File: AbstractAdaptiveByteBuffer.java    From craft-atom with MIT License 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public AdaptiveByteBuffer putString(CharSequence val, int fieldSize, CharsetEncoder encoder) throws CharacterCodingException {
    checkFieldSize(fieldSize);

    if (fieldSize == 0) {
        return this;
    }

    autoExpand(fieldSize);

    boolean utf16 = encoder.charset().name().startsWith("UTF-16");

    if (utf16 && (fieldSize & 1) != 0) {
        throw new IllegalArgumentException("fieldSize is not even.");
    }

    int oldLimit = limit();
    int end = position() + fieldSize;

    if (oldLimit < end) {
        throw new BufferOverflowException();
    }

    if (val.length() == 0) {
        if (!utf16) {
            put((byte) 0x00);
        } else {
            put((byte) 0x00);
            put((byte) 0x00);
        }
        position(end);
        return this;
    }

    CharBuffer in = CharBuffer.wrap(val);
    limit(end);
    encoder.reset();

    for (;;) {
        CoderResult cr;
        if (in.hasRemaining()) {
            cr = encoder.encode(in, buf(), true);
        } else {
            cr = encoder.flush(buf());
        }

        if (cr.isUnderflow() || cr.isOverflow()) {
            break;
        }
        cr.throwException();
    }

    limit(oldLimit);

    if (position() < end) {
        if (!utf16) {
            put((byte) 0x00);
        } else {
            put((byte) 0x00);
            put((byte) 0x00);
        }
    }

    position(end);
    return this;
}