java.nio.charset.CodingErrorAction Java Examples

The following examples show how to use java.nio.charset.CodingErrorAction. 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: CharsetEncoderByteIterator.java    From rembulan with Apache License 2.0 6 votes vote down vote up
/**
 * Constructs a new encoder instance that iterates over {@code string}, converting
 * it to bytes using the charset {@code charset}.
 *
 * <p>The encoder reads up to {@code stepSize} characters at the same time,
 * buffering the results internally. {@code stepSize} must be at least 2 (this is to
 * ensure that surrogate pairs are processed correctly).
 *
 * @param string  the string to iterate over, must not be {@code null}
 * @param charset  the charset to use for encoding characters to bytes, must not be {@code null}
 * @param stepSize  the number to characters to try encoding in each encoding step,
 *                  must be positive
 *
 * @throws NullPointerException  if {@code string} or {@code charset} is {@code null}
 * @throws IllegalArgumentException  if {@code stepSize} is lesser than 2
 */
public CharsetEncoderByteIterator(String string, Charset charset, int stepSize) {
	Objects.requireNonNull(string);
	Check.gt(stepSize, 1);

	// use the same settings as String.getBytes(Charset)
	this.encoder = charset.newEncoder()
			.onMalformedInput(CodingErrorAction.REPLACE)
			.onUnmappableCharacter(CodingErrorAction.REPLACE)
			.reset();

	this.string = string;
	this.idx = 0;
	this.byteIdx = 0;
	this.flushed = false;

	// no need to allocate more chars than what the string can give us
	stepSize = Math.min(stepSize, string.length());
	stepSize = Math.max(2, stepSize);  // but ensure we can always handle surrogate pairs

	this.in = CharBuffer.allocate(stepSize);

	int outBufferSize = (int) ((stepSize + 1) * encoder.maxBytesPerChar());
	this.out = ByteBuffer.allocate(outBufferSize);
	out.flip();
}
 
Example #2
Source File: CharsetTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
private void assertCorrectEncoding(byte[] expected, String input, String charsetName)
    throws IOException {
  Charset cs = Charset.forName(charsetName);
  CharsetEncoder encoder = cs.newEncoder()
      .onMalformedInput(CodingErrorAction.REPLACE)
      .onUnmappableCharacter(CodingErrorAction.REPLACE);
  ByteBuffer bb = encoder.encode(CharBuffer.wrap(input.toCharArray()));
  byte[] result = new byte[bb.remaining()];
  bb.get(result);
  Assert.assertArrayEquals(expected, result);
  bb = cs.encode(CharBuffer.wrap(input.toCharArray()));
  result = new byte[bb.remaining()];
  bb.get(result);
  Assert.assertArrayEquals(expected, result);
  Assert.assertArrayEquals(expected, input.getBytes(charsetName));
  Assert.assertArrayEquals(expected, input.getBytes(cs));
}
 
Example #3
Source File: CharsetConverter.java    From p4ic4idea with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new charset converted that decodes/encodes bytes in the
 * specified non-null from/to charset objects specified.
 * 
 * @param fromCharset
 * @param toCharset
 * @param ignoreBOM
 *            - true to ignore any byte order marks written by the UTF-16
 *            charset and omit them from all return byte buffers
 */
public CharsetConverter(Charset fromCharset, Charset toCharset,
		boolean ignoreBOM) {
	// Create decoder that reports malformed/unmappable values
	this.decoder = fromCharset.newDecoder();
	this.decoder.onMalformedInput(CodingErrorAction.REPORT);
	this.decoder.onUnmappableCharacter(CodingErrorAction.REPORT);

	// Create encoder that reports malformed/unmappable values
	this.encoder = toCharset.newEncoder();
	this.encoder.onMalformedInput(CodingErrorAction.REPORT);
	this.encoder.onUnmappableCharacter(CodingErrorAction.REPORT);

	// Check bom on UTF-16 since Java writes a BOM on each call to encode
	if ("UTF-16".equals(toCharset.name())) {
		checkBOM = true;
	}
	this.ignoreBOM = ignoreBOM;
}
 
