Search in sources :

Example 86 with CharBuffer

use of java.nio.CharBuffer in project graphdb by neo4j-attic.

the class AbstractDynamicStore method allocateRecords.

public Collection<DynamicRecord> allocateRecords(long startBlock, char[] src) {
    assert getFileChannel() != null : "Store closed, null file channel";
    assert src != null : "Null src argument";
    List<DynamicRecord> recordList = new LinkedList<DynamicRecord>();
    long nextBlock = startBlock;
    long prevBlock = Record.NO_PREV_BLOCK.intValue();
    int srcOffset = 0;
    int dataSize = getBlockSize() - BLOCK_HEADER_SIZE;
    do {
        DynamicRecord record = new DynamicRecord(nextBlock);
        record.setCreated();
        record.setInUse(true);
        assert prevBlock != nextBlock;
        record.setPrevBlock(prevBlock);
        if ((src.length - srcOffset) * 2 > dataSize) {
            byte[] data = new byte[dataSize];
            CharBuffer charBuf = ByteBuffer.wrap(data).asCharBuffer();
            charBuf.put(src, srcOffset, dataSize / 2);
            record.setData(data);
            prevBlock = nextBlock;
            nextBlock = nextBlockId();
            record.setNextBlock(nextBlock);
            srcOffset += dataSize / 2;
        } else {
            if (srcOffset == 0) {
                record.setCharData(src);
            } else {
                byte[] data = new byte[(src.length - srcOffset) * 2];
                CharBuffer charBuf = ByteBuffer.wrap(data).asCharBuffer();
                charBuf.put(src, srcOffset, src.length - srcOffset);
                record.setData(data);
            }
            nextBlock = Record.NO_NEXT_BLOCK.intValue();
            record.setNextBlock(nextBlock);
        }
        recordList.add(record);
    } while (nextBlock != Record.NO_NEXT_BLOCK.intValue());
    return recordList;
}
Also used : CharBuffer(java.nio.CharBuffer) LinkedList(java.util.LinkedList)

Example 87 with CharBuffer

use of java.nio.CharBuffer in project XobotOS by xamarin.

the class OutputStreamWriter method write.

/**
     * Writes {@code count} characters starting at {@code offset} in {@code buf}
     * to this writer. The characters are immediately converted to bytes by the
     * character converter and stored in a local buffer. If the buffer gets full
     * as a result of the conversion, this writer is flushed.
     *
     * @param buffer
     *            the array containing characters to write.
     * @param offset
     *            the index of the first character in {@code buf} to write.
     * @param count
     *            the maximum number of characters to write.
     * @throws IndexOutOfBoundsException
     *             if {@code offset < 0} or {@code count < 0}, or if
     *             {@code offset + count} is greater than the size of
     *             {@code buf}.
     * @throws IOException
     *             if this writer has already been closed or another I/O error
     *             occurs.
     */
@Override
public void write(char[] buffer, int offset, int count) throws IOException {
    synchronized (lock) {
        checkStatus();
        Arrays.checkOffsetAndCount(buffer.length, offset, count);
        CharBuffer chars = CharBuffer.wrap(buffer, offset, count);
        convert(chars);
    }
}
Also used : CharBuffer(java.nio.CharBuffer)

Example 88 with CharBuffer

use of java.nio.CharBuffer in project XobotOS by xamarin.

the class OutputStreamWriter method write.

/**
     * Writes {@code count} characters starting at {@code offset} in {@code str}
     * to this writer. The characters are immediately converted to bytes by the
     * character converter and stored in a local buffer. If the buffer gets full
     * as a result of the conversion, this writer is flushed.
     *
     * @param str
     *            the string containing characters to write.
     * @param offset
     *            the start position in {@code str} for retrieving characters.
     * @param count
     *            the maximum number of characters to write.
     * @throws IOException
     *             if this writer has already been closed or another I/O error
     *             occurs.
     * @throws IndexOutOfBoundsException
     *             if {@code offset < 0} or {@code count < 0}, or if
     *             {@code offset + count} is bigger than the length of
     *             {@code str}.
     */
