com.liferay.portal.kernel.io.unsync.UnsyncStringReader.java Source code

Java tutorial

Introduction

Here is the source code for com.liferay.portal.kernel.io.unsync.UnsyncStringReader.java

Source

/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.liferay.portal.kernel.io.unsync;

import java.io.IOException;
import java.io.Reader;

import java.nio.CharBuffer;

/**
 * <p>
 * See https://issues.liferay.com/browse/LPS-6648.
 * </p>
 *
 * @author Shuyang Zhou
 */
public class UnsyncStringReader extends Reader {

    public UnsyncStringReader(String string) {
        this.string = string;

        stringLength = string.length();
    }

    @Override
    public void close() {
        string = null;
    }

    @Override
    public void mark(int readAheadLimit) throws IOException {
        if (string == null) {
            throw new IOException("String is null");
        }

        markIndex = index;
    }

    @Override
    public boolean markSupported() {
        return true;
    }

    @Override
    public int read() throws IOException {
        if (string == null) {
            throw new IOException("String is null");
        }

        if (index >= stringLength) {
            return -1;
        }

        return string.charAt(index++);
    }

    @Override
    public int read(char[] chars) throws IOException {
        return read(chars, 0, chars.length);
    }

    @Override
    public int read(char[] chars, int offset, int length) throws IOException {
        if (string == null) {
            throw new IOException("String is null");
        }

        if (length <= 0) {
            return 0;
        }

        if (index >= stringLength) {
            return -1;
        }

        int read = length;

        if ((index + read) > stringLength) {
            read = stringLength - index;
        }

        string.getChars(index, index + read, chars, offset);

        index += read;

        return read;
    }

    @Override
    public int read(CharBuffer charBuffer) throws IOException {
        int remaining = charBuffer.remaining();

        char[] chars = new char[remaining];

        int read = read(chars, 0, remaining);

        if (read > 0) {
            charBuffer.put(chars, 0, read);
        }

        return read;
    }

    @Override
    public boolean ready() throws IOException {
        if (string == null) {
            throw new IOException("String is null");
        }

        return true;
    }

    @Override
    public void reset() throws IOException {
        if (string == null) {
            throw new IOException("String is null");
        }

        index = markIndex;
    }

    @Override
    public long skip(long skip) {
        if (index >= stringLength) {
            return 0;
        }

        if ((skip + index) > stringLength) {
            skip = stringLength - index;
        }

        index += skip;

        return skip;
    }

    protected int index;
    protected int markIndex;
    protected String string;
    protected int stringLength;

}