use of java.nio.charset.CharsetDecoder in project databus by linkedin.
the class TestRegisterRequestProcessor method testNullSchemasInGetSchemas.
private void testNullSchemasInGetSchemas(final int protoVersion) throws Exception {
LOG.info("Testing null return from fetchAllSchemaVersionsBySourceName() with protoversion " + protoVersion);
Properties params = new Properties();
final int srcId1 = 101;
final String srcName1 = "source-101";
if (protoVersion != 0) {
params.setProperty(DatabusHttpHeaders.PROTOCOL_VERSION_PARAM, Integer.toString(protoVersion));
}
params.setProperty(RegisterRequestProcessor.SOURCES_PARAM, Integer.toString(srcId1));
final StringBuilder responseStr = new StringBuilder();
ChunkedWritableByteChannel chunkedWritableByteChannel = EasyMock.createMock(ChunkedWritableByteChannel.class);
// We should write out proto-version as 3 if none was specified in the input, otherwise match the proto version
chunkedWritableByteChannel.addMetadata(EasyMock.eq(DatabusHttpHeaders.DBUS_CLIENT_RELAY_PROTOCOL_VERSION_HDR), protoVersion != 0 ? EasyMock.eq(protoVersion) : EasyMock.eq(3));
EasyMock.expectLastCall().times(1);
chunkedWritableByteChannel.write(EasyMock.anyObject(ByteBuffer.class));
EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
@Override
public Object answer() throws Throwable {
Charset charset = Charset.forName("UTF-8");
CharsetDecoder decoder = charset.newDecoder();
responseStr.append(decoder.decode((ByteBuffer) EasyMock.getCurrentArguments()[0]));
return responseStr.length();
}
});
EasyMock.replay(chunkedWritableByteChannel);
DatabusRequest mockReq = EasyMock.createMock(DatabusRequest.class);
EasyMock.expect(mockReq.getParams()).andReturn(params).anyTimes();
EasyMock.expect(mockReq.getResponseContent()).andReturn(chunkedWritableByteChannel);
EasyMock.replay(mockReq);
LogicalSource lsrc1 = new LogicalSource(srcId1, srcName1);
SourceIdNameRegistry mockSrcIdReg = EasyMock.createMock(SourceIdNameRegistry.class);
EasyMock.expect(mockSrcIdReg.getSource(srcId1)).andReturn(lsrc1).anyTimes();
EasyMock.replay(mockSrcIdReg);
SchemaRegistryService mockSchemaReg = EasyMock.createMock(SchemaRegistryService.class);
EasyMock.expect(mockSchemaReg.fetchAllSchemaVersionsBySourceName(srcName1)).andReturn(null);
EasyMock.replay(mockSchemaReg);
HttpRelay mockRelay = EasyMock.createMock(HttpRelay.class);
EasyMock.expect(mockRelay.getHttpStatisticsCollector()).andReturn(null).anyTimes();
EasyMock.expect(mockRelay.getSourcesIdNameRegistry()).andReturn(mockSrcIdReg).anyTimes();
EasyMock.expect(mockRelay.getSchemaRegistryService()).andReturn(mockSchemaReg).anyTimes();
EasyMock.replay(mockRelay);
RegisterRequestProcessor reqProcessor = new RegisterRequestProcessor(null, mockRelay);
reqProcessor.process(mockReq);
ObjectMapper mapper = new ObjectMapper();
List<RegisterResponseEntry> schemasList = mapper.readValue(responseStr.toString(), new TypeReference<List<RegisterResponseEntry>>() {
});
Map<Long, List<RegisterResponseEntry>> sourcesSchemasMap = RegisterResponseEntry.convertSchemaListToMap(schemasList);
// There should be 1 entry in the map.
Assert.assertEquals(0, sourcesSchemasMap.size());
EasyMock.verify(mockRelay);
EasyMock.verify(mockReq);
EasyMock.verify(mockSchemaReg);
EasyMock.verify(mockSrcIdReg);
}
use of java.nio.charset.CharsetDecoder in project netty by netty.
the class CharsetUtil method decoder.
/**
* Returns a new {@link CharsetDecoder} for the {@link Charset} with specified error actions.
*
* @param charset The specified charset
* @param malformedInputAction The decoder's action for malformed-input errors
* @param unmappableCharacterAction The decoder's action for unmappable-character errors
* @return The decoder for the specified {@code charset}
*/
public static CharsetDecoder decoder(Charset charset, CodingErrorAction malformedInputAction, CodingErrorAction unmappableCharacterAction) {
checkNotNull(charset, "charset");
CharsetDecoder d = charset.newDecoder();
d.onMalformedInput(malformedInputAction).onUnmappableCharacter(unmappableCharacterAction);
return d;
}
use of java.nio.charset.CharsetDecoder in project platformlayer by platformlayer.
the class CsrParser method tryDecodeAsString.
private String tryDecodeAsString(byte[] data) {
try {
// We do this so we get strict input processing
CharsetDecoder decoder = Charsets.UTF_8.newDecoder();
decoder.onMalformedInput(CodingErrorAction.REPORT);
decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
ByteBuffer byteBuffer = ByteBuffer.wrap(data);
CharBuffer charBuffer = decoder.decode(byteBuffer);
return charBuffer.toString();
} catch (Exception e) {
log.debug("Cannot decode as string", e);
return null;
}
}
use of java.nio.charset.CharsetDecoder in project platformlayer by platformlayer.
the class KeyParser method tryDecodeAsString.
private String tryDecodeAsString(byte[] data) {
try {
CharsetDecoder decoder = Utf8.CHARSET.newDecoder();
decoder.onMalformedInput(CodingErrorAction.REPORT);
decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
ByteBuffer byteBuffer = ByteBuffer.wrap(data);
CharBuffer charBuffer = decoder.decode(byteBuffer);
return charBuffer.toString();
} catch (Exception e) {
log.debug("Cannot decode as string", e);
return null;
}
}
use of java.nio.charset.CharsetDecoder in project sessdb by ppdai.
the class Slices method getDecoder.
/**
* Returns a cached thread-local {@link CharsetDecoder} for the specified
* <tt>charset</tt>.
*/
private static CharsetDecoder getDecoder(Charset charset) {
if (charset == null) {
throw new NullPointerException("charset");
}
Map<Charset, CharsetDecoder> map = decoders.get();
CharsetDecoder d = map.get(charset);
if (d != null) {
d.reset();
d.onMalformedInput(CodingErrorAction.REPLACE);
d.onUnmappableCharacter(CodingErrorAction.REPLACE);
return d;
}
d = charset.newDecoder();
d.onMalformedInput(CodingErrorAction.REPLACE);
d.onUnmappableCharacter(CodingErrorAction.REPLACE);
map.put(charset, d);
return d;
}
Aggregations