1
|
/*******************************************************************************
|
2
|
* Copyright (c) 2005 IBM Corporation and others.
|
3
|
* All rights reserved. This program and the accompanying materials
|
4
|
* are made available under the terms of the Eclipse Public License v1.0
|
5
|
* which accompanies this distribution, and is available at
|
6
|
* http://www.eclipse.org/legal/epl-v10.html
|
7
|
*
|
8
|
* Contributors:
|
9
|
* IBM Corporation - initial API and implementation
|
10
|
*******************************************************************************/
|
11
|
package org.eclipse.pde.internal.runtime.logview;
|
12
|
|
13
|
import java.io.File;
|
14
|
import java.io.IOException;
|
15
|
import java.io.InputStream;
|
16
|
import java.io.RandomAccessFile;
|
17
|
|
18
|
public class TailInputStream extends InputStream {
|
19
|
|
20
|
private RandomAccessFile fRaf;
|
21
|
|
22
|
private long fTail;
|
23
|
|
24
|
public TailInputStream(File file, long maxLength) throws IOException {
|
25
|
super();
|
26
|
fTail = maxLength;
|
27
|
fRaf = new RandomAccessFile(file, "r"); //$NON-NLS-1$
|
28
|
skipHead(file);
|
29
|
}
|
30
|
|
31
|
private void skipHead(File file) throws IOException {
|
32
|
if (file.length() > fTail) {
|
33
|
fRaf.seek(file.length() - fTail);
|
34
|
// skip bytes until a new line to be sure we start from a beginnng of valid UTF-8 character
|
35
|
int c= read();
|
36
|
while(c!='\n' && c!='r' && c!=-1){
|
37
|
c=read();
|
38
|
}
|
39
|
|
40
|
}
|
41
|
}
|
42
|
|
43
|
public int read() throws IOException {
|
44
|
byte[] b = new byte[1];
|
45
|
int len = fRaf.read(b, 0, 1);
|
46
|
if (len < 0) {
|
47
|
return len;
|
48
|
}
|
49
|
return b[0];
|
50
|
}
|
51
|
|
52
|
public int read(byte[] b) throws IOException {
|
53
|
return fRaf.read(b, 0, b.length);
|
54
|
}
|
55
|
|
56
|
public int read(byte[] b, int off, int len) throws IOException {
|
57
|
return fRaf.read(b, off, len);
|
58
|
}
|
59
|
|
60
|
public void close() throws IOException {
|
61
|
fRaf.close();
|
62
|
}
|
63
|
|
64
|
}
|