java.nio.charset.CoderMalfunctionError Java Examples

The following examples show how to use java.nio.charset.CoderMalfunctionError. 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: CharSequenceEncoder.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public DataBuffer encodeValue(CharSequence charSequence, DataBufferFactory bufferFactory,
		ResolvableType valueType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {

	if (!Hints.isLoggingSuppressed(hints)) {
		LogFormatUtils.traceDebug(logger, traceOn -> {
			String formatted = LogFormatUtils.formatValue(charSequence, !traceOn);
			return Hints.getLogPrefix(hints) + "Writing " + formatted;
		});
	}
	boolean release = true;
	Charset charset = getCharset(mimeType);
	int capacity = calculateCapacity(charSequence, charset);
	DataBuffer dataBuffer = bufferFactory.allocateBuffer(capacity);
	try {
		dataBuffer.write(charSequence, charset);
		release = false;
	}
	catch (CoderMalfunctionError ex) {
		throw new EncodingException("String encoding error: " + ex.getMessage(), ex);
	}
	finally {
		if (release) {
			DataBufferUtils.release(dataBuffer);
		}
	}
	return dataBuffer;
}
 
Example #2
Source File: CharSequenceEncoder.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public Flux<DataBuffer> encode(Publisher<? extends CharSequence> inputStream,
		DataBufferFactory bufferFactory, ResolvableType elementType,
		@Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {

	Charset charset = getCharset(mimeType);

	return Flux.from(inputStream).map(charSequence -> {
		if (!Hints.isLoggingSuppressed(hints)) {
			LogFormatUtils.traceDebug(logger, traceOn -> {
				String formatted = LogFormatUtils.formatValue(charSequence, !traceOn);
				return Hints.getLogPrefix(hints) + "Writing " + formatted;
			});
		}
		boolean release = true;
		int capacity = calculateCapacity(charSequence, charset);
		DataBuffer dataBuffer = bufferFactory.allocateBuffer(capacity);
		try {
			dataBuffer.write(charSequence, charset);
			release = false;
		}
		catch (CoderMalfunctionError ex) {
			throw new EncodingException("String encoding error: " + ex.getMessage(), ex);
		}
		finally {
			if (release) {
				DataBufferUtils.release(dataBuffer);
			}
		}
		return dataBuffer;
	});
}
 
Example #3
Source File: CharsetEncoder2Test.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void test_EncodeLjava_nio_CharBuffer() throws Exception {
    MockMalfunctionCharset cs = new MockMalfunctionCharset("mock", null);
    try {
        cs.encode(CharBuffer.wrap("AB"));
        fail("should throw CoderMalfunctionError");// NON-NLS-1$
    } catch (CoderMalfunctionError e) {
        // expected
    }
}
 
Example #4
Source File: JulHandlerPrintStreamAdapterFactory.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
public void write(byte[] a, int offset, int length) {
  ByteBuffer incoming = ByteBuffer.wrap(a, offset, length);
  // Consume the added bytes, flushing on decoded newlines or if we hit
  // the buffer limit.
  String msg = null;
  synchronized (decoder) {
    decoded.clear();
    boolean flush = false;
    try {
      // Process any remaining bytes from last time by adding a byte at a time.
      while (carryOverBytes > 0 && incoming.hasRemaining()) {
        carryOverByteArray[carryOverBytes++] = incoming.get();
        ByteBuffer wrapped = ByteBuffer.wrap(carryOverByteArray, 0, carryOverBytes);
        decoder.decode(wrapped, decoded, false);
        if (!wrapped.hasRemaining()) {
          carryOverBytes = 0;
        }
      }
      carryOverBytes = 0;
      if (incoming.hasRemaining()) {
        CoderResult result = decoder.decode(incoming, decoded, false);
        if (result.isOverflow()) {
          flush = true;
        }
        // Keep the unread bytes.
        assert (incoming.remaining() <= carryOverByteArray.length);
        while (incoming.hasRemaining()) {
          carryOverByteArray[carryOverBytes++] = incoming.get();
        }
      }
    } catch (CoderMalfunctionError error) {
      decoder.reset();
      carryOverBytes = 0;
      error.printStackTrace();
    }
    decoded.flip();
    synchronized (this) {
      int startLength = buffer.length();
      buffer.append(decoded);
      if (flush || buffer.indexOf("\n", startLength) >= 0) {
        msg = flushToString();
      }
    }
  }
  publishIfNonEmpty(msg);
}
 
Example #5
Source File: CoderMalfunctionErrorTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
public void testConstructor_Normal() {
	Exception ex = new Exception();
	CoderMalfunctionError e = new CoderMalfunctionError(ex);
	assertSame(ex, e.getCause());
}
 
Example #6
Source File: CoderMalfunctionErrorTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
public void testConstructor_Null() {
	CoderMalfunctionError e = new CoderMalfunctionError(null);
	assertNull(e.getCause());
}
 
Example #7
Source File: CoderMalfunctionErrorTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * @tests serialization/deserialization compatibility.
 */
public void testSerializationSelf() throws Exception {

    SerializationTest.verifySelf(new CoderMalfunctionError(null));
}
 
Example #8
Source File: CoderMalfunctionErrorTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * @tests serialization/deserialization compatibility with RI.
 */
public void testSerializationCompatibility() throws Exception {
    SerializationTest.verifyGolden(this, new CoderMalfunctionError(null));

}