Example #4
Source File: BaseFileManager.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
public CharsetDecoder getDecoder(String encodingName, boolean ignoreEncodingErrors) {
    Charset cs = (this.charset == null)
        ? Charset.forName(encodingName)
        : this.charset;
    CharsetDecoder decoder = cs.newDecoder();

    CodingErrorAction action;
    if (ignoreEncodingErrors)
        action = CodingErrorAction.REPLACE;
    else
        action = CodingErrorAction.REPORT;

    return decoder
        .onMalformedInput(action)
        .onUnmappableCharacter(action);
}
 
Example #5
Source File: StringToByteArrayUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void whenUsingCharsetEncoder_thenOK()
    throws CharacterCodingException {
    final String inputString = "Hello ਸੰਸਾਰ!";
    CharsetEncoder encoder = StandardCharsets.US_ASCII.newEncoder();
    encoder.onMalformedInput(CodingErrorAction.IGNORE)
        .onUnmappableCharacter(CodingErrorAction.REPLACE)
        .replaceWith(new byte[] { 0 });

    byte[] byteArrray = encoder.encode(CharBuffer.wrap(inputString))
        .array();

    System.out.printf(
        "Using encode with CharsetEncoder:%s, Input String:%s, Output byte array:%s\n",
        encoder, inputString, Arrays.toString(byteArrray));

    assertArrayEquals(
        new byte[] { 72, 101, 108, 108, 111, 32, 0, 0, 0, 0, 0, 33 },
        byteArrray);
}
 
Example #6
Source File: CharsetConverter.java    From p4ic4idea with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new charset converted that decodes/encodes bytes in the
 * specified non-null from/to charset objects specified.
 *
 * @param fromCharset
 * @param toCharset
 * @param ignoreBOM   - true to ignore any byte order marks written by the UTF-16
 *                    charset and omit them from all return byte buffers
 */
public CharsetConverter(Charset fromCharset, Charset toCharset,
                        boolean ignoreBOM) {
	// Create decoder that reports malformed/unmappable values
	this.decoder = fromCharset.newDecoder();
	this.decoder.onMalformedInput(CodingErrorAction.REPORT);
	this.decoder.onUnmappableCharacter(CodingErrorAction.REPORT);

	// Create encoder that reports malformed/unmappable values
	this.encoder = toCharset.newEncoder();
	this.encoder.onMalformedInput(CodingErrorAction.REPORT);
	this.encoder.onUnmappableCharacter(CodingErrorAction.REPORT);

	// Check bom on UTF-16 since Java writes a BOM on each call to encode
	if ("UTF-16".equals(toCharset.name())) {
		checkBOM = true;
	}
	this.ignoreBOM = ignoreBOM;
}
 
Example #7
Source File: CharSequenceInputStream.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Constructor.
 *
 * @param cs the input character sequence
 * @param charset the character set name to use
 * @param bufferSize the buffer size to use.
 * @throws IllegalArgumentException if the buffer is not large enough to hold a complete character
 */
public CharSequenceInputStream(final CharSequence cs, final Charset charset, final int bufferSize) {
    super();
    this.encoder = charset.newEncoder()
        .onMalformedInput(CodingErrorAction.REPLACE)
        .onUnmappableCharacter(CodingErrorAction.REPLACE);
    // Ensure that buffer is long enough to hold a complete character
    final float maxBytesPerChar = encoder.maxBytesPerChar();
    if (bufferSize < maxBytesPerChar) {
        throw new IllegalArgumentException("Buffer size " + bufferSize + " is less than maxBytesPerChar " +
                maxBytesPerChar);
    }
    this.bbuf = ByteBuffer.allocate(bufferSize);
    this.bbuf.flip();
    this.cbuf = CharBuffer.wrap(cs);
    this.mark_cbuf = NO_MARK;
    this.mark_bbuf = NO_MARK;
}
 
