Example usage for java.nio CharBuffer position

List of usage examples for java.nio CharBuffer position

Introduction

In this page you can find the example usage for java.nio CharBuffer position.

Prototype

public final Buffer position(int newPosition) 

Source Link

Document

Sets the position of this buffer.

Usage

From source file:MainClass.java

public static void main(String argv[]) {
    ByteBuffer bb = ByteBuffer.allocate(100);

    bb.mark();//ww w  .  ja  va 2  s. c om
    bb.position(5);
    bb.reset();

    bb.mark().position(5).reset();

    char[] myBuffer = new char[100];

    CharBuffer cb = CharBuffer.wrap(myBuffer);
    cb.position(12).limit(21);

    CharBuffer sliced = cb.slice();

    System.out.println("Sliced: offset=" + sliced.arrayOffset() + ", capacity=" + sliced.capacity());
}

From source file:MainClass.java

public static void main(String[] argv) throws Exception {
    CharBuffer buffer = CharBuffer.allocate(8);
    buffer.position(3).limit(5);
    CharBuffer sliceBuffer = buffer.slice();

    println(buffer);//from  www  .j  av  a2 s  .  c  om
    println(sliceBuffer);

    char[] myBuffer = new char[100];
    CharBuffer cb = CharBuffer.wrap(myBuffer);

    cb.position(12).limit(21);

    CharBuffer sliced = cb.slice();

    println(cb);
    println(sliced);
}

From source file:MainClass.java

public static void main(String[] argv) throws Exception {
    CharBuffer buffer = CharBuffer.wrap("01234567");

    buffer.position(3).limit(6).mark().position(5);

    CharBuffer dupeBuffer = buffer.duplicate();

    buffer.clear();//w w  w.j a v a 2 s .  c  o  m

    println(buffer);
    println(dupeBuffer);

    dupeBuffer.reset();
    println(dupeBuffer);

    dupeBuffer.clear();
    println(dupeBuffer);
}

From source file:BufferConverter.java

public static void main(String[] arguments) {
    try {//from   w  w  w  . ja va2 s . c om
        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);
        for (int i = 0; source.remaining() > 0; i++)
            System.out.print(source.get() + " ");

        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());
    } catch (Exception ioe) {
        System.out.println(ioe.getMessage());
    }
}

From source file:co.cask.cdap.logging.gateway.handlers.ChunkedLogReaderCallback.java

private void encodeSend(CharBuffer inBuffer, boolean endOfInput) throws IOException {
    while (true) {
        CoderResult coderResult = charsetEncoder.encode(inBuffer, chunkBuffer, endOfInput);
        if (coderResult.isOverflow()) {
            // if reached buffer capacity then flush chunk
            chunkBuffer.flip();/*from  w  w w  .  ja  v  a 2  s. c  om*/
            chunkResponder.sendChunk(ChannelBuffers.copiedBuffer(chunkBuffer));
            chunkBuffer.clear();
        } else if (coderResult.isError()) {
            // skip characters causing error, and retry
            inBuffer.position(inBuffer.position() + coderResult.length());
        } else {
            // log line was completely written
            break;
        }
    }
}

From source file:org.gtdfree.model.GTDDataXMLTools.java

static public DataHeader load(GTDModel model, InputStream in) throws XMLStreamException, IOException {

    model.setSuspendedForMultipleChanges(true);
    model.getDataRepository().suspend(true);

    XMLStreamReader r;//w w  w .  j a  v  a 2 s .c om
    try {

        // buffer size is same as default in 1.6, we explicitly request it so, not to brake if defaut changes.
        BufferedInputStream bin = new BufferedInputStream(in, 8192);
        bin.mark(8191);

        Reader rr = new InputStreamReader(bin);
        CharBuffer b = CharBuffer.allocate(96);
        rr.read(b);
        b.position(0);
        //System.out.println(b);
        Pattern pattern = Pattern.compile("<\\?.*?encoding\\s*?=.*?\\?>", Pattern.CASE_INSENSITIVE); //$NON-NLS-1$
        Matcher matcher = pattern.matcher(b);

        // reset back to start of file
        bin.reset();

        // we check if encoding is defined in xml, by the book encoding on r should be null if not defined in xml,
        // but in reality it can be arbitrary if not defined in xml. So we have to check ourselves.
        if (matcher.find()) {
            //System.out.println(matcher);
            // if defined, then XML parser will pick it up and use it
            r = XMLInputFactory.newInstance().createXMLStreamReader(bin);
            Logger.getLogger(GTDDataXMLTools.class).info("XML declared encoding: " + r.getEncoding() //$NON-NLS-1$
                    + ", system default encoding: " + Charset.defaultCharset()); //$NON-NLS-1$
        } else {
            //System.out.println(matcher);
            // if not defined, then we assume it is generated by gtd-free version 0.4 or some local editor,
            // so we assume system default encoding.
            r = XMLInputFactory.newInstance().createXMLStreamReader(new InputStreamReader(bin));
            Logger.getLogger(GTDDataXMLTools.class)
                    .info("XML assumed system default encoding: " + Charset.defaultCharset()); //$NON-NLS-1$
        }

        r.nextTag();
        if ("gtd-data".equals(r.getLocalName())) { //$NON-NLS-1$
            DataHeader dh = new DataHeader(null, r.getAttributeValue(null, "version"), //$NON-NLS-1$
                    r.getAttributeValue(null, "modified")); //$NON-NLS-1$
            if (dh.version != null) {
                if (dh.version.equals("2.0")) { //$NON-NLS-1$
                    r.nextTag();
                    _load_2_0(model, r);
                    return dh;
                }
            }
            String s = r.getAttributeValue(null, "lastActionID"); //$NON-NLS-1$
            if (s != null) {
                try {
                    model.setLastActionID(Integer.parseInt(s));
                } catch (Exception e) {
                    Logger.getLogger(GTDDataXMLTools.class).debug("Internal error.", e); //$NON-NLS-1$
                }
            }
            if (dh.version != null) {
                if (dh.version.equals("2.1")) { //$NON-NLS-1$
                    r.nextTag();
                    _load_2_1(model, r);
                    return dh;

                }
                if (dh.version.startsWith("2.2")) { //$NON-NLS-1$
                    r.nextTag();
                    _load_2_2(model, r);
                    return dh;
                }
            }
            throw new IOException("XML gtd-free data with version number " + dh.version //$NON-NLS-1$
                    + " can not be imported. Data version is newer then supported versions. Update your GTD-Free application to latest version."); //$NON-NLS-1$
        }

        _load_1_0(model, r);

        return null;

    } catch (XMLStreamException e) {
        if (e.getNestedException() != null) {
            Logger.getLogger(GTDDataXMLTools.class).debug("Parse error.", e.getNestedException()); //$NON-NLS-1$
        } else {
            Logger.getLogger(GTDDataXMLTools.class).debug("Parse error.", e); //$NON-NLS-1$
        }
        throw e;
    } catch (IOException e) {
        throw e;
    } finally {
        model.setSuspendedForMultipleChanges(false);
        model.getDataRepository().suspend(false);
    }

}