-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
437 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
package com.cedarsoftware.util; | ||
|
||
import java.io.IOException; | ||
import java.io.Reader; | ||
|
||
public class FastReader extends Reader { | ||
private Reader in; | ||
private char[] buf; | ||
private int bufferSize; | ||
private int pushbackBufferSize; | ||
private int position; // Current position in the buffer | ||
private int limit; // Number of characters currently in the buffer | ||
private char[] pushbackBuffer; | ||
private int pushbackPosition; // Current position in the pushback buffer | ||
private int line = 1; | ||
private int col = 0; | ||
|
||
public FastReader(Reader in, int bufferSize, int pushbackBufferSize) { | ||
super(in); | ||
if (bufferSize <= 0 || pushbackBufferSize < 0) { | ||
throw new IllegalArgumentException("Buffer sizes must be positive"); | ||
} | ||
this.in = in; | ||
this.bufferSize = bufferSize; | ||
this.pushbackBufferSize = pushbackBufferSize; | ||
this.buf = new char[bufferSize]; | ||
this.pushbackBuffer = new char[pushbackBufferSize]; | ||
this.position = 0; | ||
this.limit = 0; | ||
this.pushbackPosition = pushbackBufferSize; // Start from the end of pushbackBuffer | ||
} | ||
|
||
private void fill() throws IOException { | ||
if (position >= limit) { | ||
limit = in.read(buf, 0, bufferSize); | ||
if (limit > 0) { | ||
position = 0; | ||
} | ||
} | ||
} | ||
|
||
public void pushback(char ch) throws IOException { | ||
if (pushbackPosition == 0) { | ||
throw new IOException("Pushback buffer overflow"); | ||
} | ||
pushbackBuffer[--pushbackPosition] = ch; | ||
if (ch == 0x0a) { | ||
line--; | ||
} | ||
else { | ||
col--; | ||
} | ||
} | ||
|
||
protected void movePosition(char ch) | ||
{ | ||
if (ch == 0x0a) { | ||
line++; | ||
col = 0; | ||
} | ||
else { | ||
col++; | ||
} | ||
} | ||
|
||
public int read() throws IOException { | ||
if (in == null) { | ||
throw new IOException("FastReader stream is closed."); | ||
} | ||
char ch; | ||
if (pushbackPosition < pushbackBufferSize) { | ||
ch = pushbackBuffer[pushbackPosition++]; | ||
movePosition(ch); | ||
return ch; | ||
} | ||
|
||
fill(); | ||
if (limit == -1) { | ||
return -1; | ||
} | ||
|
||
ch = buf[position++]; | ||
movePosition(ch); | ||
return ch; | ||
} | ||
|
||
public int read(char[] cbuf, int off, int len) throws IOException { | ||
if (in == null) { | ||
throw new IOException("FastReader stream is closed."); | ||
} | ||
int bytesRead = 0; | ||
|
||
while (len > 0) { | ||
int available = pushbackBufferSize - pushbackPosition; | ||
if (available > 0) { | ||
int toRead = Math.min(available, len); | ||
System.arraycopy(pushbackBuffer, pushbackPosition, cbuf, off, toRead); | ||
pushbackPosition += toRead; | ||
off += toRead; | ||
len -= toRead; | ||
bytesRead += toRead; | ||
} else { | ||
fill(); | ||
if (limit == -1) { | ||
return bytesRead > 0 ? bytesRead : -1; | ||
} | ||
int toRead = Math.min(limit - position, len); | ||
System.arraycopy(buf, position, cbuf, off, toRead); | ||
position += toRead; | ||
off += toRead; | ||
len -= toRead; | ||
bytesRead += toRead; | ||
} | ||
} | ||
|
||
return bytesRead; | ||
} | ||
|
||
public void close() throws IOException { | ||
if (in != null) { | ||
in.close(); | ||
in = null; | ||
} | ||
} | ||
|
||
public int getLine() | ||
{ | ||
return line; | ||
} | ||
|
||
public int getCol() | ||
{ | ||
return col; | ||
} | ||
|
||
public String getLastSnippet() | ||
{ | ||
StringBuilder s = new StringBuilder(); | ||
for (int i=0; i < position; i++) | ||
{ | ||
s.append(buf[i]); | ||
} | ||
return s.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package com.cedarsoftware.util; | ||
|
||
import java.io.IOException; | ||
import java.io.Writer; | ||
|
||
public class FastWriter extends Writer { | ||
private static final int DEFAULT_BUFFER_SIZE = 8192; | ||
|
||
private Writer out; | ||
private char[] cb; | ||
private int nextChar; | ||
|
||
public FastWriter(Writer out) { | ||
this(out, DEFAULT_BUFFER_SIZE); | ||
} | ||
|
||
public FastWriter(Writer out, int bufferSize) { | ||
super(out); | ||
if (bufferSize <= 0) { | ||
throw new IllegalArgumentException("Buffer size <= 0"); | ||
} | ||
this.out = out; | ||
this.cb = new char[bufferSize]; | ||
this.nextChar = 0; | ||
} | ||
|
||
private void flushBuffer() throws IOException { | ||
if (nextChar == 0) { | ||
return; | ||
} | ||
out.write(cb, 0, nextChar); | ||
nextChar = 0; | ||
} | ||
|
||
public void write(int c) throws IOException { | ||
if (out == null) { | ||
throw new IOException("FastWriter stream is closed."); | ||
} | ||
if (nextChar >= cb.length) { | ||
flushBuffer(); | ||
} | ||
cb[nextChar++] = (char) c; | ||
} | ||
|
||
public void write(char[] cbuf, int off, int len) throws IOException { | ||
if (out == null) { | ||
throw new IOException("FastWriter stream is closed."); | ||
} | ||
if ((off < 0) || (off > cbuf.length) || (len < 0) || | ||
((off + len) > cbuf.length) || ((off + len) < 0)) { | ||
throw new IndexOutOfBoundsException(); | ||
} else if (len == 0) { | ||
return; | ||
} | ||
if (len >= cb.length) { | ||
// If the request length exceeds the size of the output buffer, | ||
// flush the buffer and then write the data directly. | ||
flushBuffer(); | ||
out.write(cbuf, off, len); | ||
return; | ||
} | ||
if (len > cb.length - nextChar) { | ||
flushBuffer(); | ||
} | ||
System.arraycopy(cbuf, off, cb, nextChar, len); | ||
nextChar += len; | ||
} | ||
|
||
public void write(String str, int off, int len) throws IOException { | ||
if (out == null) { | ||
throw new IOException("FastWriter stream is closed."); | ||
} | ||
int b = off, t = off + len; | ||
while (b < t) { | ||
int d = Math.min(cb.length - nextChar, t - b); | ||
str.getChars(b, b + d, cb, nextChar); | ||
b += d; | ||
nextChar += d; | ||
if (nextChar >= cb.length) { | ||
flushBuffer(); | ||
} | ||
} | ||
} | ||
|
||
public void flush() throws IOException { | ||
flushBuffer(); | ||
out.flush(); | ||
} | ||
|
||
public void close() throws IOException { | ||
if (out == null) { | ||
return; | ||
} | ||
try { | ||
flushBuffer(); | ||
} finally { | ||
out.close(); | ||
out = null; | ||
cb = null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,4 +75,5 @@ public static boolean checkContainsIgnoreCase(String source, String... contains) | |
} | ||
return true; | ||
} | ||
|
||
} |
Oops, something went wrong.