Example #8
Source File: FilenameUtils.java    From MLib with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Convert a filename from Java´s native UTF-16 to OS native character encoding.
 *
 * @param fileName The UTF-16 filename string.
 * @return Natively encoded string for the OS.
 */
private static String convertToNativeEncoding(String fileName, boolean isPath) {
    String ret = fileName;

    ret = removeIllegalCharacters(ret, isPath);

    //convert our filename to OS encoding...
    try {
        final CharsetEncoder charsetEncoder = Charset.defaultCharset().newEncoder();
        charsetEncoder.onMalformedInput(CodingErrorAction.REPLACE); // otherwise breaks on first unconvertable char
        charsetEncoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
        charsetEncoder.replaceWith(new byte[]{'_'});

        final ByteBuffer buf = charsetEncoder.encode(CharBuffer.wrap(ret));
        if (buf.hasArray()) {
            ret = new String(buf.array());
        }

        //remove NUL character from conversion...
        ret = ret.replaceAll("\\u0000", "");
    } catch (CharacterCodingException e) {
        e.printStackTrace();
    }

    return ret;
}
 
Example #9
Source File: Text.java    From big-c with Apache License 2.0 6 votes vote down vote up
private static String decode(ByteBuffer utf8, boolean replace) 
  throws CharacterCodingException {
  CharsetDecoder decoder = DECODER_FACTORY.get();
  if (replace) {
    decoder.onMalformedInput(
        java.nio.charset.CodingErrorAction.REPLACE);
    decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
  }
  String str = decoder.decode(utf8).toString();
  // set decoder back to its default value: REPORT
  if (replace) {
    decoder.onMalformedInput(CodingErrorAction.REPORT);
    decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
  }
  return str;
}
 
Example #10
Source File: TerminalEmulator.java    From Ansole with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Construct a terminal emulator that uses the supplied screen
 *
 * @param session the terminal session the emulator is attached to
 * @param screen  the screen to render characters into.
 * @param columns the number of columns to emulate
 * @param rows    the number of rows to emulate
 * @param scheme  the default color scheme of this emulator
 */
public TerminalEmulator(TermSession session, TranscriptScreen screen, int columns, int rows, ColorScheme scheme) {
    mSession = session;
    mMainBuffer = screen;
    mScreen = mMainBuffer;
    mAltBuffer = new TranscriptScreen(columns, rows, rows, scheme);
    mRows = rows;
    mColumns = columns;
    mTabStop = new boolean[mColumns];

    setColorScheme(scheme);

    mUTF8ByteBuffer = ByteBuffer.allocate(4);
    mInputCharBuffer = CharBuffer.allocate(2);
    mUTF8Decoder = Charset.forName("UTF-8").newDecoder();
    mUTF8Decoder.onMalformedInput(CodingErrorAction.REPLACE);
    mUTF8Decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);

    reset();
}
 
Example #11
Source File: StringUtils.java    From tajo with Apache License 2.0 6 votes vote down vote up
public static byte[] convertCharsToBytes(char[] src, Charset charset) {
  CharsetEncoder encoder = charset.newEncoder();
  byte[] resultArray = new byte[(int) (src.length * encoder.maxBytesPerChar())];
  
  if (src.length != 0) {
    CharBuffer charBuffer = CharBuffer.wrap(src);
    ByteBuffer byteBuffer = ByteBuffer.wrap(resultArray);
    
    encoder.onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE);
    encoder.reset();
    
    CoderResult coderResult = encoder.encode(charBuffer, byteBuffer, true);
    if (coderResult.isUnderflow()) {
      coderResult = encoder.flush(byteBuffer);
      
      if (coderResult.isUnderflow()) {
        if (resultArray.length != byteBuffer.position()) {
          resultArray = Arrays.copyOf(resultArray, byteBuffer.position());
        }
      }
    }
  }
  
  return resultArray;
}
 
