private CoderResult decodeBufferLoop(ByteBuffer src, CharBuffer dst) { int mark = src.position(); try { while (src.hasRemaining()) { byte b = src.get(); if (!dst.hasRemaining()) return CoderResult.OVERFLOW; dst.put((char)(b & 0xff)); mark++; } return CoderResult.UNDERFLOW; } finally { src.position(mark); } }
/** * Tells whether or not the given byte array is a legal replacement value * for this encoder. * * <p> A replacement is legal if, and only if, it is a legal sequence of * bytes in this encoder's charset; that is, it must be possible to decode * the replacement into one or more sixteen-bit Unicode characters. * * <p> The default implementation of this method is not very efficient; it * should generally be overridden to improve performance. </p> * * @param repl The byte array to be tested * * @return <tt>true</tt> if, and only if, the given byte array * is a legal replacement value for this encoder */ public boolean isLegalReplacement(byte[] repl) { WeakReference<CharsetDecoder> wr = cachedDecoder; CharsetDecoder dec = null; if ((wr == null) || ((dec = wr.get()) == null)) { dec = charset().newDecoder(); dec.onMalformedInput(CodingErrorAction.REPORT); dec.onUnmappableCharacter(CodingErrorAction.REPORT); cachedDecoder = new WeakReference<CharsetDecoder>(dec); } else { dec.reset(); } ByteBuffer bb = ByteBuffer.wrap(repl); CharBuffer cb = CharBuffer.allocate((int)(bb.remaining() * dec.maxCharsPerByte())); CoderResult cr = dec.decode(bb, cb, true); return !cr.isError(); }
@Test public void testGetChar() { ByteBuffer bb = ByteBuffer.allocate(10); CharBuffer cb = bb.asCharBuffer(); cb.put("abcde"); byte[] bytes = bb.array(); ByteSource bs = createByteSource(bytes); char c = bs.getChar(); assertEquals('a', c); assertEquals(2, bs.position()); c = bs.getChar(); assertEquals('b', c); assertEquals(4, bs.position()); bs.position(8); c = bs.getChar(); assertEquals('e', c); assertEquals(10, bs.position()); try { bs.getChar(); fail("expected BufferUnderflowException"); } catch (BufferUnderflowException expected) { } }
public void testGet_io() throws IOException { assertEquals(-1, ArbitraryInstances.get(InputStream.class).read()); assertEquals(-1, ArbitraryInstances.get(ByteArrayInputStream.class).read()); assertEquals(-1, ArbitraryInstances.get(Readable.class).read(CharBuffer.allocate(1))); assertEquals(-1, ArbitraryInstances.get(Reader.class).read()); assertEquals(-1, ArbitraryInstances.get(StringReader.class).read()); assertEquals(0, ArbitraryInstances.get(Buffer.class).capacity()); assertEquals(0, ArbitraryInstances.get(CharBuffer.class).capacity()); assertEquals(0, ArbitraryInstances.get(ByteBuffer.class).capacity()); assertEquals(0, ArbitraryInstances.get(ShortBuffer.class).capacity()); assertEquals(0, ArbitraryInstances.get(IntBuffer.class).capacity()); assertEquals(0, ArbitraryInstances.get(LongBuffer.class).capacity()); assertEquals(0, ArbitraryInstances.get(FloatBuffer.class).capacity()); assertEquals(0, ArbitraryInstances.get(DoubleBuffer.class).capacity()); ArbitraryInstances.get(PrintStream.class).println("test"); ArbitraryInstances.get(PrintWriter.class).println("test"); assertNotNull(ArbitraryInstances.get(File.class)); assertFreshInstanceReturned( ByteArrayOutputStream.class, OutputStream.class, Writer.class, StringWriter.class, PrintStream.class, PrintWriter.class); assertEquals(ByteSource.empty(), ArbitraryInstances.get(ByteSource.class)); assertEquals(CharSource.empty(), ArbitraryInstances.get(CharSource.class)); assertNotNull(ArbitraryInstances.get(ByteSink.class)); assertNotNull(ArbitraryInstances.get(CharSink.class)); }
byte[] getBytes(String s) { CharsetEncoder ce = encoder().reset(); char[] ca = s.toCharArray(); int len = (int)(ca.length * ce.maxBytesPerChar()); byte[] ba = new byte[len]; if (len == 0) return ba; ByteBuffer bb = ByteBuffer.wrap(ba); CharBuffer cb = CharBuffer.wrap(ca); CoderResult cr = ce.encode(cb, bb, true); if (!cr.isUnderflow()) throw new IllegalArgumentException(cr.toString()); cr = ce.flush(bb); if (!cr.isUnderflow()) throw new IllegalArgumentException(cr.toString()); if (bb.position() == ba.length) // defensive copy? return ba; else return Arrays.copyOf(ba, bb.position()); }
@Override protected CoderResult implFlush(ByteBuffer out) { CoderResult res; if (buffer != null) { res = handleHead(null, out); return res; } else if (remainder != null) { encoder.encode(remainder, out, true); } else { CharBuffer empty = (CharBuffer) CharBuffer.allocate(0).flip(); encoder.encode(empty, out, true); } res = encoder.flush(out); return res; }
private void doWriteText(CharBuffer buffer, boolean finalFragment) throws IOException { CharsetEncoder encoder = B2CConverter.UTF_8.newEncoder(); do { CoderResult cr = encoder.encode(buffer, bb, true); if (cr.isError()) { cr.throwException(); } bb.flip(); if (buffer.hasRemaining()) { doWriteBytes(bb, false); } else { doWriteBytes(bb, finalFragment); } } while (buffer.hasRemaining()); // Reset - bb will be cleared in doWriteBytes() cb.clear(); }
public void flush() throws IOException { //Log.i("PackageManager", "flush mPos=" + mPos); if (mPos > 0) { if (mOutputStream != null) { CharBuffer charBuffer = CharBuffer.wrap(mText, 0, mPos); CoderResult result = mCharset.encode(charBuffer, mBytes, true); while (true) { if (result.isError()) { throw new IOException(result.toString()); } else if (result.isOverflow()) { flushBytes(); result = mCharset.encode(charBuffer, mBytes, true); continue; } break; } flushBytes(); mOutputStream.flush(); } else { mWriter.write(mText, 0, mPos); mWriter.flush(); } mPos = 0; } }
public WsFrameBase(WsSession wsSession, Transformation transformation) { inputBuffer = new byte[Constants.DEFAULT_BUFFER_SIZE]; messageBufferBinary = ByteBuffer.allocate(wsSession.getMaxBinaryMessageBufferSize()); messageBufferText = CharBuffer.allocate(wsSession.getMaxTextMessageBufferSize()); this.wsSession = wsSession; Transformation finalTransformation; if (isMasked()) { finalTransformation = new UnmaskTransformation(); } else { finalTransformation = new NoopTransformation(); } if (transformation == null) { this.transformation = finalTransformation; } else { transformation.setNext(finalTransformation); this.transformation = transformation; } }
/** * Returns a UTF8 encoded <code>byte[]</code> representation of the * <code>char[]</code> used to create this buffer. * * @return A byte[] */ byte[] getComputedBytes() { if ( computedBytes == null ) { ByteBuffer byteBuffer = UTF8.encode( CharBuffer.wrap( precomputedChars, 0, precomputedChars.length ) ); computedBytes = new byte[byteBuffer.remaining()]; byteBuffer.get( computedBytes ); // clear out the temporary bytebuffer byteBuffer.flip(); byte[] nullifier = new byte[byteBuffer.limit()]; Arrays.fill( nullifier, ( byte ) 0 ); byteBuffer.put( nullifier ); } return computedBytes; }
private CoderResult encodeBufferLoop(CharBuffer src, ByteBuffer dst) { int mark = src.position(); try { while (src.hasRemaining()) { char c = src.get(); if (c <= '\u00FF') { if (!dst.hasRemaining()) return CoderResult.OVERFLOW; dst.put((byte)c); mark++; continue; } if (sgp.parse(c, src) < 0) return sgp.error(); return sgp.unmappableResult(); } return CoderResult.UNDERFLOW; } finally { src.position(mark); } }
private CoderResult decodeBufferLoop(ByteBuffer src, CharBuffer dst) { int mark = src.position(); try { while (src.hasRemaining()) { char c = decode(src.get()); if (c == UNMAPPABLE_DECODING) return CoderResult.unmappableForLength(1); if (!dst.hasRemaining()) return CoderResult.OVERFLOW; dst.put(c); mark++; } return CoderResult.UNDERFLOW; } finally { src.position(mark); } }
@Override protected CoderResult implFlush(CharBuffer out) { CoderResult res; if (buffer != null) { res = handleHead(null, out); return res; } else if (remainder != null) { decoder.decode(remainder, out, true); } else { ByteBuffer empty = (ByteBuffer) ByteBuffer.allocate(0).flip(); decoder.decode(empty, out, true); } res = decoder.flush(out); return res; }
private void resizeCharBuffer() throws IOException { int maxSize = getCharBufferMaxSize(); if (cb.limit() >= maxSize) { throw new IOException(sm.getString("message.bufferTooSmall")); } long newSize = cb.limit() * 2; if (newSize > maxSize) { newSize = maxSize; } // Cast is safe. newSize < maxSize and maxSize is an int CharBuffer newBuffer = CharBuffer.allocate((int) newSize); cb.rewind(); newBuffer.put(cb); cb = newBuffer; }
private char[] getTable16KeyOffsets(int offset) { int length = b16BitUnits.charAt(offset++); if(length > 0) { char[] result = new char[length]; if(length <= 16) { for(int i = 0; i < length; ++i) { result[i] = b16BitUnits.charAt(offset++); } } else { CharBuffer temp = b16BitUnits.duplicate(); temp.position(offset); temp.get(result); } return result; } else { return emptyChars; } }
/** * Returns a new CharBuffer identical to buf, except twice the capacity. */ private static CharBuffer grow(CharBuffer buf) { char[] copy = Arrays.copyOf(buf.array(), buf.capacity() * 2); CharBuffer bigger = CharBuffer.wrap(copy); bigger.position(buf.position()); bigger.limit(buf.limit()); return bigger; }
/** * Randomize the char buffer's contents, position and limit. */ static CharBuffer randomize(CharBuffer cb) { while (cb.hasRemaining()) { cb.put((char)RAND.nextInt()); } return randomizeRange(cb); }
private static byte[] getPasswordBytes(char[] passwd) { Charset utf8 = Charset.forName("UTF-8"); CharBuffer cb = CharBuffer.wrap(passwd); ByteBuffer bb = utf8.encode(cb); int len = bb.limit(); byte[] passwdBytes = new byte[len]; bb.get(passwdBytes, 0, len); return passwdBytes; }
CMapFormat6(ByteBuffer bbuffer, int offset, char[] xlat) { bbuffer.position(offset+6); CharBuffer buffer = bbuffer.asCharBuffer(); firstCode = buffer.get(); entryCount = buffer.get(); glyphIdArray = new char[entryCount]; for (int i=0; i< entryCount; i++) { glyphIdArray[i] = buffer.get(); } }
public ByteBuffer fromString(String source) { // the encoder must be reset each time it's used, hence the thread-local storage CharsetEncoder theEncoder = encoder.get(); theEncoder.reset(); try { return theEncoder.encode(CharBuffer.wrap(source)); } catch (CharacterCodingException exc) { throw new MarshalException(String.format("Invalid ASCII character in string literal: %s", exc)); } }
/** * Generates one or two UTF-16 characters to represent the given UCS-4 * character. * * @param uc The UCS-4 character * @param len The number of input bytes from which the UCS-4 value * was constructed (used when creating result objects) * @param dst The destination buffer, to which one or two UTF-16 * characters will be written * * @return Either a positive count of the number of UTF-16 characters * written to the destination buffer, or -1, in which case * error() will return a descriptive result object */ public int generate(int uc, int len, CharBuffer dst) { if (Character.isBmpCodePoint(uc)) { char c = (char) uc; if (Character.isSurrogate(c)) { error = CoderResult.malformedForLength(len); return -1; } if (dst.remaining() < 1) { error = CoderResult.OVERFLOW; return -1; } dst.put(c); error = null; return 1; } else if (Character.isValidCodePoint(uc)) { if (dst.remaining() < 2) { error = CoderResult.OVERFLOW; return -1; } dst.put(Character.highSurrogate(uc)); dst.put(Character.lowSurrogate(uc)); error = null; return 2; } else { error = CoderResult.unmappableForLength(len); return -1; } }
private static String getHash(String text, String charset, Constants.HashAlgorithm algorithm) { MessageDigest md; try { md = MessageDigest.getInstance(algorithm.getAlgorithm()); } catch (NoSuchAlgorithmException nsae) { logger.error("Implementation of " + algorithm + " not found. " + nsae); return null; } CharsetEncoder encoder = Charset.forName(charset) .newEncoder(); ByteBuffer encoded; try { encoded = encoder.encode(CharBuffer.wrap(text)); } catch (CharacterCodingException e) { logger.error("Cannot encode text into bytes using charset " + charset + ": " + e.getMessage()); return null; } byte[] inbytes; inbytes = new byte[encoded.remaining()]; encoded.get(inbytes, 0, inbytes.length); byte[] bytes = md.digest(inbytes); // Output the bytes of the hash as a String (text/plain) StringBuilder sb = new StringBuilder(2 * bytes.length); for (int i = 0; i < bytes.length; i++) { int low = bytes[i] & 0x0f; int high = (bytes[i] & 0xf0) >> 4; sb.append(Constants.HEXADECIMAL[high]); sb.append(Constants.HEXADECIMAL[low]); } return sb.toString(); }
public TextMessageSendHandler(SendHandler handler, CharBuffer message, boolean isLast, CharsetEncoder encoder, ByteBuffer encoderBuffer, WsRemoteEndpointImplBase endpoint) { this.handler = handler; this.message = message; this.isLast = isLast; this.encoder = encoder.reset(); this.buffer = encoderBuffer; this.endpoint = endpoint; }
private CoderResult charset94NR(short newByte, CharBuffer cb) { if (newByte >= 0x21 && newByte <= (state == CHARSET_NRIIF ? 0x23 : 0x2F)) { // {I} state = CHARSET_NRIF; queue.write(newByte); } else if (newByte >= 0x40 && newByte <= 0x7E) { // F return switchDecoder(newByte, cb); } else { return escapeSequenceOther(newByte); } return CoderResult.UNDERFLOW; }
private CoderResult encodeBufferLoop(CharBuffer src, ByteBuffer dst) { int mark = src.position(); try { while (src.hasRemaining()) { char c = src.get(); if (Character.isSurrogate(c)) { if (sgp.parse(c, src) < 0) return sgp.error(); return sgp.unmappableResult(); } if (c >= '\uFFFE') return CoderResult.unmappableForLength(1); if (!dst.hasRemaining()) return CoderResult.OVERFLOW; char e = index2.charAt(index1[(c & mask1) >> shift] + (c & mask2)); // If output byte is zero because input char is zero // then character is mappable, o.w. fail if (e == '\u0000' && c != '\u0000') return CoderResult.unmappableForLength(1); mark++; dst.put((byte)e); } return CoderResult.UNDERFLOW; } finally { src.position(mark); } }
@Override public synchronized int read(CharBuffer target) throws IOException { checkNotNull(target); checkOpen(); if (!hasRemaining()) { return -1; } int charsToRead = Math.min(target.remaining(), remaining()); for (int i = 0; i < charsToRead; i++) { target.put(seq.charAt(pos++)); } return charsToRead; }
protected final CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) { if (src.hasArray() && dst.hasArray()) return encodeArrayLoop(src, dst); else return encodeBufferLoop(src, dst); }
/** * Construct a new {@link ReaderInputStream}. * * @param reader the target {@link Reader} * @param encoder the charset encoder * @param bufferSize the size of the input buffer in number of characters * @since 2.1 */ public ReaderInputStream(Reader reader, CharsetEncoder encoder, int bufferSize) { this.reader = reader; this.encoder = encoder; this.encoderIn = CharBuffer.allocate(bufferSize); this.encoderIn.flip(); this.encoderOut = ByteBuffer.allocate(128); this.encoderOut.flip(); }
protected CoderResult encodeBufferLoop(CharBuffer src, ByteBuffer dst) { int mark = src.position(); try { while (src.hasRemaining()) { int inSize = 1; char c = src.get(); int bb = encodeChar(c); if (bb == UNMAPPABLE_ENCODING) { if (Character.isSurrogate(c)) { int cp; if ((cp = sgp().parse(c, src)) < 0) return sgp.error(); bb = encodeSupp(cp); if (bb == UNMAPPABLE_ENCODING) return CoderResult.unmappableForLength(2); inSize = 2; } else { return CoderResult.unmappableForLength(1); } } if (bb > MAX_SINGLEBYTE) { // DoubleByte if (dst.remaining() < 2) return CoderResult.OVERFLOW; dst.put((byte)(bb >> 8)); dst.put((byte)(bb)); } else { if (dst.remaining() < 1) return CoderResult.OVERFLOW; dst.put((byte)bb); } mark += inSize; } return CoderResult.UNDERFLOW; } finally { src.position(mark); } }
byte[] encode(char[] ca, int off, int len) { int en = scale(len, ce.maxBytesPerChar()); byte[] ba = new byte[en]; if (len == 0) return ba; if (ce instanceof ArrayEncoder) { int blen = ((ArrayEncoder)ce).encode(ca, off, len, ba); return safeTrim(ba, blen, cs, isTrusted); } else { ce.reset(); ByteBuffer bb = ByteBuffer.wrap(ba); CharBuffer cb = CharBuffer.wrap(ca, off, len); 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(), cs, isTrusted); } }
/** * Grow the charbuffer making sure not to overflow size integer. Note * this grows in the same manner as the ArrayList that is it adds 50% * to the current size. */ private void growCharBuffer() { // overflow-conscious code int oldCapacity = charBuffer.capacity(); //System.out.println("old capacity " + oldCapacity); int newCapacity = oldCapacity + (oldCapacity >> 1); if (newCapacity < 0) { newCapacity = Integer.MAX_VALUE; } //System.out.println("new capacity " + newCapacity); charBuffer = CharBuffer.allocate(newCapacity); }
protected CoderResult decodeArrayLoop(ByteBuffer src, CharBuffer dst) { byte[] sa = src.array(); int sp = src.arrayOffset() + src.position(); int sl = src.arrayOffset() + src.limit(); char[] da = dst.array(); int dp = dst.arrayOffset() + dst.position(); int dl = dst.arrayOffset() + dst.limit(); try { while (sp < sl && dp < dl) { // inline the decodeSingle/Double() for better performance int inSize = 1; int b1 = sa[sp] & 0xff; char c = b2cSB[b1]; if (c == UNMAPPABLE_DECODING) { if (sl - sp < 2) return crMalformedOrUnderFlow(b1); int b2 = sa[sp + 1] & 0xff; if (b2 < b2Min || b2 > b2Max || (c = b2c[b1][b2 - b2Min]) == UNMAPPABLE_DECODING) { return crMalformedOrUnmappable(b1, b2); } inSize++; } da[dp++] = c; sp += inSize; } return (sp >= sl) ? CoderResult.UNDERFLOW : CoderResult.OVERFLOW; } finally { src.position(sp - src.arrayOffset()); dst.position(dp - dst.arrayOffset()); } }
public LongCharSequence(File file, Charset charset) throws FileNotFoundException { decoder = prepareDecoder(charset); fileInputStream = new FileInputStream(file); fileChannel = fileInputStream.getChannel(); fileSize = file.length(); charBuffer = CharBuffer.allocate((int) Math.min(fileSize, SIZE_LIMIT)); }
/** * Invokes the delegate's <code>read(CharBuffer)</code> method. * @param target the char buffer to read the characters into * @return the number of characters read or -1 if the end of stream * @throws IOException if an I/O error occurs * @since 2.0 */ @Override public int read(CharBuffer target) throws IOException { try { beforeRead(target != null ? target.length() : 0); int n = in.read(target); afterRead(n); return n; } catch (IOException e) { handleIOException(e); return -1; } }
public void sendString(String text) throws IOException { if (text == null) { throw new IllegalArgumentException(sm.getString("wsRemoteEndpoint.nullData")); } stateMachine.textStart(); sendPartialString(CharBuffer.wrap(text), true); }
/** * Write String using specified encoding * <p> * When this is called multiple times, all but the last value has a trailing null * * @param encoder * @param next * @param i * @param noOfValues * @return * @throws CharacterCodingException */ private ByteBuffer writeString(CharsetEncoder encoder, String next, int i, int noOfValues) throws CharacterCodingException { ByteBuffer bb; if ((i + 1) == noOfValues) { bb = encoder.encode(CharBuffer.wrap(next)); } else { bb = encoder.encode(CharBuffer.wrap(next + '\0')); } bb.rewind(); return bb; }
protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) { if (src.hasArray() && dst.hasArray()) return encodeArrayLoop(src, dst); else return encodeBufferLoop(src, dst); }
@Override protected CoderResult decodeLoop(ByteBuffer in, CharBuffer out) { if (in.hasArray() && out.hasArray()) { return decodeHasArray(in, out); } return decodeNotHasArray(in, out); }