| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.dev.util; |
| 7 |
|
| 8 |
import java.io.ByteArrayOutputStream; |
| 9 |
import java.io.IOException; |
| 10 |
import java.io.RandomAccessFile; |
| 11 |
import java.sql.SQLException; |
| 12 |
import java.util.ArrayList; |
| 13 |
import java.util.concurrent.TimeUnit; |
| 14 |
|
| 15 |
import org.h2.message.DbException; |
| 16 |
import org.h2.util.Tool; |
| 17 |
|
| 18 |
/** |
| 19 |
* A text file viewer that support very large files. |
| 20 |
*/ |
| 21 |
public class FileViewer extends Tool { |
| 22 |
|
| 23 |
/** |
| 24 |
* Run the viewer. |
| 25 |
* |
| 26 |
* @param args the command line arguments |
| 27 |
*/ |
| 28 |
public static void main(String... args) throws SQLException { |
| 29 |
new FileViewer().runTool(args); |
| 30 |
} |
| 31 |
|
| 32 |
@Override |
| 33 |
protected void showUsage() { |
| 34 |
out.println("A text file viewer that support very large files."); |
| 35 |
out.println("java "+getClass().getName() + "\n" + |
| 36 |
" -file <file> The name of the file to view\n" + |
| 37 |
" [-find <text>] Find a string and display the next lines\n" + |
| 38 |
" [-start <x>] Start at the given position\n" + |
| 39 |
" [-head] Display the first lines\n" + |
| 40 |
" [-tail] Display the last lines\n" + |
| 41 |
" [-lines <x>] Display only x lines (default: 30)\n" + |
| 42 |
" [-quiet] Do not print progress information"); |
| 43 |
} |
| 44 |
|
| 45 |
@Override |
| 46 |
public void runTool(String... args) throws SQLException { |
| 47 |
String file = null; |
| 48 |
String find = null; |
| 49 |
boolean head = false, tail = false; |
| 50 |
int lines = 30; |
| 51 |
boolean quiet = false; |
| 52 |
long start = 0; |
| 53 |
for (int i = 0; args != null && i < args.length; i++) { |
| 54 |
String arg = args[i]; |
| 55 |
if (arg.equals("-file")) { |
| 56 |
file = args[++i]; |
| 57 |
} else if (arg.equals("-find")) { |
| 58 |
find = args[++i]; |
| 59 |
} else if (arg.equals("-start")) { |
| 60 |
start = Long.decode(args[++i]); |
| 61 |
} else if (arg.equals("-head")) { |
| 62 |
head = true; |
| 63 |
} else if (arg.equals("-tail")) { |
| 64 |
tail = true; |
| 65 |
} else if (arg.equals("-lines")) { |
| 66 |
lines = Integer.decode(args[++i]); |
| 67 |
} else if (arg.equals("-quiet")) { |
| 68 |
quiet = true; |
| 69 |
} else if (arg.equals("-help") || arg.equals("-?")) { |
| 70 |
showUsage(); |
| 71 |
return; |
| 72 |
} else { |
| 73 |
showUsageAndThrowUnsupportedOption(arg); |
| 74 |
} |
| 75 |
} |
| 76 |
if (file == null) { |
| 77 |
showUsage(); |
| 78 |
return; |
| 79 |
} |
| 80 |
if (!head && !tail && find == null) { |
| 81 |
head = true; |
| 82 |
} |
| 83 |
try { |
| 84 |
process(file, find, head, tail, start, lines, quiet); |
| 85 |
} catch (IOException e) { |
| 86 |
throw DbException.toSQLException(e); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
private static void process(String fileName, String find, |
| 91 |
boolean head, boolean tail, long start, int lines, |
| 92 |
boolean quiet) throws IOException { |
| 93 |
RandomAccessFile file = new RandomAccessFile(fileName, "r"); |
| 94 |
long length = file.length(); |
| 95 |
if (head) { |
| 96 |
file.seek(start); |
| 97 |
list(start, "Head", readLines(file, lines)); |
| 98 |
} |
| 99 |
if (find != null) { |
| 100 |
file.seek(start); |
| 101 |
long pos = find(file, find.getBytes(), quiet); |
| 102 |
if (pos >= 0) { |
| 103 |
file.seek(pos); |
| 104 |
list(pos, "Found " + find, readLines(file, lines)); |
| 105 |
} |
| 106 |
} |
| 107 |
if (tail) { |
| 108 |
long pos = length - 100L * lines; |
| 109 |
ArrayList<String> list = null; |
| 110 |
while (pos > 0) { |
| 111 |
file.seek(pos); |
| 112 |
list = readLines(file, Integer.MAX_VALUE); |
| 113 |
if (list.size() > lines) { |
| 114 |
break; |
| 115 |
} |
| 116 |
pos -= 100L * lines; |
| 117 |
} |
| 118 |
// remove the first (maybe partial) line |
| 119 |
list.remove(0); |
| 120 |
while (list.size() > lines) { |
| 121 |
list.remove(0); |
| 122 |
} |
| 123 |
list(pos, "Tail", list); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
private static long find(RandomAccessFile file, byte[] find, boolean quiet) |
| 128 |
throws IOException { |
| 129 |
long pos = file.getFilePointer(); |
| 130 |
long length = file.length(); |
| 131 |
int bufferSize = 4 * 1024; |
| 132 |
byte[] data = new byte[bufferSize * 2]; |
| 133 |
long last = System.nanoTime(); |
| 134 |
while (pos < length) { |
| 135 |
System.arraycopy(data, bufferSize, data, 0, bufferSize); |
| 136 |
if (pos + bufferSize > length) { |
| 137 |
file.readFully(data, bufferSize, (int) (length - pos)); |
| 138 |
return find(data, find, (int) (bufferSize + length - pos - find.length)); |
| 139 |
} |
| 140 |
if (!quiet) { |
| 141 |
long now = System.nanoTime(); |
| 142 |
if (now > last + TimeUnit.SECONDS.toNanos(5)) { |
| 143 |
System.out.println((100 * pos / length) + "%"); |
| 144 |
last = now; |
| 145 |
} |
| 146 |
} |
| 147 |
file.readFully(data, bufferSize, bufferSize); |
| 148 |
int f = find(data, find, bufferSize); |
| 149 |
if (f >= 0) { |
| 150 |
return f + pos - bufferSize; |
| 151 |
} |
| 152 |
pos += bufferSize; |
| 153 |
} |
| 154 |
return -1; |
| 155 |
} |
| 156 |
|
| 157 |
private static int find(byte[] data, byte[] find, int max) { |
| 158 |
outer: |
| 159 |
for (int i = 0; i < max; i++) { |
| 160 |
for (int j = 0; j < find.length; j++) { |
| 161 |
if (data[i + j] != find[j]) { |
| 162 |
continue outer; |
| 163 |
} |
| 164 |
} |
| 165 |
return i; |
| 166 |
} |
| 167 |
return -1; |
| 168 |
} |
| 169 |
|
| 170 |
private static void list(long pos, String header, ArrayList<String> list) { |
| 171 |
System.out.println("-----------------------------------------------"); |
| 172 |
System.out.println("[" + pos + "]: " + header); |
| 173 |
System.out.println("-----------------------------------------------"); |
| 174 |
for (String l : list) { |
| 175 |
System.out.println(l); |
| 176 |
} |
| 177 |
System.out.println("-----------------------------------------------"); |
| 178 |
} |
| 179 |
|
| 180 |
private static ArrayList<String> readLines(RandomAccessFile file, |
| 181 |
int maxLines) throws IOException { |
| 182 |
ArrayList<String> lines = new ArrayList<>(); |
| 183 |
ByteArrayOutputStream buff = new ByteArrayOutputStream(100); |
| 184 |
boolean lastNewline = false; |
| 185 |
while (maxLines > 0) { |
| 186 |
int x = file.read(); |
| 187 |
if (x < 0) { |
| 188 |
break; |
| 189 |
} |
| 190 |
if (x == '\r' || x == '\n') { |
| 191 |
if (!lastNewline) { |
| 192 |
maxLines--; |
| 193 |
lastNewline = true; |
| 194 |
byte[] data = buff.toByteArray(); |
| 195 |
String s = new String(data); |
| 196 |
lines.add(s); |
| 197 |
buff.reset(); |
| 198 |
} |
| 199 |
continue; |
| 200 |
} |
| 201 |
if (lastNewline) { |
| 202 |
lastNewline = false; |
| 203 |
} |
| 204 |
buff.write(x); |
| 205 |
} |
| 206 |
byte[] data = buff.toByteArray(); |
| 207 |
if (data.length > 0) { |
| 208 |
String s = new String(data); |
| 209 |
lines.add(s); |
| 210 |
} |
| 211 |
return lines; |
| 212 |
} |
| 213 |
|
| 214 |
} |