public String asString() { Charset charset = this.type.getCharset(); if (charset == null) { charset = StandardCharsets.ISO_8859_1; } return asString(charset); }
public Request bodyString(final String s, final ContentType contentType) { final Charset charset = contentType != null ? contentType.getCharset() : null; final byte[] raw = charset != null ? s.getBytes(charset) : s.getBytes(); return body(new ByteArrayEntity(raw, contentType)); }
public byte[] getBodyBytes() { if (bodyAsBytes != null) { return bodyAsBytes; } else if (bodyAsText != null) { final Charset charset = (contentType != null ? contentType : ContentType.DEFAULT_TEXT).getCharset(); return bodyAsText.getBytes(charset != null ? charset : StandardCharsets.US_ASCII); } else { return null; } }
public String getBodyText() { if (bodyAsBytes != null) { final Charset charset = (contentType != null ? contentType : ContentType.DEFAULT_TEXT).getCharset(); return new String(bodyAsBytes, charset != null ? charset : StandardCharsets.US_ASCII); } else if (bodyAsText != null) { return bodyAsText; } else { return null; } }
@Override public String getCharset() { final Charset charset = this.contentType.getCharset(); return charset != null ? charset.name() : null; }
public Reader getReader() { final Charset charset = getContentType().getCharset(); return new InputStreamReader( new ByteArrayInputStream(this.content), charset != null ? charset : StandardCharsets.US_ASCII); }
public BasicAsyncEntityProducer(final CharSequence content, final ContentType contentType, final boolean chunked) { Args.notNull(content, "Content"); this.contentType = contentType; Charset charset = contentType != null ? contentType.getCharset() : null; if (charset == null) { charset = StandardCharsets.US_ASCII; } this.bytebuf = charset.encode(CharBuffer.wrap(content)); this.length = this.bytebuf.remaining(); this.chunked = chunked; this.exception = new AtomicReference<>(null); }
public String asString() { Charset charset = this.type.getCharset(); if (charset == null) { charset = StandardCharsets.ISO_8859_1; } return asString(charset); }
/** * @since 4.3 */ public StringBody(final String text, final ContentType contentType) { super(contentType); Args.notNull(text, "Text"); final Charset charset = contentType.getCharset(); this.content = text.getBytes(charset != null ? charset : StandardCharsets.US_ASCII); }
public BasicAsyncEntityProducer(final CharSequence content, final ContentType contentType, final boolean chunked) { Args.notNull(content, "Content"); this.contentType = contentType; Charset charset = contentType != null ? contentType.getCharset() : null; if (charset == null) { charset = StandardCharsets.US_ASCII; } this.bytebuf = charset.encode(CharBuffer.wrap(content)); this.length = this.bytebuf.remaining(); this.chunked = chunked; this.exception = new AtomicReference<>(null); }
public Request bodyString(final String s, final ContentType contentType) { final Charset charset = contentType != null ? contentType.getCharset() : null; final byte[] raw = charset != null ? s.getBytes(charset) : s.getBytes(); return body(new ByteArrayEntity(raw, contentType)); }
static SimpleBody create(final String body, final ContentType contentType) { Args.notNull(body, "Body"); if (body.length() > 2048) { return new SimpleBody(null, body, contentType); } final Charset charset = (contentType != null ? contentType : ContentType.DEFAULT_TEXT).getCharset(); final byte[] bytes = body.getBytes(charset != null ? charset : StandardCharsets.US_ASCII); return new SimpleBody(bytes, null, contentType); }
@Test public void testWithCharset() throws Exception { ContentType contentType = ContentType.create("text/plain", "US-ASCII"); Assert.assertEquals("text/plain", contentType.getMimeType()); Assert.assertEquals(StandardCharsets.US_ASCII, contentType.getCharset()); Assert.assertEquals("text/plain; charset=US-ASCII", contentType.toString()); contentType = contentType.withCharset(Charset.forName("UTF-8")); Assert.assertEquals("text/plain", contentType.getMimeType()); Assert.assertEquals("UTF-8", contentType.getCharset().name()); Assert.assertEquals("text/plain; charset=UTF-8", contentType.toString()); }
@Test public void testLowCaseText() throws Exception { final ContentType contentType = ContentType.create("Text/Plain", "ascii"); Assert.assertEquals("text/plain", contentType.getMimeType()); Assert.assertEquals(StandardCharsets.US_ASCII, contentType.getCharset()); }
@Test public void testParseEmptyCharset() throws Exception { final ContentType contentType = ContentType.parse("text/plain; charset=\" \""); Assert.assertEquals("text/plain", contentType.getMimeType()); Assert.assertEquals(null, contentType.getCharset()); }
@Test public void testWithCharsetString() throws Exception { ContentType contentType = ContentType.create("text/plain", "US-ASCII"); Assert.assertEquals("text/plain", contentType.getMimeType()); Assert.assertEquals(StandardCharsets.US_ASCII, contentType.getCharset()); Assert.assertEquals("text/plain; charset=US-ASCII", contentType.toString()); contentType = contentType.withCharset(StandardCharsets.UTF_8); Assert.assertEquals("text/plain", contentType.getMimeType()); Assert.assertEquals(StandardCharsets.UTF_8, contentType.getCharset()); Assert.assertEquals("text/plain; charset=UTF-8", contentType.toString()); }
@Test public void testBasis() throws Exception { final ContentType contentType = ContentType.create("text/plain", "US-ASCII"); Assert.assertEquals("text/plain", contentType.getMimeType()); Assert.assertEquals(StandardCharsets.US_ASCII, contentType.getCharset()); Assert.assertEquals("text/plain; charset=US-ASCII", contentType.toString()); }
@Test public void testParse() throws Exception { final ContentType contentType = ContentType.parse("text/plain; charset=\"ascii\""); Assert.assertEquals("text/plain", contentType.getMimeType()); Assert.assertEquals(StandardCharsets.US_ASCII, contentType.getCharset()); Assert.assertEquals("text/plain; charset=ascii", contentType.toString()); }
@Test public void testExtract() throws Exception { try (final BasicHttpEntity httpentity = new BasicHttpEntity()) { httpentity.setContentType("text/plain; charset = UTF-8"); final ContentType contentType = EntityUtils.getContentType(httpentity); Assert.assertNotNull(contentType); Assert.assertEquals("text/plain", contentType.getMimeType()); Assert.assertEquals(StandardCharsets.UTF_8, contentType.getCharset()); }}
@Test public void testExtractNoCharset() throws Exception { try (final BasicHttpEntity httpentity = new BasicHttpEntity()) { httpentity.setContentType("text/plain; param=yadayada"); final ContentType contentType = EntityUtils.getContentType(httpentity); Assert.assertNotNull(contentType); Assert.assertEquals("text/plain", contentType.getMimeType()); Assert.assertNull(contentType.getCharset()); }}