KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > runtime > logview > TailInputStream


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 JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.io.RandomAccessFile JavaDoc;
17
18 public class TailInputStream extends InputStream JavaDoc {
19
20     private RandomAccessFile JavaDoc fRaf;
21
22     private long fTail;
23
24     public TailInputStream(File JavaDoc file, long maxLength) throws IOException JavaDoc {
25         super();
26         fTail = maxLength;
27         fRaf = new RandomAccessFile JavaDoc(file, "r"); //$NON-NLS-1$
28
skipHead(file);
29     }
30
31     private void skipHead(File JavaDoc file) throws IOException JavaDoc {
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 JavaDoc {
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 JavaDoc {
53         return fRaf.read(b, 0, b.length);
54     }
55
56     public int read(byte[] b, int off, int len) throws IOException JavaDoc {
57         return fRaf.read(b, off, len);
58     }
59
60     public void close() throws IOException JavaDoc {
61         fRaf.close();
62     }
63
64 }
65
Popular Tags