Example #12
Source File: BaseFileManager.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public CharsetDecoder getDecoder(String encodingName, boolean ignoreEncodingErrors) {
    Charset cs = (this.charset == null)
        ? Charset.forName(encodingName)
        : this.charset;
    CharsetDecoder decoder = cs.newDecoder();

    CodingErrorAction action;
    if (ignoreEncodingErrors)
        action = CodingErrorAction.REPLACE;
    else
        action = CodingErrorAction.REPORT;

    return decoder
        .onMalformedInput(action)
        .onUnmappableCharacter(action);
}
 
Example #13
Source File: PubsubConstraints.java    From gcp-ingestion with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Truncates a string to the number of characters that fit in X bytes avoiding multi byte
 * characters being cut in half at the cut off point. Also handles surrogate pairs where 2
 * characters in the string is actually one literal character.
 *
 * <p>Based on:
 * https://stackoverflow.com/a/35148974/1260237
 * http://www.jroller.com/holy/entry/truncating_utf_string_to_the
 */
private static String truncateToFitUtf8ByteLength(final String s, final int maxBytes) {
  if (s == null) {
    return null;
  }
  Charset charset = StandardCharsets.UTF_8;
  CharsetDecoder decoder = charset.newDecoder();
  byte[] sba = s.getBytes(charset);
  if (sba.length <= maxBytes) {
    return s;
  }
  final int maxTruncatedBytes = maxBytes - ELLIPSIS.getBytes(charset).length;
  // Ensure truncation by having byte buffer = maxTruncatedBytes
  ByteBuffer bb = ByteBuffer.wrap(sba, 0, maxTruncatedBytes);
  CharBuffer cb = CharBuffer.allocate(maxTruncatedBytes);
  // Ignore an incomplete character
  decoder.onMalformedInput(CodingErrorAction.IGNORE);
  decoder.decode(bb, cb, true);
  decoder.flush(cb);
  return new String(cb.array(), 0, cb.position()) + ELLIPSIS;
}
 
Example #14
Source File: CharsetUtil.java    From android-netty 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 #15
Source File: OldCharset_SingleByteAbstractTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public static void decodeReplace (byte[] input, char[] expectedOutput) throws CharacterCodingException {
        ByteBuffer inputBB = ByteBuffer.wrap(input);
        CharBuffer outputCB;
        decoder.onMalformedInput(CodingErrorAction.REPLACE);
        decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
        outputCB = decoder.decode(inputBB);
        outputCB.rewind();
        assertEqualChars2("Decoded charactes must match!",
                expectedOutput,
                outputCB.array(),
                input);
//        assertTrue("Decoded charactes (REPLACEed ones INCLUSIVE) must match!",
//                Arrays.equals(expectedOutput, outputCB.array()));

//        assertEqualChars("Decoded charactes (REPLACEed ones INCLUSIVE) must match!",
//                expectedOutput,
//                outputCB.array());

//        assertEquals("Decoded charactes must match!",
//                String.valueOf(allChars),
//                outputCB.toString());
    }
 
Example #16
Source File: StringCoding.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private StringDecoder(Charset cs, String rcn) {
    this.requestedCharsetName = rcn;
    this.cs = cs;
    this.cd = cs.newDecoder()
        .onMalformedInput(CodingErrorAction.REPLACE)
        .onUnmappableCharacter(CodingErrorAction.REPLACE);
    this.isTrusted = (cs.getClass().getClassLoader0() == null);
}
 
Example #17
Source File: ZipCoder.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private CharsetDecoder decoder() {
    CharsetDecoder dec = decTL.get();
    if (dec == null) {
        dec = cs.newDecoder()
          .onMalformedInput(CodingErrorAction.REPORT)
          .onUnmappableCharacter(CodingErrorAction.REPORT);
        decTL.set(dec);
    }
    return dec;
}
 
