Example usage for java.nio CharBuffer limit

List of usage examples for java.nio CharBuffer limit

Introduction

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

Prototype

public final int limit() 

Source Link

Document

Returns the limit of this buffer.

Usage

From source file:MainClass.java

public static void main(String[] args) {
    String[] phrases = { "A", "B 1", "C 1.3" };
    String dirname = "C:/test";
    String filename = "Phrases.txt";
    File dir = new File(dirname);
    File aFile = new File(dir, filename);
    FileOutputStream outputFile = null;
    try {/*  w  w w .  j a  v  a  2s.c  om*/
        outputFile = new FileOutputStream(aFile, true);
    } catch (FileNotFoundException e) {
        e.printStackTrace(System.err);
    }
    FileChannel outChannel = outputFile.getChannel();
    ByteBuffer buf = ByteBuffer.allocate(1024);
    System.out.println(buf.position());
    System.out.println(buf.limit());
    System.out.println(buf.capacity());
    CharBuffer charBuf = buf.asCharBuffer();
    System.out.println(charBuf.position());
    System.out.println(charBuf.limit());
    System.out.println(charBuf.capacity());
    Formatter formatter = new Formatter(charBuf);
    int number = 0;
    for (String phrase : phrases) {
        formatter.format("%n %s", ++number, phrase);
        System.out.println(charBuf.position());
        System.out.println(charBuf.limit());
        System.out.println(charBuf.capacity());
        charBuf.flip();
        System.out.println(charBuf.position());
        System.out.println(charBuf.limit());
        System.out.println(charBuf.length());
        buf.limit(2 * charBuf.length()); // Set byte buffer limit
        System.out.println(buf.position());
        System.out.println(buf.limit());
        System.out.println(buf.remaining());
        try {
            outChannel.write(buf);
            buf.clear();
            charBuf.clear();
        } catch (IOException e) {
            e.printStackTrace(System.err);
        }
    }
    try {
        outputFile.close();
    } catch (IOException e) {
        e.printStackTrace(System.err);
    }
}

From source file:MainClass.java

public static void main(String[] args) {
    File aFile = new File("afile.txt");
    FileOutputStream outputFile = null;
    try {/*  w w  w .  ja va  2  s  .c  o m*/
        outputFile = new FileOutputStream(aFile, true);
        System.out.println("File stream created successfully.");
    } catch (FileNotFoundException e) {
        e.printStackTrace(System.err);
    }
    ByteBuffer buf = ByteBuffer.allocate(1024);
    System.out.println("\nByte buffer:");
    System.out.printf("position = %2d  Limit = %4d  capacity = %4d%n", buf.position(), buf.limit(),
            buf.capacity());

    // Create a view buffer
    CharBuffer charBuf = buf.asCharBuffer();
    System.out.println("Char view buffer:");
    System.out.printf("position = %2d  Limit = %4d  capacity = %4d%n", charBuf.position(), charBuf.limit(),
            charBuf.capacity());
    try {
        outputFile.close(); // Close the O/P stream & the channel
    } catch (IOException e) {
        e.printStackTrace(System.err);
    }
}

From source file:MainClass.java

private static void println(CharBuffer cb) {
    System.out.println("pos=" + cb.position() + ", limit=" + cb.limit() + ": '" + cb + "'");
}

From source file:MainClass.java

private static void println(CharBuffer cb) {
    System.out.println("pos=" + cb.position() + ", limit=" + cb.limit() + ", capacity=" + cb.capacity() + ": '"
            + cb + "'");
}

From source file:MainClass.java

private static void println(CharBuffer cb) {
    System.out.println("pos=" + cb.position() + ", limit=" + cb.limit() + ", capacity=" + cb.capacity()
            + ", arrayOffset=" + cb.arrayOffset());
}

From source file:it.geosolutions.geostore.core.security.password.SecurityUtils.java

/**
 * Converts byte array to char array./*from   www . j  a  va  2s  . c o  m*/
 */
public static char[] toChars(byte[] b, Charset charset) {
    CharBuffer buff = charset.decode(ByteBuffer.wrap(b));
    char[] tmp = new char[buff.limit()];
    buff.get(tmp);
    return tmp;
}

