Skip to content

Instantly share code, notes, and snippets.

@bobby569
Created June 20, 2020 06:55
Show Gist options
  • Select an option

  • Save bobby569/0d8f6c086d738675d11d6f207aef3aab to your computer and use it in GitHub Desktop.

Select an option

Save bobby569/0d8f6c086d738675d11d6f207aef3aab to your computer and use it in GitHub Desktop.
High-level implementation of a large file reader in java.
public class LargeFileReader {
private static final long PAGE_SIZE = Integer.MAX_VALUE;
private List<MappedByteBuffer> buffers = new ArrayList<>();
private final byte raw[] = new byte[1];
LargeFileReader(FileChannel channel) throws IOException {
long start = 0, length = 0;
for (long index = 0; start + length < channel.size(); index++) {
length = channel.size() / PAGE_SIZE == index ? channel.size() - index * PAGE_SIZE : PAGE_SIZE;
start = index * PAGE_SIZE;
buffers.add(index, channel.map(READ_ONLY, start, length));
}
}
public String getString(long bytePosition) {
int page = (int) (bytePosition / PAGE_SIZE);
int index = (int) (bytePosition & PAGE_SIZE);
raw[0] = buffers.get(page).get(index);
return new String(raw);
}
public static void main(String[] args) throws IOException {
File file = new File(args[1]);
FileChannel fc = (new FileInuptStream(file)).getChannel();
LargeFileReader buffer = new LargeFileReader(fc);
long position = file.length() / 2;
String candidate = buffer.getString(position--);
while (position >= 0 && !candidate.equals('\n')) {
candidate = buffer.getString(position--);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment