use of java.nio.charset.Charset in project async-http-client by AsyncHttpClient.
the class NettyRequestFactory method body.
private NettyBody body(Request request, boolean connect) {
NettyBody nettyBody = null;
if (!connect) {
Charset bodyCharset = withDefault(request.getCharset(), DEFAULT_CHARSET);
if (request.getByteData() != null) {
nettyBody = new NettyByteArrayBody(request.getByteData());
} else if (request.getCompositeByteData() != null) {
nettyBody = new NettyCompositeByteArrayBody(request.getCompositeByteData());
} else if (request.getStringData() != null) {
nettyBody = new NettyByteBufferBody(StringUtils.charSequence2ByteBuffer(request.getStringData(), bodyCharset));
} else if (request.getByteBufferData() != null) {
nettyBody = new NettyByteBufferBody(request.getByteBufferData());
} else if (request.getStreamData() != null) {
nettyBody = new NettyInputStreamBody(request.getStreamData());
} else if (isNonEmpty(request.getFormParams())) {
CharSequence contentType = null;
if (!request.getHeaders().contains(CONTENT_TYPE)) {
contentType = HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED;
}
nettyBody = new NettyByteBufferBody(urlEncodeFormParams(request.getFormParams(), bodyCharset), contentType);
} else if (isNonEmpty(request.getBodyParts())) {
nettyBody = new NettyMultipartBody(request.getBodyParts(), request.getHeaders(), config);
} else if (request.getFile() != null) {
nettyBody = new NettyFileBody(request.getFile(), config);
} else if (request.getBodyGenerator() instanceof FileBodyGenerator) {
FileBodyGenerator fileBodyGenerator = (FileBodyGenerator) request.getBodyGenerator();
nettyBody = new NettyFileBody(fileBodyGenerator.getFile(), fileBodyGenerator.getRegionSeek(), fileBodyGenerator.getRegionLength(), config);
} else if (request.getBodyGenerator() instanceof InputStreamBodyGenerator) {
InputStreamBodyGenerator inStreamGenerator = InputStreamBodyGenerator.class.cast(request.getBodyGenerator());
nettyBody = new NettyInputStreamBody(inStreamGenerator.getInputStream(), inStreamGenerator.getContentLength());
} else if (request.getBodyGenerator() instanceof ReactiveStreamsBodyGenerator) {
ReactiveStreamsBodyGenerator reactiveStreamsBodyGenerator = (ReactiveStreamsBodyGenerator) request.getBodyGenerator();
nettyBody = new NettyReactiveStreamsBody(reactiveStreamsBodyGenerator.getPublisher(), reactiveStreamsBodyGenerator.getContentLength());
} else if (request.getBodyGenerator() != null) {
nettyBody = new NettyBodyBody(request.getBodyGenerator().createBody(), config);
}
}
return nettyBody;
}
use of java.nio.charset.Charset in project async-http-client by AsyncHttpClient.
the class HttpUtilsTest method testParseCharsetWithSingleQuotes.
@Test
public void testParseCharsetWithSingleQuotes() {
Charset charset = HttpUtils.parseCharset("Content-type: application/json; charset='utf-8'");
assertEquals(charset, StandardCharsets.UTF_8, "parseCharset returned wrong Charset");
}
use of java.nio.charset.Charset in project async-http-client by AsyncHttpClient.
the class HttpUtilsTest method testParseCharsetReturnsNullWhenNoCharset.
@Test
public void testParseCharsetReturnsNullWhenNoCharset() {
Charset charset = HttpUtils.parseCharset("Content-type: application/json");
assertNull(charset, "parseCharset should return null when charset is not specified in header value");
}
use of java.nio.charset.Charset in project zxing by zxing.
the class StringUtilsTestCase method main.
/**
* Utility for printing out a string in given encoding as a Java statement, since it's better
* to write that into the Java source file rather than risk character encoding issues in the
* source file itself.
*
* @param args command line arguments
*/
public static void main(String[] args) {
String text = args[0];
Charset charset = Charset.forName(args[1]);
StringBuilder declaration = new StringBuilder();
declaration.append("new byte[] { ");
for (byte b : text.getBytes(charset)) {
declaration.append("(byte) 0x");
declaration.append(Integer.toHexString(b & 0xFF));
declaration.append(", ");
}
declaration.append('}');
System.out.println(declaration);
}
use of java.nio.charset.Charset in project zxing by zxing.
the class DecodedBitStreamParser method decode.
static DecoderResult decode(int[] codewords, String ecLevel) throws FormatException {
StringBuilder result = new StringBuilder(codewords.length * 2);
Charset encoding = DEFAULT_ENCODING;
// Get compaction mode
int codeIndex = 1;
int code = codewords[codeIndex++];
PDF417ResultMetadata resultMetadata = new PDF417ResultMetadata();
while (codeIndex < codewords[0]) {
switch(code) {
case TEXT_COMPACTION_MODE_LATCH:
codeIndex = textCompaction(codewords, codeIndex, result);
break;
case BYTE_COMPACTION_MODE_LATCH:
case BYTE_COMPACTION_MODE_LATCH_6:
codeIndex = byteCompaction(code, codewords, encoding, codeIndex, result);
break;
case MODE_SHIFT_TO_BYTE_COMPACTION_MODE:
result.append((char) codewords[codeIndex++]);
break;
case NUMERIC_COMPACTION_MODE_LATCH:
codeIndex = numericCompaction(codewords, codeIndex, result);
break;
case ECI_CHARSET:
CharacterSetECI charsetECI = CharacterSetECI.getCharacterSetECIByValue(codewords[codeIndex++]);
encoding = Charset.forName(charsetECI.name());
break;
case ECI_GENERAL_PURPOSE:
// Can't do anything with generic ECI; skip its 2 characters
codeIndex += 2;
break;
case ECI_USER_DEFINED:
// Can't do anything with user ECI; skip its 1 character
codeIndex++;
break;
case BEGIN_MACRO_PDF417_CONTROL_BLOCK:
codeIndex = decodeMacroBlock(codewords, codeIndex, resultMetadata);
break;
case BEGIN_MACRO_PDF417_OPTIONAL_FIELD:
case MACRO_PDF417_TERMINATOR:
// Should not see these outside a macro block
throw FormatException.getFormatInstance();
default:
// Default to text compaction. During testing numerous barcodes
// appeared to be missing the starting mode. In these cases defaulting
// to text compaction seems to work.
codeIndex--;
codeIndex = textCompaction(codewords, codeIndex, result);
break;
}
if (codeIndex < codewords.length) {
code = codewords[codeIndex++];
} else {
throw FormatException.getFormatInstance();
}
}
if (result.length() == 0) {
throw FormatException.getFormatInstance();
}
DecoderResult decoderResult = new DecoderResult(null, result.toString(), null, ecLevel);
decoderResult.setOther(resultMetadata);
return decoderResult;
}
Aggregations