Example #18
Source File: CharacterEncodingExamplesUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenUTF8String_whenDecodeByUS_ASCII_thenReplaceMalformedInputSequence() throws IOException {
    Assertions.assertEquals(
      "The fa��ade pattern is a software design pattern.",
      CharacterEncodingExamples.decodeText(
        "The façade pattern is a software design pattern.",
        StandardCharsets.US_ASCII,
        CodingErrorAction.REPLACE));
}
 
Example #19
Source File: StringCoding.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
static byte[] encode(Charset cs, char[] ca, int off, int len) {
    CharsetEncoder ce = cs.newEncoder();
    int en = scale(len, ce.maxBytesPerChar());
    byte[] ba = new byte[en];
    if (len == 0)
        return ba;
    boolean isTrusted = false;
    if (System.getSecurityManager() != null) {
        if (!(isTrusted = (cs.getClass().getClassLoader0() == null))) {
            ca =  Arrays.copyOfRange(ca, off, off + len);
            off = 0;
        }
    }
    ce.onMalformedInput(CodingErrorAction.REPLACE)
      .onUnmappableCharacter(CodingErrorAction.REPLACE)
      .reset();
    if (ce instanceof ArrayEncoder) {
        int blen = ((ArrayEncoder)ce).encode(ca, off, len, ba);
        return safeTrim(ba, blen, cs, isTrusted);
    } else {
        ByteBuffer bb = ByteBuffer.wrap(ba);
        CharBuffer cb = CharBuffer.wrap(ca, off, len);
        try {
            CoderResult cr = ce.encode(cb, bb, true);
            if (!cr.isUnderflow())
                cr.throwException();
            cr = ce.flush(bb);
            if (!cr.isUnderflow())
                cr.throwException();
        } catch (CharacterCodingException x) {
            throw new Error(x);
        }
        return safeTrim(ba, bb.position(), cs, isTrusted);
    }
}
 
Example #20
Source File: StringCoding.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
static byte[] encode(Charset cs, char[] ca, int off, int len) {
    CharsetEncoder ce = cs.newEncoder();
    int en = scale(len, ce.maxBytesPerChar());
    byte[] ba = new byte[en];
    if (len == 0)
        return ba;
    boolean isTrusted = false;
    if (System.getSecurityManager() != null) {
        if (!(isTrusted = (cs.getClass().getClassLoader0() == null))) {
            ca =  Arrays.copyOfRange(ca, off, off + len);
            off = 0;
        }
    }
    ce.onMalformedInput(CodingErrorAction.REPLACE)
      .onUnmappableCharacter(CodingErrorAction.REPLACE)
      .reset();
    if (ce instanceof ArrayEncoder) {
        int blen = ((ArrayEncoder)ce).encode(ca, off, len, ba);
        return safeTrim(ba, blen, cs, isTrusted);
    } else {
        ByteBuffer bb = ByteBuffer.wrap(ba);
        CharBuffer cb = CharBuffer.wrap(ca, off, len);
        try {
            CoderResult cr = ce.encode(cb, bb, true);
            if (!cr.isUnderflow())
                cr.throwException();
            cr = ce.flush(bb);
            if (!cr.isUnderflow())
                cr.throwException();
        } catch (CharacterCodingException x) {
            throw new Error(x);
        }
        return safeTrim(ba, bb.position(), cs, isTrusted);
    }
}
 
Example #21
Source File: TextDecoder.java    From yajsync with GNU General Public License v3.0 5 votes vote down vote up
public static TextDecoder newStrict(Charset charset)
{
    CharsetDecoder encoder = charset.newDecoder().
        onMalformedInput(CodingErrorAction.REPORT).
        onUnmappableCharacter(CodingErrorAction.REPORT);
    TextDecoder instance = new TextDecoder(encoder);
    return instance;
}
 