@Override
public void write(String str, int offset, int count) throws IOException {
    synchronized (lock) {
        if (count < 0) {
            throw new StringIndexOutOfBoundsException(str, offset, count);
        }
        if (str == null) {
            throw new NullPointerException("str == null");
        }
        if ((offset | count) < 0 || offset > str.length() - count) {
            throw new StringIndexOutOfBoundsException(str, offset, count);
        }
        checkStatus();
        CharBuffer chars = CharBuffer.wrap(str, offset, count + offset);
        convert(chars);
    }
}
Also used : CharBuffer(java.nio.CharBuffer)

Example 89 with CharBuffer

use of java.nio.CharBuffer in project android_frameworks_base by ResurrectionRemix.

the class FastPrintWriter method flushLocked.

private void flushLocked() 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 (!mIoError) {
                if (result.isError()) {
                    throw new IOException(result.toString());
                } else if (result.isOverflow()) {
                    flushBytesLocked();
                    result = mCharset.encode(charBuffer, mBytes, true);
                    continue;
                }
                break;
            }
            if (!mIoError) {
                flushBytesLocked();
                mOutputStream.flush();
            }
        } else if (mWriter != null) {
            if (!mIoError) {
                mWriter.write(mText, 0, mPos);
                mWriter.flush();
            }
        } else {
            int nonEolOff = 0;
            final int sepLen = mSeparator.length();
            final int len = sepLen < mPos ? sepLen : mPos;
            while (nonEolOff < len && mText[mPos - 1 - nonEolOff] == mSeparator.charAt(mSeparator.length() - 1 - nonEolOff)) {
                nonEolOff++;
            }
            if (nonEolOff >= mPos) {
                mPrinter.println("");
            } else {
                mPrinter.println(new String(mText, 0, mPos - nonEolOff));
            }
        }
        mPos = 0;
    }
}
Also used : CharBuffer(java.nio.CharBuffer) IOException(java.io.IOException) CoderResult(java.nio.charset.CoderResult)

Example 90 with CharBuffer

use of java.nio.CharBuffer in project android_frameworks_base by ResurrectionRemix.

the class StrictJarManifest method writeEntry.

private static void writeEntry(OutputStream os, Attributes.Name name, String value, CharsetEncoder encoder, ByteBuffer bBuf) throws IOException {
    String nameString = name.toString();
    os.write(nameString.getBytes(StandardCharsets.US_ASCII));
    os.write(VALUE_SEPARATOR);
    encoder.reset();
    bBuf.clear().limit(LINE_LENGTH_LIMIT - nameString.length() - 2);
    CharBuffer cBuf = CharBuffer.wrap(value);
    while (true) {
        CoderResult r = encoder.encode(cBuf, bBuf, true);
        if (CoderResult.UNDERFLOW == r) {
            r = encoder.flush(bBuf);
        }
        os.write(bBuf.array(), bBuf.arrayOffset(), bBuf.position());
        os.write(LINE_SEPARATOR);
        if (CoderResult.UNDERFLOW == r) {
            break;
        }
        os.write(' ');
        bBuf.clear().limit(LINE_LENGTH_LIMIT - 1);
    }
}
Also used : CharBuffer(java.nio.CharBuffer) CoderResult(java.nio.charset.CoderResult)

Aggregations

CharBuffer (java.nio.CharBuffer)401 ByteBuffer (java.nio.ByteBuffer)152 CoderResult (java.nio.charset.CoderResult)83 CharsetDecoder (java.nio.charset.CharsetDecoder)47 IOException (java.io.IOException)45 Charset (java.nio.charset.Charset)33 Test (org.junit.Test)23 CharacterCodingException (java.nio.charset.CharacterCodingException)15 CharsetEncoder (java.nio.charset.CharsetEncoder)15 FileInputStream (java.io.FileInputStream)11 IntBuffer (java.nio.IntBuffer)10 Reader (java.io.Reader)9 BufferOverflowException (java.nio.BufferOverflowException)9 DoubleBuffer (java.nio.DoubleBuffer)9 FloatBuffer (java.nio.FloatBuffer)9 LongBuffer (java.nio.LongBuffer)9 ShortBuffer (java.nio.ShortBuffer)9 BufferUnderflowException (java.nio.BufferUnderflowException)7 ValueWrapper (org.apache.geode.internal.memcached.ValueWrapper)7 InputStream (java.io.InputStream)6