io.github.green4j.jelly.CharArrayWriter Maven / Gradle / Ivy
/**
* MIT License
*
* Copyright (c) 2018-2024 Anatoly Gudkov and others.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package io.github.green4j.jelly;
public class CharArrayWriter implements
ClearableBufferingWriter,
CharArray {
private final Frame frame = new Frame() {
@Override
public void setCharAt(final int index, final char c) {
array[frameStart + index] = c;
}
@Override
public char charAt(final int index) {
return array[frameStart + index];
}
@Override
public CharSequence subSequence(final int start, final int end) {
throw new UnsupportedOperationException();
}
@Override
public int length() {
return frameSize;
}
@Override
public String toString() {
return new String(array, frameStart, frameSize);
}
};
private char[] array;
private int start;
private int length;
private int frameStart;
private int frameSize;
public CharArrayWriter() {
}
public CharArrayWriter(final int initialSize) {
this(new char[initialSize]);
}
public CharArrayWriter(final char[] array) {
this(array, 0);
}
public CharArrayWriter(final char[] array, final int start) {
set(array, start);
}
public void set(final char[] array) {
set(array, 0);
}
public void set(final char[] array, final int start) {
assert array.length > start;
this.array = array;
this.start = start;
this.length = 0;
}
@Override
public char[] array() {
return array;
}
@Override
public int start() {
return start;
}
@Override
public int length() {
return length;
}
@Override
public void clear() {
length = 0;
}
/**
* Returns a Frame of required size starting from the current position.
* @param size of the buffer to pre-allocate
* @return a Frame
*/
@Override
public Frame append(final int size) {
assert size > 0;
makeSureRoomSize(size);
frameStart = start + length;
frameSize = size;
length += size;
return frame;
}
@Override
public void append(final char c) {
assert array != null;
makeSureRoomSize(1);
array[start + length] = c;
length++;
}
@Override
public void append(final CharSequence data) {
if (data == null) {
appendNull();
return;
}
append(data, 0, data.length());
}
@Override
public void append(final CharSequence data, final int start, final int len) {
assert array != null;
makeSureRoomSize(len);
final int startIndex = this.start + length;
for (int i = 0; i < len; i++) {
array[startIndex + i] = data.charAt(start + i);
}
length += len;
}
@Override
public void flush() {
}
private void appendNull() {
makeSureRoomSize(4);
int charIndex = start + length;
array[charIndex++] = 'n';
array[charIndex++] = 'u';
array[charIndex++] = 'l';
array[charIndex] = 'l';
length += 4;
}
private void makeSureRoomSize(final int roomSize) {
final int delta = array.length - (start + length + roomSize);
if (delta < 0) {
final int newSize = Math.max(roomSize, array.length - start) << 1;
final char[] newArray = new char[newSize];
System.arraycopy(array, start, newArray, 0, length);
array = newArray;
start = 0;
}
}
@Override
public String toString() {
if (array == null) {
return "null";
}
return new String(array, start, length);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy