Create a CharacterIterator to read files
This commit is contained in:
parent
703e5d1a51
commit
6acd40719f
|
@ -1,5 +1,7 @@
|
||||||
package io.github.lonamiwebs.overgram.parser;
|
package io.github.lonamiwebs.overgram.parser;
|
||||||
|
|
||||||
|
import io.github.lonamiwebs.overgram.utils.CharacterIterator;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
@ -8,7 +10,6 @@ import java.util.List;
|
||||||
|
|
||||||
public class TLReader {
|
public class TLReader {
|
||||||
|
|
||||||
private static final int BUFFER_SIZE = 4096;
|
|
||||||
|
|
||||||
public static List<TLObject> readTlObjects(final File file) {
|
public static List<TLObject> readTlObjects(final File file) {
|
||||||
final List<String> lines = readFileWithDelimiter(new File("telegram_api.tl"), ';');
|
final List<String> lines = readFileWithDelimiter(new File("telegram_api.tl"), ';');
|
||||||
|
@ -22,18 +23,14 @@ public class TLReader {
|
||||||
|
|
||||||
private static List<String> readFileWithDelimiter(final File file, final char delimiter) {
|
private static List<String> readFileWithDelimiter(final File file, final char delimiter) {
|
||||||
final List<String> result = new ArrayList<>();
|
final List<String> result = new ArrayList<>();
|
||||||
try (final FileReader reader = new FileReader(file)) {
|
try (final CharacterIterator iter = new CharacterIterator(new FileReader(file))) {
|
||||||
int read;
|
|
||||||
final char[] buffer = new char[BUFFER_SIZE];
|
|
||||||
final StringBuilder builder = new StringBuilder();
|
final StringBuilder builder = new StringBuilder();
|
||||||
while ((read = reader.read(buffer)) > 0) {
|
for (char c : iter) {
|
||||||
for (int i = 0; i < read; ++i) {
|
if (c == delimiter) {
|
||||||
if (buffer[i] == delimiter) {
|
result.add(builder.toString());
|
||||||
result.add(builder.toString());
|
builder.setLength(0);
|
||||||
builder.setLength(0);
|
} else {
|
||||||
} else {
|
builder.append(c);
|
||||||
builder.append(buffer[i]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (IOException ignored) {
|
} catch (IOException ignored) {
|
||||||
|
|
|
@ -0,0 +1,61 @@
|
||||||
|
package io.github.lonamiwebs.overgram.utils;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
public class CharacterIterator implements Iterator<Character>, Iterable<Character>, AutoCloseable {
|
||||||
|
|
||||||
|
private static final int BUFFER_SIZE = 4096;
|
||||||
|
|
||||||
|
private final InputStreamReader reader;
|
||||||
|
private final char[] buffer;
|
||||||
|
private int index;
|
||||||
|
private int length;
|
||||||
|
|
||||||
|
public CharacterIterator(final InputStreamReader reader) {
|
||||||
|
this.reader = reader;
|
||||||
|
buffer = new char[BUFFER_SIZE];
|
||||||
|
index = 0;
|
||||||
|
readNext();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Iterator<Character> iterator() {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hasNext() {
|
||||||
|
if (index == BUFFER_SIZE) {
|
||||||
|
readNext();
|
||||||
|
index = 0;
|
||||||
|
}
|
||||||
|
return length != -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Character next() {
|
||||||
|
if (index == BUFFER_SIZE) {
|
||||||
|
readNext();
|
||||||
|
index = 0;
|
||||||
|
}
|
||||||
|
return buffer[index++];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() {
|
||||||
|
try {
|
||||||
|
reader.close();
|
||||||
|
} catch (Exception ignored) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void readNext() {
|
||||||
|
try {
|
||||||
|
length = reader.read(buffer);
|
||||||
|
} catch (IOException ignored) {
|
||||||
|
length = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue