public static void encode(Charset charset, String string, OutputStream os) throws IOException { if (string.length() == 0) return; final CharsetEncoder ce = Charsets.getCharsetEncoder(charset); int en = scale(string.length(), ce.maxBytesPerChar()); byte[] ba = new byte[en]; ce.reset(); ByteBuffer bb = ByteBuffer.wrap(ba); CharBuffer cb = CharBuffer.wrap(string); try { CoderResult cr = ce.encode(cb, bb, true); if (!cr.isUnderflow()) cr.throwException(); cr = ce.flush(bb); if (!cr.isUnderflow()) cr.throwException(); } catch (CharacterCodingException x) { // Substitution is always enabled, // so this shouldn't happen throw new Error(x); } os.write(ba, 0, bb.position()); }
public static void encode(Charset charset, String string, OutputStream os) throws IOException { if (string.length() == 0) return; final CharsetEncoder ce = Charsets.getCharsetEncoder(charset); int en = scale(string.length(), ce.maxBytesPerChar()); byte[] ba = new byte[en]; ce.reset(); ByteBuffer bb = ByteBuffer.wrap(ba); CharBuffer cb = CharBuffer.wrap(string); try { CoderResult cr = ce.encode(cb, bb, true); if (!cr.isUnderflow()) cr.throwException(); cr = ce.flush(bb); if (!cr.isUnderflow()) cr.throwException(); } catch (CharacterCodingException x) { // Substitution is always enabled, // so this shouldn't happen throw new Error(x); } os.write(ba, 0, bb.position()); }
public static void encode(Charset charset, String string, OutputStream os) throws IOException { if (string.length() == 0) return; final CharsetEncoder ce = Charsets.getCharsetEncoder(charset); int en = scale(string.length(), ce.maxBytesPerChar()); byte[] ba = new byte[en]; ce.reset(); ByteBuffer bb = ByteBuffer.wrap(ba); CharBuffer cb = CharBuffer.wrap(string); try { CoderResult cr = ce.encode(cb, bb, true); if (!cr.isUnderflow()) cr.throwException(); cr = ce.flush(bb); if (!cr.isUnderflow()) cr.throwException(); } catch (CharacterCodingException x) { // Substitution is always enabled, // so this shouldn't happen throw new Error(x); } os.write(ba, 0, bb.position()); }
public static void encode(Charset charset, String string, OutputStream os) throws IOException { if (string.length() == 0) return; final CharsetEncoder ce = Charsets.getCharsetEncoder(charset); int en = scale(string.length(), ce.maxBytesPerChar()); byte[] ba = new byte[en]; ce.reset(); ByteBuffer bb = ByteBuffer.wrap(ba); CharBuffer cb = CharBuffer.wrap(string); try { CoderResult cr = ce.encode(cb, bb, true); if (!cr.isUnderflow()) cr.throwException(); cr = ce.flush(bb); if (!cr.isUnderflow()) cr.throwException(); } catch (CharacterCodingException x) { // Substitution is always enabled, // so this shouldn't happen throw new Error(x); } os.write(ba, 0, bb.position()); }
public static void encode(Charset charset, String string, OutputStream os) throws IOException { if (string.length() == 0) return; final CharsetEncoder ce = Charsets.getCharsetEncoder(charset); int en = scale(string.length(), ce.maxBytesPerChar()); byte[] ba = new byte[en]; ce.reset(); ByteBuffer bb = ByteBuffer.wrap(ba); CharBuffer cb = CharBuffer.wrap(string); try { CoderResult cr = ce.encode(cb, bb, true); if (!cr.isUnderflow()) cr.throwException(); cr = ce.flush(bb); if (!cr.isUnderflow()) cr.throwException(); } catch (CharacterCodingException x) { // Substitution is always enabled, // so this shouldn't happen throw new Error(x); } os.write(ba, 0, bb.position()); }
public static void encode(Charset charset, String string, OutputStream os) throws IOException { if (string.length() == 0) return; final CharsetEncoder ce = Charsets.getCharsetEncoder(charset); int en = scale(string.length(), ce.maxBytesPerChar()); byte[] ba = new byte[en]; ce.reset(); ByteBuffer bb = ByteBuffer.wrap(ba); CharBuffer cb = CharBuffer.wrap(string); try { CoderResult cr = ce.encode(cb, bb, true); if (!cr.isUnderflow()) cr.throwException(); cr = ce.flush(bb); if (!cr.isUnderflow()) cr.throwException(); } catch (CharacterCodingException x) { // Substitution is always enabled, // so this shouldn't happen throw new Error(x); } os.write(ba, 0, bb.position()); }
public static byte[] encode(Charset charset, String string) { if (string.length() == 0) { return EMPTY_ARRAY; } CharsetEncoder ce = Charsets.getCharsetEncoder(charset); int en = scale(string.length(), ce.maxBytesPerChar()); byte[] ba = new byte[en]; ce.reset(); ByteBuffer bb = ByteBuffer.wrap(ba); CharBuffer cb = CharBuffer.wrap(string); try { CoderResult cr = ce.encode(cb, bb, true); if (!cr.isUnderflow()) cr.throwException(); cr = ce.flush(bb); if (!cr.isUnderflow()) cr.throwException(); } catch (CharacterCodingException x) { // Substitution is always enabled, // so this shouldn't happen throw new Error(x); } return safeTrim(ba, bb.position()); }
public static byte[] encode(Charset charset, String string) { if (string.length() == 0) { return EMPTY_ARRAY; } CharsetEncoder ce = Charsets.getCharsetEncoder(charset); int en = scale(string.length(), ce.maxBytesPerChar()); byte[] ba = new byte[en]; ce.reset(); ByteBuffer bb = ByteBuffer.wrap(ba); CharBuffer cb = CharBuffer.wrap(string); try { CoderResult cr = ce.encode(cb, bb, true); if (!cr.isUnderflow()) cr.throwException(); cr = ce.flush(bb); if (!cr.isUnderflow()) cr.throwException(); } catch (CharacterCodingException x) { // Substitution is always enabled, // so this shouldn't happen throw new Error(x); } return safeTrim(ba, bb.position()); }
public static byte[] encode(Charset charset, String string) { if (string.length() == 0) { return EMPTY_ARRAY; } CharsetEncoder ce = Charsets.getCharsetEncoder(charset); int en = scale(string.length(), ce.maxBytesPerChar()); byte[] ba = new byte[en]; ce.reset(); ByteBuffer bb = ByteBuffer.wrap(ba); CharBuffer cb = CharBuffer.wrap(string); try { CoderResult cr = ce.encode(cb, bb, true); if (!cr.isUnderflow()) cr.throwException(); cr = ce.flush(bb); if (!cr.isUnderflow()) cr.throwException(); } catch (CharacterCodingException x) { // Substitution is always enabled, // so this shouldn't happen throw new Error(x); } return safeTrim(ba, bb.position()); }
public static byte[] encode(Charset charset, String string) { if (string.length() == 0) { return EMPTY_ARRAY; } CharsetEncoder ce = Charsets.getCharsetEncoder(charset); int en = scale(string.length(), ce.maxBytesPerChar()); byte[] ba = new byte[en]; ce.reset(); ByteBuffer bb = ByteBuffer.wrap(ba); CharBuffer cb = CharBuffer.wrap(string); try { CoderResult cr = ce.encode(cb, bb, true); if (!cr.isUnderflow()) cr.throwException(); cr = ce.flush(bb); if (!cr.isUnderflow()) cr.throwException(); } catch (CharacterCodingException x) { // Substitution is always enabled, // so this shouldn't happen throw new Error(x); } return safeTrim(ba, bb.position()); }
public static byte[] encode(Charset charset, String string) { if (string.length() == 0) { return EMPTY_ARRAY; } CharsetEncoder ce = Charsets.getCharsetEncoder(charset); int en = scale(string.length(), ce.maxBytesPerChar()); byte[] ba = new byte[en]; ce.reset(); ByteBuffer bb = ByteBuffer.wrap(ba); CharBuffer cb = CharBuffer.wrap(string); try { CoderResult cr = ce.encode(cb, bb, true); if (!cr.isUnderflow()) cr.throwException(); cr = ce.flush(bb); if (!cr.isUnderflow()) cr.throwException(); } catch (CharacterCodingException x) { // Substitution is always enabled, // so this shouldn't happen throw new Error(x); } return safeTrim(ba, bb.position()); }
public static byte[] encode(Charset charset, String string) { if (string.length() == 0) { return EMPTY_ARRAY; } CharsetEncoder ce = Charsets.getCharsetEncoder(charset); int en = scale(string.length(), ce.maxBytesPerChar()); byte[] ba = new byte[en]; ce.reset(); ByteBuffer bb = ByteBuffer.wrap(ba); CharBuffer cb = CharBuffer.wrap(string); try { CoderResult cr = ce.encode(cb, bb, true); if (!cr.isUnderflow()) cr.throwException(); cr = ce.flush(bb); if (!cr.isUnderflow()) cr.throwException(); } catch (CharacterCodingException x) { // Substitution is always enabled, // so this shouldn't happen throw new Error(x); } return safeTrim(ba, bb.position()); }
public void testEncodersCache() { final CharsetEncoder encoder0 = Charsets.getCharsetEncoder(charsets[0]); final CharsetEncoder encoder1 = Charsets.getCharsetEncoder(charsets[1]); final CharsetEncoder encoder2 = Charsets.getCharsetEncoder(charsets[2]); final CharsetEncoder encoder3 = Charsets.getCharsetEncoder(charsets[3]); assertTrue("Encoder is not the same", encoder0 == Charsets.getCharsetEncoder(charsets[0])); assertTrue("Encoder is not the same", encoder1 == Charsets.getCharsetEncoder(charsets[1])); assertTrue("Encoder is not the same", encoder2 == Charsets.getCharsetEncoder(charsets[2])); assertTrue("Encoder is not the same", encoder3 == Charsets.getCharsetEncoder(charsets[3])); final CharsetEncoder encoder4 = Charsets.getCharsetEncoder(charsets[4]); assertTrue("Encoder should be different", encoder0 != Charsets.getCharsetEncoder(charsets[0])); assertTrue("Encoder is not the same", encoder4 == Charsets.getCharsetEncoder(charsets[4])); }