static boolean check(CharsetDecoder dec, byte[] bytes, boolean direct, int[] flow) { int inPos = flow[0]; int inLen = flow[1]; int outPos = flow[2]; int outLen = flow[3]; int expedInPos = flow[4]; int expedOutPos = flow[5]; CoderResult expedCR = (flow[6] == 0) ? CoderResult.UNDERFLOW : CoderResult.OVERFLOW; ByteBuffer bbf; CharBuffer cbf; if (direct) { bbf = ByteBuffer.allocateDirect(inPos + bytes.length); cbf = ByteBuffer.allocateDirect((outPos + outLen) * 2).asCharBuffer(); } else { bbf = ByteBuffer.allocate(inPos + bytes.length); cbf = CharBuffer.allocate(outPos + outLen); } bbf.position(inPos); bbf.put(bytes).flip().position(inPos).limit(inPos + inLen); cbf.position(outPos); dec.reset(); CoderResult cr = dec.decode(bbf, cbf, false); if (cr != expedCR || bbf.position() != expedInPos || cbf.position() != expedOutPos) { System.out.printf("Expected(direct=%5b): [", direct); for (int i : flow) System.out.print(" " + i); System.out.println( "] CR=" + cr + ", inPos=" + bbf.position() + ", outPos=" + cbf.position()); return false; } return true; }
protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) { int mark = src.position(); if (needsMark) { if (dst.remaining() < 2) return CoderResult.OVERFLOW; put(BYTE_ORDER_MARK, dst); needsMark = false; } try { while (src.hasRemaining()) { char c = src.get(); if (!Surrogate.is(c)) { if (dst.remaining() < 2) return CoderResult.OVERFLOW; mark++; put(c, dst); continue; } int d = sgp.parse(c, src); if (d < 0) return sgp.error(); if (dst.remaining() < 4) return CoderResult.OVERFLOW; mark += 2; put(Surrogate.high(d), dst); put(Surrogate.low(d), dst); } return CoderResult.UNDERFLOW; } finally { src.position(mark); } }
public CharBuffer put(CharBuffer src) { if (src instanceof DirectCharBufferS) { if (src == this) throw new IllegalArgumentException(); DirectCharBufferS sb = (DirectCharBufferS) src; int spos = sb.position(); int slim = sb.limit(); assert (spos <= slim); int srem = (spos <= slim ? slim - spos : 0); int pos = position(); int lim = limit(); assert (pos <= lim); int rem = (pos <= lim ? lim - pos : 0); if (srem > rem) throw new BufferOverflowException(); unsafe.copyMemory(sb.ix(spos), ix(pos), srem << 1); sb.position(spos + srem); position(pos + srem); } else if (!src.isDirect()) { int spos = src.position(); int slim = src.limit(); assert (spos <= slim); int srem = (spos <= slim ? slim - spos : 0); put(src.hb, src.offset + spos, srem); src.position(spos + srem); } else { super.put(src); } return this; }
static byte[] encode(char[] cc, Charset cs, boolean testDirect, Time t) throws Exception { ByteBuffer bbf; CharBuffer cbf; CharsetEncoder enc = cs.newEncoder(); String csn = cs.name(); if (testDirect) { bbf = ByteBuffer.allocateDirect(cc.length * 4); cbf = ByteBuffer.allocateDirect(cc.length * 2).asCharBuffer(); cbf.put(cc).flip(); } else { bbf = ByteBuffer.allocate(cc.length * 4); cbf = CharBuffer.wrap(cc); } CoderResult cr = null; long t1 = System.nanoTime() / 1000; for (int i = 0; i < iteration; i++) { cbf.rewind(); bbf.clear(); enc.reset(); cr = enc.encode(cbf, bbf, true); } long t2 = System.nanoTime() / 1000; t.t = (t2 - t1) / iteration; if (cr != CoderResult.UNDERFLOW) { System.out.println("ENC-----------------"); int pos = cbf.position(); System.out.printf(" cr=%s, cbf.pos=%d, cc[pos]=%x%n", cr.toString(), pos, cc[pos] & 0xffff); throw new RuntimeException("Encoding err: " + csn); } byte[] bb = new byte[bbf.position()]; bbf.flip(); bbf.get(bb); return bb; }
public static void main(String[] arguments) { try { // read byte data into a byte buffer String data = "friends.dat"; FileInputStream inData = new FileInputStream(data); FileChannel inChannel = inData.getChannel(); long inSize = inChannel.size(); ByteBuffer source = ByteBuffer.allocate((int) inSize); inChannel.read(source, 0); source.position(0); System.out.println("Original byte data:"); for (int i = 0; source.remaining() > 0; i++) { System.out.print(source.get() + " "); } // convert byte data into character data source.position(0); Charset ascii = Charset.forName("US-ASCII"); CharsetDecoder toAscii = ascii.newDecoder(); CharBuffer destination = toAscii.decode(source); destination.position(0); System.out.println("\n\nNew character data:"); for (int i = 0; destination.remaining() > 0; i++) { System.out.print(destination.get()); } System.out.println(); } catch (FileNotFoundException fne) { System.out.println(fne.getMessage()); } catch (IOException ioe) { System.out.println(ioe.getMessage()); } }
public static void main(String[] args) { ByteBuffer bb = ByteBuffer.wrap(new byte[] {0, 0, 0, 0, 0, 0, 0, 'a'}); bb.rewind(); printnb("Byte Buffer "); while (bb.hasRemaining()) printnb(bb.position() + " -> " + bb.get() + ", "); print(); CharBuffer cb = ((ByteBuffer) bb.rewind()).asCharBuffer(); printnb("Char Buffer "); while (cb.hasRemaining()) printnb(cb.position() + " -> " + cb.get() + ", "); print(); FloatBuffer fb = ((ByteBuffer) bb.rewind()).asFloatBuffer(); printnb("Float Buffer "); while (fb.hasRemaining()) printnb(fb.position() + " -> " + fb.get() + ", "); print(); IntBuffer ib = ((ByteBuffer) bb.rewind()).asIntBuffer(); printnb("Int Buffer "); while (ib.hasRemaining()) printnb(ib.position() + " -> " + ib.get() + ", "); print(); LongBuffer lb = ((ByteBuffer) bb.rewind()).asLongBuffer(); printnb("Long Buffer "); while (lb.hasRemaining()) printnb(lb.position() + " -> " + lb.get() + ", "); print(); ShortBuffer sb = ((ByteBuffer) bb.rewind()).asShortBuffer(); printnb("Short Buffer "); while (sb.hasRemaining()) printnb(sb.position() + " -> " + sb.get() + ", "); print(); DoubleBuffer db = ((ByteBuffer) bb.rewind()).asDoubleBuffer(); printnb("Double Buffer "); while (db.hasRemaining()) printnb(db.position() + " -> " + db.get() + ", "); }
@Override int read(final TextInput ti) throws IOException { int c = -1; while (++c < 4) { final int ch = ti.readByte(); if (ch < 0) break; cache[c] = (byte) ch; outc.position(0); inc.position(0); inc.limit(c + 1); csd.reset(); final CoderResult cr = csd.decode(inc, outc, true); if (cr.isMalformed()) continue; // return character int i = 0; final int os = outc.position(); for (int o = 0; o < os; ++o) i |= outc.get(o) << (o << 3); return i; } return c == 0 ? -1 : invalid(); }
/** * Writes any remaining output to the output buffer and resets the converter to its initial state. * * @param output char array to receive flushed output. * @param outStart start writing to output array at this offset. * @param outEnd stop writing to output array at this offset (exclusive). * @exception MalformedInputException if the output to be flushed contained a partial or invalid * multibyte character sequence. flush will write what it can to the output buffer and reset * the converter before throwing this exception. An additional call to flush is not required. * @exception ConversionBufferFullException if output array is filled before all the output can be * flushed. flush will write what it can to the output buffer and remember its state. An * additional call to flush with a new output buffer will conclude the operation. */ public int flush(char[] output, int outStart, int outEnd) throws MalformedInputException, ConversionBufferFullException { byteOff = charOff = 0; if (outStart >= outEnd || outStart >= output.length) throw new ConversionBufferFullException(); if (dst != null && dst.array() == output) dst.position(outStart).limit(outEnd); else dst = CharBuffer.wrap(output, outStart, outEnd - outStart); CoderResult cr = null; try { if (src != null) cr = decoder.decode((ByteBuffer) src.clear(), dst, true); assert !cr.isUnmappable(); if (cr.isMalformed()) { badInputLength = cr.length(); reset(); throw new MalformedInputException(); } } catch (IllegalStateException ise) { if (src != null) cr = decoder.reset().decode(src, dst, true); } try { cr = decoder.flush(dst); } catch (Exception e) { assert false; } finally { byteOff = 0; charOff = dst.position(); src = null; } if (cr.isOverflow()) throw new ConversionBufferFullException(); // Return the length written to the output buffer if (cr.isUnderflow()) { int written = charOff - outStart; reset(); return written; } assert false; return -1; // should be never reached }
/** * Converts an array of bytes containing characters in an external encoding into an array of * Unicode characters. This method allows a buffer by buffer conversion of a data stream. The * state of the conversion is saved between calls to convert. Among other things, this means * multibyte input sequences can be split between calls. If a call to convert results in an * exception, the conversion may be continued by calling convert again with suitably modified * parameters. All conversions should be finished with a call to the flush method. * * @return the number of bytes written to output. * @param input byte array containing text to be converted. * @param inStart begin conversion at this offset in input array. * @param inEnd stop conversion at this offset in input array (exclusive). * @param output character array to receive conversion result. * @param outStart start writing to output array at this offset. * @param outEnd stop writing to output array at this offset (exclusive). * @exception MalformedInputException if the input buffer contains any sequence of bytes that is * illegal for the input character set. * @exception UnknownCharacterException for any character that that cannot be converted to * Unicode. Thrown only when converter is not in substitution mode. * @exception ConversionBufferFullException if output array is filled prior to converting all the * input. */ public int convert(byte[] input, int inStart, int inEnd, char[] output, int outStart, int outEnd) throws UnknownCharacterException, MalformedInputException, ConversionBufferFullException { byteOff = inStart; charOff = outStart; // throw exceptions compatible to legacy ByteToCharXxx converters if (inStart >= inEnd) return 0; if (inStart >= input.length) throw new ArrayIndexOutOfBoundsException(inStart); if (outStart >= outEnd || outStart >= output.length) throw new ConversionBufferFullException(); if (src != null && src.array() == input) src.position(inStart).limit(inEnd); else src = ByteBuffer.wrap(input, inStart, inEnd - inStart); if (dst != null && dst.array() == output) dst.position(outStart).limit(outEnd); else dst = CharBuffer.wrap(output, outStart, outEnd - outStart); CoderResult cr; try { cr = decoder.decode(src, dst, false); } catch (IllegalStateException ise) { cr = decoder.reset().decode(src, dst, false); } finally { byteOff = src.position(); charOff = dst.position(); } if (cr.isUnmappable()) { badInputLength = cr.length(); throw new UnknownCharacterException(); } if (cr.isMalformed()) { badInputLength = cr.length(); throw new MalformedInputException(); } if (cr.isOverflow()) throw new ConversionBufferFullException(); // Return the length written to the output buffer if (cr.isUnderflow()) return charOff - outStart; return -1; // should be never reached }
public String toString(int start, int end) { if ((end > limit()) || (start > end)) throw new IndexOutOfBoundsException(); try { int len = end - start; char[] ca = new char[len]; CharBuffer cb = CharBuffer.wrap(ca); CharBuffer db = this.duplicate(); db.position(start); db.limit(end); cb.put(db); return new String(ca); } catch (StringIndexOutOfBoundsException x) { throw new IndexOutOfBoundsException(); } }
public static void test(CharBuffer buff, CharBuffer slice) throws RuntimeException { boolean marked = false; try { slice.reset(); marked = true; } catch (InvalidMarkException ime) { // expected } if (marked || slice.position() != 0 || buff.remaining() != slice.limit() || buff.remaining() != slice.capacity()) { throw new RuntimeException("Calling the CharBuffer.slice method failed."); } }
static char[] decode(byte[] bb, Charset cs, boolean testDirect, Time t) throws Exception { String csn = cs.name(); CharsetDecoder dec = cs.newDecoder(); ByteBuffer bbf; CharBuffer cbf; if (testDirect) { bbf = ByteBuffer.allocateDirect(bb.length); cbf = ByteBuffer.allocateDirect(bb.length * 2).asCharBuffer(); bbf.put(bb); } else { bbf = ByteBuffer.wrap(bb); cbf = CharBuffer.allocate(bb.length); } CoderResult cr = null; long t1 = System.nanoTime() / 1000; for (int i = 0; i < iteration; i++) { bbf.rewind(); cbf.clear(); dec.reset(); cr = dec.decode(bbf, cbf, true); } long t2 = System.nanoTime() / 1000; t.t = (t2 - t1) / iteration; if (cr != CoderResult.UNDERFLOW) { System.out.println("DEC-----------------"); int pos = bbf.position(); System.out.printf( " cr=%s, bbf.pos=%d, bb[pos]=%x,%x,%x,%x%n", cr.toString(), pos, bb[pos++] & 0xff, bb[pos++] & 0xff, bb[pos++] & 0xff, bb[pos++] & 0xff); throw new RuntimeException("Decoding err: " + csn); } char[] cc = new char[cbf.position()]; cbf.flip(); cbf.get(cc); return cc; }
public static void main(String[] args) throws Exception { System.out.println(">>> StringCharBufferSliceTest-main: testing the slice method..."); final String in = "for testing"; System.out.println(">>> StringCharBufferSliceTest-main: testing with the position 0."); CharBuffer buff = CharBuffer.wrap(in); test(buff, buff.slice()); System.out.println(">>> StringCharBufferSliceTest-main: testing with new position."); buff.position(2); test(buff, buff.slice()); System.out.println( ">>> StringCharBufferSliceTest-main: testing with non zero initial position."); buff = CharBuffer.wrap(in, 3, in.length()); test(buff, buff.slice()); System.out.println(">>> StringCharBufferSliceTest-main: done!"); }
CharBufferSpliterator(CharBuffer buffer) { this(buffer, buffer.position(), buffer.limit()); }
private void flushOutput() throws IOException { writer.write(out.array(), 0, out.position()); out.clear(); }
// we assume out is large enough for this conversion // returns number of filled chars in out buffer public int convert(byte[] in, int inOffset, int inLength, char[] out) { final ByteBuffer inBuffer = ByteBuffer.wrap(in, inOffset, inLength); final CharBuffer outBuffer = CharBuffer.wrap(out, 0, out.length); myDecoder.decode(inBuffer, outBuffer, false); return outBuffer.position(); }
public static void main(String[] args) throws Exception { System.out.println(">>> StringCharBufferSliceTest-main: testing the slice method..."); final String in = "for testing"; System.out.println(">>> StringCharBufferSliceTest-main: testing with the position 0."); CharBuffer buff = CharBuffer.wrap(in); test(buff, buff.slice()); System.out.println(">>> StringCharBufferSliceTest-main: testing with new position."); buff.position(2); test(buff, buff.slice()); System.out.println( ">>> StringCharBufferSliceTest-main: testing with non zero initial position."); buff = CharBuffer.wrap(in, 3, in.length()); test(buff, buff.slice()); System.out.println(">>> StringCharBufferSliceTest-main: testing slice result with get()"); buff.position(4); buff.limit(7); CharBuffer slice = buff.slice(); for (int i = 0; i < 3; i++) { if (slice.get() != buff.get()) { throw new RuntimeException("Wrong characters in slice result."); } } System.out.println(">>> StringCharBufferSliceTest-main: testing slice result with get(int)"); buff.position(4); buff.limit(7); slice = buff.slice(); for (int i = 0; i < 3; i++) { if (slice.get(i) != buff.get(4 + i)) { throw new RuntimeException("Wrong characters in slice result."); } } System.out.println(">>> StringCharBufferSliceTest-main: testing slice with result of slice"); buff.position(0); buff.limit(buff.capacity()); slice = buff.slice(); for (int i = 0; i < 4; i++) { slice.position(i); CharBuffer nextSlice = slice.slice(); if (nextSlice.position() != 0) throw new RuntimeException("New buffer's position should be zero"); if (!nextSlice.equals(slice)) throw new RuntimeException("New buffer should be equal"); slice = nextSlice; } System.out.println(">>> StringCharBufferSliceTest-main: testing toString."); buff.position(4); buff.limit(7); slice = buff.slice(); if (!slice.toString().equals("tes")) { throw new RuntimeException("bad toString() after slice(): " + slice.toString()); } System.out.println(">>> StringCharBufferSliceTest-main: testing subSequence."); buff.position(4); buff.limit(8); slice = buff.slice(); CharSequence subSeq = slice.subSequence(1, 3); if (subSeq.charAt(0) != 'e' || subSeq.charAt(1) != 's') { throw new RuntimeException("bad subSequence() after slice(): '" + subSeq + "'"); } System.out.println(">>> StringCharBufferSliceTest-main: testing duplicate."); buff.position(4); buff.limit(8); slice = buff.slice(); CharBuffer dupe = slice.duplicate(); if (dupe.charAt(0) != 't' || dupe.charAt(1) != 'e' || dupe.charAt(2) != 's' || dupe.charAt(3) != 't') { throw new RuntimeException("bad duplicate() after slice(): '" + dupe + "'"); } System.out.println(">>> StringCharBufferSliceTest-main: done!"); }