Example #22
Source File: MD5DigesterTest.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
@Test
public void test_findAndReplaceEncodedClientLineEndingIfRequireLineEndingCovert()
        throws IOException, NoSuchMethodException, IllegalAccessException,
        InvocationTargetException {
    byte[] sourceBytes = new byte[] { 'q', 'c', 'e', '\r', '\n', '\r', '\n', 'd', 'h', 'b',
            '\n', '\r', '\n' };
    RpcUnicodeInputStream unicodeInputStream = new RpcUnicodeInputStream(
            new ByteArrayInputStream(sourceBytes));
    InputStreamReader encodedStreamReader = new InputStreamReader(unicodeInputStream,
            StandardCharsets.US_ASCII);
    CharsetEncoder utf8CharsetEncoder = CharsetDefs.UTF8.newEncoder()
            .onMalformedInput(CodingErrorAction.REPORT)
            .onUnmappableCharacter(CodingErrorAction.REPORT);
    char[] buffer = new char[5];
    buffer[0] = 'q';
    buffer[1] = 'c';
    buffer[2] = 'e';
    buffer[3] = '\r';
    ByteBuffer utf8ByteBuffer = utf8CharsetEncoder.encode(CharBuffer.wrap(buffer, 0, 4));
    encodedStreamReader.read(new char[4]);

    ByteBuffer actual = invokePrivate_findAndReplaceEncodedClientLineEndingIfRequireLineEndingCovert(
            encodedStreamReader, utf8CharsetEncoder, utf8ByteBuffer,
            ClientLineEnding.FST_L_CRLF);

    byte[] expectedBytes = new byte[] { 'q', 'c', 'e', '\n' };
    ByteBuffer expected = ByteBuffer.wrap(expectedBytes, 0, 4);

    assertThat(actual, is(expected));
}
 
Example #23
Source File: CharsetUtil.java    From getty with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a cached thread-local {@link CharsetEncoder} for the specified {@link Charset}.
 *
 * @param charset The specified charset
 * @return The encoder for the specified {@code charset}
 */
public static CharsetEncoder encoder(Charset charset) {
    checkNotNull(charset, "charset");

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

    e = encoder(charset, CodingErrorAction.REPLACE, CodingErrorAction.REPLACE);
    map.put(charset, e);
    return e;
}
 
Example #24
Source File: MD5DigesterTest.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
@Test
public void test_findAndReplaceEncodedClientLineEndingIfRequireLineEndingCovert_3()
        throws IOException, NoSuchMethodException, IllegalAccessException,
        InvocationTargetException {
    byte[] sourceBytes = new byte[] { 'q', 'c', 'e', '\r', '\n', '\r', '\n', 'd', 'h', 'b',
            '\n', '\r', '\n' };
    RpcUnicodeInputStream unicodeInputStream = new RpcUnicodeInputStream(
            new ByteArrayInputStream(sourceBytes));
    InputStreamReader encodedStreamReader = new InputStreamReader(unicodeInputStream,
            StandardCharsets.US_ASCII);
    CharsetEncoder utf8CharsetEncoder = CharsetDefs.UTF8.newEncoder()
            .onMalformedInput(CodingErrorAction.REPORT)
            .onUnmappableCharacter(CodingErrorAction.REPORT);
    char[] buffer = new char[9];
    buffer[0] = 'q';
    buffer[1] = 'c';
    buffer[2] = 'e';
    buffer[3] = '\r';
    buffer[4] = '\n';
    buffer[5] = '\r';
    buffer[6] = '\n';
    buffer[7] = 'd';
    ByteBuffer utf8ByteBuffer = utf8CharsetEncoder.encode(CharBuffer.wrap(buffer, 0, 8));
    encodedStreamReader.read(new char[8]);

    ByteBuffer actual = invokePrivate_findAndReplaceEncodedClientLineEndingIfRequireLineEndingCovert(
            encodedStreamReader, utf8CharsetEncoder, utf8ByteBuffer,
            ClientLineEnding.FST_L_CRLF);

    byte[] expectedBytes = new byte[] { 'q', 'c', 'e', '\n', '\n', 'd' };
    ByteBuffer expected = ByteBuffer.wrap(expectedBytes, 0, 6);

    assertThat(actual, is(expected));
}
 
