use of java.nio.charset.Charset in project druid by druid-io.
the class InputRowParserSerdeTest method testStringInputRowParserSerdeMultiCharset.
@Test
public void testStringInputRowParserSerdeMultiCharset() throws Exception {
Charset[] testCharsets = { Charsets.US_ASCII, Charsets.ISO_8859_1, Charsets.UTF_8, Charsets.UTF_16BE, Charsets.UTF_16LE, Charsets.UTF_16 };
for (Charset testCharset : testCharsets) {
InputRow parsed = testCharsetParseHelper(testCharset);
Assert.assertEquals(ImmutableList.of("foo", "bar"), parsed.getDimensions());
Assert.assertEquals(ImmutableList.of("x"), parsed.getDimension("foo"));
Assert.assertEquals(ImmutableList.of("y"), parsed.getDimension("bar"));
Assert.assertEquals(new DateTime("3000").getMillis(), parsed.getTimestampFromEpoch());
}
}
use of java.nio.charset.Charset in project jetty.project by eclipse.
the class StreamResetTest method testBlockingWriteAfterStreamReceivingReset.
@Test
public void testBlockingWriteAfterStreamReceivingReset() throws Exception {
final CountDownLatch resetLatch = new CountDownLatch(1);
final CountDownLatch dataLatch = new CountDownLatch(1);
start(new HttpServlet() {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Charset charset = StandardCharsets.UTF_8;
byte[] data = "AFTER RESET".getBytes(charset);
response.setStatus(200);
response.setContentType("text/plain;charset=" + charset.name());
response.setContentLength(data.length * 10);
response.flushBuffer();
try {
// Wait for the reset to happen.
Assert.assertTrue(resetLatch.await(5, TimeUnit.SECONDS));
} catch (InterruptedException x) {
throw new InterruptedIOException();
}
try {
// been reset, it should throw an exception.
for (int i = 0; i < 10; i++) {
Thread.sleep(500);
response.getOutputStream().write(data);
response.flushBuffer();
}
} catch (InterruptedException x) {
} catch (IOException x) {
dataLatch.countDown();
}
}
});
Session client = newClient(new Session.Listener.Adapter());
MetaData.Request request = newRequest("GET", new HttpFields());
HeadersFrame frame = new HeadersFrame(request, null, true);
client.newStream(frame, new FuturePromise<>(), new Stream.Listener.Adapter() {
@Override
public void onHeaders(Stream stream, HeadersFrame frame) {
stream.reset(new ResetFrame(stream.getId(), ErrorCode.CANCEL_STREAM_ERROR.code), Callback.NOOP);
resetLatch.countDown();
}
});
Assert.assertTrue(dataLatch.await(5, TimeUnit.SECONDS));
}
use of java.nio.charset.Charset in project moco by dreamhead.
the class DynamicResponseHandlerFactory method fileResource.
private Resource fileResource(final String name, final FileContainer fileContainer) {
if (fileContainer.isForTemplate()) {
if ("version".equalsIgnoreCase(name)) {
return version(fileContainer.asTemplateResource());
}
return fileContainer.asTemplateResource(name);
}
TextContainer filename = fileContainer.getName();
if (filename.isRawText()) {
return asResource(name, fileContainer);
}
if (filename.isForTemplate()) {
Optional<Charset> charset = fileContainer.getCharset();
Resource resource = filename.asTemplateResource();
return asResource(name, resource, charset);
}
throw new IllegalArgumentException(format("unknown file container:[%s]", fileContainer));
}
use of java.nio.charset.Charset in project jetty.project by eclipse.
the class ServerGenerator method generateResponseHeaders.
public Result generateResponseHeaders(int request, int code, String reason, HttpFields fields, Callback callback) {
request &= 0xFF_FF;
final Charset utf8 = StandardCharsets.UTF_8;
List<byte[]> bytes = new ArrayList<>(fields.size() * 2);
int length = 0;
if (code != 200 || sendStatus200) {
// Special 'Status' header
bytes.add(STATUS);
length += STATUS.length + COLON.length;
if (reason == null)
reason = HttpStatus.getMessage(code);
byte[] responseBytes = (code + " " + reason).getBytes(utf8);
bytes.add(responseBytes);
length += responseBytes.length + EOL.length;
}
// Other headers
for (HttpField field : fields) {
String name = field.getName();
byte[] nameBytes = name.getBytes(utf8);
bytes.add(nameBytes);
String value = field.getValue();
byte[] valueBytes = value.getBytes(utf8);
bytes.add(valueBytes);
length += nameBytes.length + COLON.length;
length += valueBytes.length + EOL.length;
}
// End of headers
length += EOL.length;
final ByteBuffer buffer = byteBufferPool.acquire(length, true);
BufferUtil.clearToFill(buffer);
for (int i = 0; i < bytes.size(); i += 2) buffer.put(bytes.get(i)).put(COLON).put(bytes.get(i + 1)).put(EOL);
buffer.put(EOL);
BufferUtil.flipToFlush(buffer, 0);
return generateContent(request, buffer, true, false, callback, FCGI.FrameType.STDOUT);
}
use of java.nio.charset.Charset in project jetty.project by eclipse.
the class MultiPartContentProviderTest method testFieldWithOverridenContentType.
@Test
public void testFieldWithOverridenContentType() throws Exception {
String name = "field";
String value = "รจ";
Charset encoding = StandardCharsets.ISO_8859_1;
start(new AbstractMultiPartHandler() {
@Override
protected void handle(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Collection<Part> parts = request.getParts();
Assert.assertEquals(1, parts.size());
Part part = parts.iterator().next();
Assert.assertEquals(name, part.getName());
String contentType = part.getContentType();
Assert.assertNotNull(contentType);
int equal = contentType.lastIndexOf('=');
Charset charset = Charset.forName(contentType.substring(equal + 1));
Assert.assertEquals(encoding, charset);
Assert.assertEquals(value, IO.toString(part.getInputStream(), charset));
}
});
MultiPartContentProvider multiPart = new MultiPartContentProvider();
HttpFields fields = new HttpFields();
fields.put(HttpHeader.CONTENT_TYPE, "text/plain;charset=" + encoding.name());
BytesContentProvider content = new BytesContentProvider(value.getBytes(encoding));
multiPart.addFieldPart(name, content, fields);
multiPart.close();
ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).method(HttpMethod.POST).content(multiPart).send();
Assert.assertEquals(200, response.getStatus());
}
Aggregations