X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.message.ui%2Fsrc%2Forg%2Fsimantics%2Fmessage%2Fui%2FTailInputStream.java;fp=bundles%2Forg.simantics.message.ui%2Fsrc%2Forg%2Fsimantics%2Fmessage%2Fui%2FTailInputStream.java;h=14c7fedb4e143fb97297b9893d37b33be6505aa5;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.message.ui/src/org/simantics/message/ui/TailInputStream.java b/bundles/org.simantics.message.ui/src/org/simantics/message/ui/TailInputStream.java new file mode 100644 index 000000000..14c7fedb4 --- /dev/null +++ b/bundles/org.simantics.message.ui/src/org/simantics/message/ui/TailInputStream.java @@ -0,0 +1,65 @@ +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.message.ui; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.RandomAccessFile; + +public class TailInputStream extends InputStream { + + private RandomAccessFile fRaf; + + private long fTail; + + public TailInputStream(File file, long maxLength) throws IOException { + super(); + fTail = maxLength; + fRaf = new RandomAccessFile(file, "r"); //$NON-NLS-1$ + skipHead(file); + } + + private void skipHead(File file) throws IOException { + if (file.length() > fTail) { + fRaf.seek(file.length() - fTail); + // skip bytes until a new line to be sure we start from a beginnng of valid UTF-8 character + int c = read(); + while (c != '\n' && c != 'r' && c != -1) { + c = read(); + } + + } + } + + public int read() throws IOException { + byte[] b = new byte[1]; + int len = fRaf.read(b, 0, 1); + if (len < 0) { + return len; + } + return b[0]; + } + + public int read(byte[] b) throws IOException { + return fRaf.read(b, 0, b.length); + } + + public int read(byte[] b, int off, int len) throws IOException { + return fRaf.read(b, off, len); + } + + public void close() throws IOException { + fRaf.close(); + } + +}