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