Example #25
Source File: StringCoding.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private StringEncoder(Charset cs, String rcn) {
    this.requestedCharsetName = rcn;
    this.cs = cs;
    this.ce = cs.newEncoder()
        .onMalformedInput(CodingErrorAction.REPLACE)
        .onUnmappableCharacter(CodingErrorAction.REPLACE);
    this.isTrusted = (cs.getClass().getClassLoader0() == null);
}
 
Example #26
Source File: ZipCoder.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private CharsetEncoder encoder() {
    if (enc == null) {
        enc = cs.newEncoder()
          .onMalformedInput(CodingErrorAction.REPORT)
          .onUnmappableCharacter(CodingErrorAction.REPORT);
    }
    return enc;
}
 
Example #27
Source File: ZipCoder.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private CharsetDecoder decoder() {
    if (dec == null) {
        dec = cs.newDecoder()
          .onMalformedInput(CodingErrorAction.REPORT)
          .onUnmappableCharacter(CodingErrorAction.REPORT);
    }
    return dec;
}
 
Example #28
Source File: StringCoding.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
static byte[] encode(Charset cs, char[] ca, int off, int len) {
    CharsetEncoder ce = cs.newEncoder();
    int en = scale(len, ce.maxBytesPerChar());
    byte[] ba = new byte[en];
    if (len == 0)
        return ba;
    boolean isTrusted = false;
    if (System.getSecurityManager() != null) {
        if (!(isTrusted = (cs.getClass().getClassLoader0() == null))) {
            ca =  Arrays.copyOfRange(ca, off, off + len);
            off = 0;
        }
    }
    ce.onMalformedInput(CodingErrorAction.REPLACE)
      .onUnmappableCharacter(CodingErrorAction.REPLACE)
      .reset();
    if (ce instanceof ArrayEncoder) {
        int blen = ((ArrayEncoder)ce).encode(ca, off, len, ba);
        return safeTrim(ba, blen, cs, isTrusted);
    } else {
        ByteBuffer bb = ByteBuffer.wrap(ba);
        CharBuffer cb = CharBuffer.wrap(ca, off, len);
        try {
            CoderResult cr = ce.encode(cb, bb, true);
            if (!cr.isUnderflow())
                cr.throwException();
            cr = ce.flush(bb);
            if (!cr.isUnderflow())
                cr.throwException();
        } catch (CharacterCodingException x) {
            throw new Error(x);
        }
        return safeTrim(ba, bb.position(), cs, isTrusted);
    }
}
 
Example #29
Source File: ZipCoder.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private CharsetEncoder encoder() {
    CharsetEncoder enc = encTL.get();
    if (enc == null) {
        enc = cs.newEncoder()
          .onMalformedInput(CodingErrorAction.REPORT)
          .onUnmappableCharacter(CodingErrorAction.REPORT);
        encTL.set(enc);
    }
    return enc;
}
 
Example #30
Source File: StringRecord.java    From stratosphere with Apache License 2.0 5 votes vote down vote up
/**
 * Converts the provided String to bytes using the UTF-8 encoding. If <code>replace</code> is true, then malformed
 * input is replaced with the
 * substitution character, which is U+FFFD. Otherwise the method throws a
 * MalformedInputException.
 * 
 * @return ByteBuffer: bytes stores at ByteBuffer.array() and length is
 *         ByteBuffer.limit()
 */
public static ByteBuffer encode(final String string, final boolean replace) throws CharacterCodingException {

	final CharsetEncoder encoder = ENCODER_FACTORY.get();
	if (replace) {
		encoder.onMalformedInput(CodingErrorAction.REPLACE);
		encoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
	}
	ByteBuffer bytes = encoder.encode(CharBuffer.wrap(string.toCharArray()));
	if (replace) {
		encoder.onMalformedInput(CodingErrorAction.REPORT);
		encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
	}
	return bytes;
}