01: /*******************************************************************************
02: * Copyright (c) 2005 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.pde.internal.runtime.logview;
11:
12: import java.io.File;
13: import java.io.IOException;
14: import java.io.InputStream;
15: import java.io.RandomAccessFile;
16:
17: public class TailInputStream extends InputStream {
18:
19: private RandomAccessFile fRaf;
20:
21: private long fTail;
22:
23: public TailInputStream(File file, long maxLength)
24: 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: }
|