2013-01-13 22:08:24 +01:00
|
|
|
package com.earth2me.essentials.textreader;
|
|
|
|
|
2015-04-15 06:06:16 +02:00
|
|
|
import net.ess3.api.IEssentials;
|
|
|
|
|
2020-10-03 19:46:05 +02:00
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.io.Reader;
|
2013-01-13 22:08:24 +01:00
|
|
|
import java.lang.ref.SoftReference;
|
2020-04-25 14:08:57 +02:00
|
|
|
import java.nio.charset.StandardCharsets;
|
2020-10-03 19:46:05 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Locale;
|
|
|
|
import java.util.Map;
|
2013-01-13 22:08:24 +01:00
|
|
|
|
2015-04-15 06:06:16 +02:00
|
|
|
public class BookInput implements IText {
|
2020-04-25 14:08:57 +02:00
|
|
|
private final static HashMap<String, SoftReference<BookInput>> cache = new HashMap<>();
|
2015-04-15 06:06:16 +02:00
|
|
|
private final transient List<String> lines;
|
|
|
|
private final transient List<String> chapters;
|
|
|
|
private final transient Map<String, Integer> bookmarks;
|
|
|
|
private final transient long lastChange;
|
2013-01-13 22:08:24 +01:00
|
|
|
|
2015-04-15 06:06:16 +02:00
|
|
|
public BookInput(final String filename, final boolean createFile, final IEssentials ess) throws IOException {
|
2013-01-13 22:08:24 +01:00
|
|
|
|
2015-04-15 06:06:16 +02:00
|
|
|
File file = null;
|
|
|
|
if (file == null || !file.exists()) {
|
|
|
|
file = new File(ess.getDataFolder(), filename + ".txt");
|
|
|
|
}
|
|
|
|
if (!file.exists()) {
|
|
|
|
if (createFile) {
|
2020-10-03 19:46:05 +02:00
|
|
|
try (final InputStream input = ess.getResource(filename + ".txt"); final OutputStream output = new FileOutputStream(file)) {
|
2015-04-15 06:06:16 +02:00
|
|
|
final byte[] buffer = new byte[1024];
|
|
|
|
int length = input.read(buffer);
|
|
|
|
while (length > 0) {
|
|
|
|
output.write(buffer, 0, length);
|
|
|
|
length = input.read(buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ess.getLogger().info("File " + filename + ".txt does not exist. Creating one for you.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!file.exists()) {
|
|
|
|
lastChange = 0;
|
|
|
|
lines = Collections.emptyList();
|
|
|
|
chapters = Collections.emptyList();
|
|
|
|
bookmarks = Collections.emptyMap();
|
|
|
|
throw new FileNotFoundException("Could not create " + filename + ".txt");
|
|
|
|
} else {
|
|
|
|
lastChange = file.lastModified();
|
2020-10-03 19:46:05 +02:00
|
|
|
final boolean readFromfile;
|
2015-04-15 06:06:16 +02:00
|
|
|
synchronized (cache) {
|
|
|
|
final SoftReference<BookInput> inputRef = cache.get(file.getName());
|
2020-10-03 19:46:05 +02:00
|
|
|
final BookInput input;
|
2015-04-15 06:06:16 +02:00
|
|
|
if (inputRef == null || (input = inputRef.get()) == null || input.lastChange < lastChange) {
|
2020-04-25 14:08:57 +02:00
|
|
|
lines = new ArrayList<>();
|
|
|
|
chapters = new ArrayList<>();
|
|
|
|
bookmarks = new HashMap<>();
|
|
|
|
cache.put(file.getName(), new SoftReference<>(this));
|
2015-04-15 06:06:16 +02:00
|
|
|
readFromfile = true;
|
|
|
|
} else {
|
|
|
|
lines = Collections.unmodifiableList(input.getLines());
|
|
|
|
chapters = Collections.unmodifiableList(input.getChapters());
|
|
|
|
bookmarks = Collections.unmodifiableMap(input.getBookmarks());
|
|
|
|
readFromfile = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (readFromfile) {
|
2020-04-25 14:08:57 +02:00
|
|
|
final Reader reader = new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8);
|
2015-04-15 06:06:16 +02:00
|
|
|
final BufferedReader bufferedReader = new BufferedReader(reader);
|
|
|
|
try {
|
|
|
|
int lineNumber = 0;
|
|
|
|
while (bufferedReader.ready()) {
|
|
|
|
final String line = bufferedReader.readLine();
|
|
|
|
if (line == null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (line.length() > 0 && line.charAt(0) == '#') {
|
|
|
|
bookmarks.put(line.substring(1).toLowerCase(Locale.ENGLISH).replaceAll("&[0-9a-fk]", ""), lineNumber);
|
|
|
|
chapters.add(line.substring(1).replace('&', '§').replace("§§", "&"));
|
|
|
|
}
|
|
|
|
lines.add(line.replace('&', '§').replace("§§", "&"));
|
|
|
|
lineNumber++;
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
reader.close();
|
|
|
|
bufferedReader.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-01-13 22:08:24 +01:00
|
|
|
|
2015-04-15 06:06:16 +02:00
|
|
|
@Override
|
|
|
|
public List<String> getLines() {
|
|
|
|
return lines;
|
|
|
|
}
|
2013-01-13 22:08:24 +01:00
|
|
|
|
2015-04-15 06:06:16 +02:00
|
|
|
@Override
|
|
|
|
public List<String> getChapters() {
|
|
|
|
return chapters;
|
|
|
|
}
|
2013-01-13 22:08:24 +01:00
|
|
|
|
2015-04-15 06:06:16 +02:00
|
|
|
@Override
|
|
|
|
public Map<String, Integer> getBookmarks() {
|
|
|
|
return bookmarks;
|
|
|
|
}
|
2013-01-13 22:08:24 +01:00
|
|
|
}
|