From source file:com.silverpeas.ical.StringUtils.java

static char[] decodeToArray(byte[] bytes, Charset encoding) {
    if (CharEncoding.US_ASCII.equals(encoding.name())) {
        char[] array = new char[bytes.length];
        for (int i = 0; i < array.length; i++) {
            array[i] = (char) bytes[i];
        }// w  w  w . j  a  v  a  2s.c o m
        return array;
    }
    try {
        CharBuffer buffer = encoding.newDecoder().decode(ByteBuffer.wrap(bytes));
        char[] array = new char[buffer.limit()];
        System.arraycopy(buffer.array(), 0, array, 0, array.length);
        return array;
    } catch (Exception nioException) {
        return (new String(bytes, encoding)).toCharArray();
    }
}

From source file:com.odiago.flumebase.io.CharBufferUtils.java

/**
 * Parses a CharSequence into an integer in base 10.
 *///  w  w w .  j  a  va  2 s  .co  m
public static int parseInt(CharBuffer chars) throws ColumnParseException {
    int result = 0;

    final int limit = chars.limit();
    final int start = chars.position();
    if (0 == limit - start) {
        // The empty string can not be parsed as an integer.
        throw new ColumnParseException("No value provided");
    }

    boolean isNegative = false;
    for (int pos = start; pos < limit; pos++) {
        char cur = chars.get();
        if (pos == start && cur == '-') {
            isNegative = true;
            if (limit - start == 1) {
                // "-" is not an integer we accept.
                throw new ColumnParseException("No integer part provided");
            }
        } else if (Character.isDigit(cur)) {
            byte digitVal = (byte) (cur - '0');
            result = result * 10 - digitVal;
            // TODO: Detect over/underflow and signal exception?
        } else {
            throw new ColumnParseException("Invalid character in number");
        }
    }

    // We built up the value as a negative, to use the larger "half" of the
    // integer range. If it's not negative, flip it on return.
    return isNegative ? result : -result;
}

From source file:com.odiago.flumebase.io.CharBufferUtils.java

/**
 * Parses a CharSequence into a long in base 10.
 *///from w ww .  ja v a  2 s . c  o m
public static long parseLong(CharBuffer chars) throws ColumnParseException {
    long result = 0L;

    final int limit = chars.limit();
    final int start = chars.position();
    if (0 == limit - start) {
        // The empty string can not be parsed as an integer.
        throw new ColumnParseException("No value provided");
    }

    boolean isNegative = false;
    for (int pos = start; pos < limit; pos++) {
        char cur = chars.get();
        if (pos == start && cur == '-') {
            isNegative = true;
            if (limit - start == 1) {
                // "-" is not an integer we accept.
                throw new ColumnParseException("No integer part provided");
            }
        } else if (Character.isDigit(cur)) {
            byte digitVal = (byte) (cur - '0');
            result = result * 10 - digitVal;
            // TODO: Detect over/underflow and signal exception?
        } else {
            throw new ColumnParseException("Invalid character in number");
        }
    }

    // We built up the value as a negative, to use the larger "half" of the
    // integer range. If it's not negative, flip it on return.
    return isNegative ? result : -result;
}

From source file:com.healthmarketscience.jackcess.impl.office.EncryptionHeader.java

private static String readCspName(ByteBuffer buffer) {

    // unicode string, must be multiple of 2
    int rem = (buffer.remaining() / 2) * 2;
    String cspName = "";
    if (rem > 0) {

        ByteBuffer cspNameBuf = ByteBuffer.wrap(ByteUtil.getBytes(buffer, rem));
        CharBuffer tmpCspName = UNICODE_CHARSET.decode(cspNameBuf);

        // should be null terminated, strip that
        for (int i = 0; i < tmpCspName.limit(); ++i) {
            if (tmpCspName.charAt(i) == '\0') {
                tmpCspName.limit(i);/*from  www . j a va  2 s .co  m*/
                break;
            }
        }

        cspName = tmpCspName.toString();
    }

    return cspName;
}