Search in sources :

Example 1 with Charset

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());
    }
}
Also used : InputRow(io.druid.data.input.InputRow) Charset(java.nio.charset.Charset) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 2 with Charset

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));
}
Also used : InterruptedIOException(java.io.InterruptedIOException) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) WriteListener(javax.servlet.WriteListener) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) Charset(java.nio.charset.Charset) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) ServletOutputStream(javax.servlet.ServletOutputStream) IStream(org.eclipse.jetty.http2.IStream) ResetFrame(org.eclipse.jetty.http2.frames.ResetFrame) Session(org.eclipse.jetty.http2.api.Session) ISession(org.eclipse.jetty.http2.ISession) Test(org.junit.Test)

Example 3 with Charset

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));
}
Also used : Resource(com.github.dreamhead.moco.resource.Resource) Charset(java.nio.charset.Charset)

Example 4 with Charset

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);
}
Also used : HttpField(org.eclipse.jetty.http.HttpField) ArrayList(java.util.ArrayList) Charset(java.nio.charset.Charset) ByteBuffer(java.nio.ByteBuffer)

Example 5 with Charset

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());
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Charset(java.nio.charset.Charset) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Part(javax.servlet.http.Part) HttpFields(org.eclipse.jetty.http.HttpFields) Collection(java.util.Collection) AbstractHttpClientServerTest(org.eclipse.jetty.client.AbstractHttpClientServerTest) Test(org.junit.Test)

Aggregations

Charset (java.nio.charset.Charset)1424 IOException (java.io.IOException)266 Test (org.junit.Test)186 InputStream (java.io.InputStream)115 ByteBuffer (java.nio.ByteBuffer)110 ArrayList (java.util.ArrayList)106 File (java.io.File)104 InputStreamReader (java.io.InputStreamReader)102 HashMap (java.util.HashMap)76 CharBuffer (java.nio.CharBuffer)65 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)58 List (java.util.List)57 Map (java.util.Map)57 OutputStreamWriter (java.io.OutputStreamWriter)56 CharsetDecoder (java.nio.charset.CharsetDecoder)56 ByteArrayInputStream (java.io.ByteArrayInputStream)54 Path (java.nio.file.Path)50 CharsetEncoder (java.nio.charset.CharsetEncoder)49 BufferedReader (java.io.BufferedReader)48 FileInputStream (java.io.FileInputStream)48