use of java.nio.charset.CharsetEncoder in project weex-example by KalicyZhou.
the class PDF417HighLevelEncoder method determineConsecutiveBinaryCount.
/**
* Determines the number of consecutive characters that are encodable using binary compaction.
*
* @param msg the message
* @param startpos the start position within the message
* @param encoding the charset used to convert the message to a byte array
* @return the requested character count
*/
private static int determineConsecutiveBinaryCount(String msg, int startpos, Charset encoding) throws WriterException {
final CharsetEncoder encoder = encoding.newEncoder();
int len = msg.length();
int idx = startpos;
while (idx < len) {
char ch = msg.charAt(idx);
int numericCount = 0;
while (numericCount < 13 && isDigit(ch)) {
numericCount++;
//textCount++;
int i = idx + numericCount;
if (i >= len) {
break;
}
ch = msg.charAt(i);
}
if (numericCount >= 13) {
return idx - startpos;
}
ch = msg.charAt(idx);
if (!encoder.canEncode(ch)) {
throw new WriterException("Non-encodable character detected: " + ch + " (Unicode: " + (int) ch + ')');
}
idx++;
}
return idx - startpos;
}
use of java.nio.charset.CharsetEncoder in project robovm by robovm.
the class ASCIICharsetEncoderTest method testInternalState_Reset.
//reset could be called at any time
public void testInternalState_Reset() {
CharsetEncoder newEncoder = cs.newEncoder();
//Init - > reset
newEncoder.reset();
//reset - > reset
newEncoder.reset();
//encoding - >reset
{
CharBuffer in = CharBuffer.wrap("A");
ByteBuffer out = ByteBuffer.allocate(0x10);
newEncoder.encode(in, out, false);
newEncoder.reset();
}
//encoding end -> reset
{
CharBuffer in = CharBuffer.wrap("A");
ByteBuffer out = ByteBuffer.allocate(0x10);
newEncoder.encode(in, out, true);
newEncoder.reset();
}
//flused -> reset
{
CharBuffer in = CharBuffer.wrap("A");
ByteBuffer out = ByteBuffer.allocate(0x10);
newEncoder.encode(in, out, true);
newEncoder.flush(out);
newEncoder.reset();
}
}
use of java.nio.charset.CharsetEncoder in project robovm by robovm.
the class Manifest method write.
/**
* Writes out the attribute information of the specified manifest to the
* specified {@code OutputStream}
*
* @param manifest
* the manifest to write out.
* @param out
* The {@code OutputStream} to write to.
* @throws IOException
* If an error occurs writing the {@code Manifest}.
*/
static void write(Manifest manifest, OutputStream out) throws IOException {
CharsetEncoder encoder = StandardCharsets.UTF_8.newEncoder();
ByteBuffer buffer = ByteBuffer.allocate(LINE_LENGTH_LIMIT);
Attributes.Name versionName = Attributes.Name.MANIFEST_VERSION;
String version = manifest.mainAttributes.getValue(versionName);
if (version == null) {
versionName = Attributes.Name.SIGNATURE_VERSION;
version = manifest.mainAttributes.getValue(versionName);
}
if (version != null) {
writeEntry(out, versionName, version, encoder, buffer);
Iterator<?> entries = manifest.mainAttributes.keySet().iterator();
while (entries.hasNext()) {
Attributes.Name name = (Attributes.Name) entries.next();
if (!name.equals(versionName)) {
writeEntry(out, name, manifest.mainAttributes.getValue(name), encoder, buffer);
}
}
}
out.write(LINE_SEPARATOR);
Iterator<String> i = manifest.getEntries().keySet().iterator();
while (i.hasNext()) {
String key = i.next();
writeEntry(out, Attributes.Name.NAME, key, encoder, buffer);
Attributes attributes = manifest.entries.get(key);
Iterator<?> entries = attributes.keySet().iterator();
while (entries.hasNext()) {
Attributes.Name name = (Attributes.Name) entries.next();
writeEntry(out, name, attributes.getValue(name), encoder, buffer);
}
out.write(LINE_SEPARATOR);
}
}
use of java.nio.charset.CharsetEncoder in project platform_frameworks_base by android.
the class StrictJarManifest method write.
/**
* Writes out the attribute information of the specified manifest to the
* specified {@code OutputStream}
*
* @param manifest
* the manifest to write out.
* @param out
* The {@code OutputStream} to write to.
* @throws IOException
* If an error occurs writing the {@code StrictJarManifest}.
*/
static void write(StrictJarManifest manifest, OutputStream out) throws IOException {
CharsetEncoder encoder = StandardCharsets.UTF_8.newEncoder();
ByteBuffer buffer = ByteBuffer.allocate(LINE_LENGTH_LIMIT);
Attributes.Name versionName = Attributes.Name.MANIFEST_VERSION;
String version = manifest.mainAttributes.getValue(versionName);
if (version == null) {
versionName = Attributes.Name.SIGNATURE_VERSION;
version = manifest.mainAttributes.getValue(versionName);
}
if (version != null) {
writeEntry(out, versionName, version, encoder, buffer);
Iterator<?> entries = manifest.mainAttributes.keySet().iterator();
while (entries.hasNext()) {
Attributes.Name name = (Attributes.Name) entries.next();
if (!name.equals(versionName)) {
writeEntry(out, name, manifest.mainAttributes.getValue(name), encoder, buffer);
}
}
}
out.write(LINE_SEPARATOR);
Iterator<String> i = manifest.getEntries().keySet().iterator();
while (i.hasNext()) {
String key = i.next();
writeEntry(out, Attributes.Name.NAME, key, encoder, buffer);
Attributes attributes = manifest.entries.get(key);
Iterator<?> entries = attributes.keySet().iterator();
while (entries.hasNext()) {
Attributes.Name name = (Attributes.Name) entries.next();
writeEntry(out, name, attributes.getValue(name), encoder, buffer);
}
out.write(LINE_SEPARATOR);
}
}
use of java.nio.charset.CharsetEncoder in project gocd by gocd.
the class NokogiriHelpers method convertEncoding.
public static byte[] convertEncoding(Charset output_charset, String input_string) throws CharacterCodingException {
CharsetEncoder encoder = output_charset.newEncoder();
CharBuffer charBuffer = CharBuffer.wrap(input_string);
ByteBuffer byteBuffer = encoder.encode(charBuffer);
byte[] buffer = new byte[byteBuffer.remaining()];
byteBuffer.get(buffer);
return buffer;
}
Aggregations