Skip to content

Commit

Permalink
Draft: CharArrayReader
Browse files Browse the repository at this point in the history
  • Loading branch information
mkarg committed Oct 12, 2024
1 parent 487b938 commit 7420a15
Show file tree
Hide file tree
Showing 3 changed files with 246 additions and 86 deletions.
101 changes: 16 additions & 85 deletions src/java.base/share/classes/java/io/CharArrayReader.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -36,29 +36,15 @@
* @since 1.1
*/
public class CharArrayReader extends Reader {
/** The character buffer. */
protected char[] buf;

/** The current buffer position. */
protected int pos;

/** The position of mark in buffer. */
protected int markedPos = 0;

/**
* The index of the end of this buffer. There is no valid
* data at or beyond this index.
*/
protected int count;
private final Reader r;

/**
* Creates a CharArrayReader from the specified array of chars.
* @param buf Input buffer (not copied)
*/
public CharArrayReader(char[] buf) {
this.buf = buf;
this.pos = 0;
this.count = buf.length;
this(buf, 0, buf.length);
}

/**
Expand All @@ -79,20 +65,7 @@ public CharArrayReader(char[] buf) {
* @param length Number of chars to read
*/
public CharArrayReader(char[] buf, int offset, int length) {
if ((offset < 0) || (offset > buf.length) || (length < 0) ||
((offset + length) < 0)) {
throw new IllegalArgumentException();
}
this.buf = buf;
this.pos = offset;
this.count = Math.min(offset + length, buf.length);
this.markedPos = offset;
}

/** Checks to make sure that the stream has not been closed */
private void ensureOpen() throws IOException {
if (buf == null)
throw new IOException("Stream closed");
r = Reader.of(buf, offset, length);
}

/**
Expand All @@ -102,11 +75,7 @@ private void ensureOpen() throws IOException {
*/
public int read() throws IOException {
synchronized (lock) {
ensureOpen();
if (pos >= count)
return -1;
else
return buf[pos++];
return r.read();
}
}

Expand All @@ -130,43 +99,14 @@ public int read() throws IOException {
*/
public int read(char[] cbuf, int off, int len) throws IOException {
synchronized (lock) {
ensureOpen();
Objects.checkFromIndexSize(off, len, cbuf.length);
if (len == 0) {
return 0;
}

if (pos >= count) {
return -1;
}

int avail = count - pos;
if (len > avail) {
len = avail;
}
if (len <= 0) {
return 0;
}
System.arraycopy(buf, pos, cbuf, off, len);
pos += len;
return len;
return r.read(cbuf, off, len);
}
}

@Override
public int read(CharBuffer target) throws IOException {
synchronized (lock) {
ensureOpen();

if (pos >= count) {
return -1;
}

int avail = count - pos;
int len = Math.min(avail, target.remaining());
target.put(buf, pos, len);
pos += len;
return len;
return r.read(target);
}
}

Expand All @@ -187,17 +127,7 @@ public int read(CharBuffer target) throws IOException {
*/
public long skip(long n) throws IOException {
synchronized (lock) {
ensureOpen();

long avail = count - pos;
if (n > avail) {
n = avail;
}
if (n < 0) {
return 0;
}
pos += (int) n;
return n;
return r.skip(n);
}
}

Expand All @@ -209,8 +139,7 @@ public long skip(long n) throws IOException {
*/
public boolean ready() throws IOException {
synchronized (lock) {
ensureOpen();
return (count - pos) > 0;
return r.ready();
}
}

Expand All @@ -235,8 +164,7 @@ public boolean markSupported() {
*/
public void mark(int readAheadLimit) throws IOException {
synchronized (lock) {
ensureOpen();
markedPos = pos;
r.mark(readAheadLimit);
}
}

Expand All @@ -248,8 +176,7 @@ public void mark(int readAheadLimit) throws IOException {
*/
public void reset() throws IOException {
synchronized (lock) {
ensureOpen();
pos = markedPos;
r.reset();
}
}

Expand All @@ -262,7 +189,11 @@ public void reset() throws IOException {
*/
public void close() {
synchronized (lock) {
buf = null;
try {
r.close();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}
}
Loading

0 comments on commit 7420a15

Please sign in to comment.