Example usage for java.io RandomAccessFile getFilePointer

List of usage examples for java.io RandomAccessFile getFilePointer

Introduction

In this page you can find the example usage for java.io RandomAccessFile getFilePointer.

Prototype

public native long getFilePointer() throws IOException;

Source Link

Document

Returns the current offset in this file.

Usage

From source file:dbseer.comp.process.live.LogTailer.java

/**
 * Read new lines.// w w w. j  a  va  2 s .c  o  m
 *
 * @param reader The file to read
 * @return The new position after the lines have been read
 * @throws java.io.IOException if an I/O error occurs.
 */
private long readLines(RandomAccessFile reader) throws IOException {
    StringBuilder sb = new StringBuilder();

    long pos = reader.getFilePointer();
    long rePos = pos; // position to re-read

    int num;
    boolean seenCR = false;
    while (run && ((num = reader.read(inbuf)) != -1)) {
        for (int i = 0; i < num; i++) {
            byte ch = inbuf[i];
            switch (ch) {
            case '\n':
                seenCR = false; // swallow CR before LF
                listener.handle(sb.toString(), pos + i + 1);
                sb.setLength(0);
                rePos = pos + i + 1;
                break;
            case '\r':
                if (seenCR) {
                    sb.append('\r');
                }
                seenCR = true;
                break;
            default:
                if (seenCR) {
                    seenCR = false; // swallow final CR
                    listener.handle(sb.toString(), pos + i + 1);
                    sb.setLength(0);
                    rePos = pos + i + 1;
                }
                sb.append((char) ch); // add character, not its ascii value
            }
        }

        pos = reader.getFilePointer();
    }

    reader.seek(rePos); // Ensure we can re-read if necessary
    return rePos;
}

From source file:org.fastcatsearch.ir.document.DocumentWriter.java

public Document readDocument(int docNo) throws IOException, IRException {
    long prevPosPos = positionOutput.position();
    long docPos = -1;
    try {// w  w w.j  a  v  a 2  s . c  om
        long positionOffset = ((long) docNo) * IOUtil.SIZE_OF_LONG;
        positionOutput.seek(positionOffset);
        docPos = IOUtil.readLong(positionOutput.getRaf());
    } finally {
        positionOutput.seek(prevPosPos);
    }

    // find a document block
    long prevDocPos = docOutput.position();
    try {
        docOutput.seek(docPos);
        RandomAccessFile raf = docOutput.getRaf();
        int len = IOUtil.readInt(raf);
        long n = raf.getFilePointer();
        InputStream docInput = Channels.newInputStream(docOutput.getRaf().getChannel().position(n));
        //2014-11-26 ?  working ?   ? ? GC ?? OOM ? ?.
        // Stream  .
        InflaterInputStream decompressInputStream = null;
        inflaterOutput.reset();
        int count = -1;
        try {
            BoundedInputStream boundedInputStream = new BoundedInputStream(docInput, len);
            boundedInputStream.setPropagateClose(false);// docInput  .
            decompressInputStream = new InflaterInputStream(boundedInputStream, new Inflater(), 512);
            while ((count = decompressInputStream.read(workingBuffer)) != -1) {
                inflaterOutput.write(workingBuffer, 0, count);
            }
        } finally {
            decompressInputStream.close();
        }
    } finally {
        docOutput.seek(prevDocPos);
    }

    BytesRef bytesRef = inflaterOutput.getBytesRef();
    DataInput bai = new BytesDataInput(bytesRef.bytes, 0, bytesRef.length);

    Document document = new Document(fields.size());
    for (int i = 0; i < fields.size(); i++) {
        FieldSetting fs = fields.get(i);
        Field f = null;
        boolean hasValue = bai.readBoolean();
        if (hasValue) {
            f = fs.createEmptyField();
            f.readRawFrom(bai);
        } else {
            f = fs.createEmptyField();
        }
        if (f != null) {
            String multiValueDelimiter = fs.getMultiValueDelimiter();
            try {
                f.parseIndexable(multiValueDelimiter);
            } catch (FieldDataParseException e) {
                throw new IOException(e);
            }
        }
        document.add(f);
    }
    document.setDocId(docNo);
    return document;
}

From source file:com.baidu.terminator.manager.service.LogServiceImpl.java

@Override
public Log readLog(int linkId, long offset) throws IOException {
    String logFileLocation = LinkLogger.getLogFileLocation(linkId);
    FileUtils.createFile(logFileLocation);

    RandomAccessFile raf = null;
    List<String> lines = new ArrayList<String>();
    long length = 0;

    try {/*from   w  w w .j  ava 2 s . c  o m*/
        raf = new RandomAccessFile(logFileLocation, "r");
        raf.seek(offset);
        length = raf.length();

        long point = raf.getFilePointer();
        while (point < length) {
            String line = raf.readLine();
            String utf8Line = new String(line.getBytes("8859_1"), "utf-8");
            lines.add(utf8Line);

            if (point - offset >= MAX_READ_BYTES) {
                length = point;
                break;
            }
            point = raf.getFilePointer();
        }
    } finally {
        if (raf != null) {
            raf.close();
        }
    }

    Log log = new Log();
    log.setLogLocation(logFileLocation);
    log.setOffset(length);
    log.setContent(lines);
    return log;
}

From source file:com.btoddb.fastpersitentqueue.JournalFileTest.java

@Test
public void testInvalidUUID() throws Exception {
    // can't actuall write an invalid UUID because it's made of two variable length longs
    // only way to get an invalid UUID is to have a truncated file
    JournalFile jf1 = new JournalFile(theFile);
    jf1.initForWriting(new UUID());
    jf1.close();/*ww  w.j  ava2 s.  c  om*/

    // mess up the UUID
    RandomAccessFile raFile = new RandomAccessFile(theFile, "rw");
    Utils.writeInt(raFile, 1);
    Utils.writeInt(raFile, -1);
    raFile.setLength(raFile.getFilePointer());
    raFile.close();
    //        Utils.writeLong(raFile, numberOfEntries.get());

    JournalFile jf2 = new JournalFile(theFile);
    try {
        jf2.initForReading();
        fail("should have found invalid UUID in the form of truncated file");
    } catch (EOFException e) {
        // good!
    }
}

From source file:org.xdi.util.FileUtil.java

/**
 * Writes data in a file on specified position
 * //  www . j  a  v a 2s.  com
 * @param filePath
 * @param position
 * @param data
 * @return
 */
public boolean writeToFile(String filePath, long position, String data) {

    try {
        File f = new File(filePath);
        RandomAccessFile raf;
        raf = new RandomAccessFile(f, "rw");
        raf.seek(position);
        StringBuilder dataAfterPostion = new StringBuilder(data);
        while (raf.getFilePointer() < raf.length()) {
            String line = raf.readLine();
            dataAfterPostion.append(line);
        }
        raf.seek(position);
        raf.writeUTF(dataAfterPostion.toString());
        raf.close();
        return true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return false;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
}

From source file:com.dotmarketing.servlets.taillog.Tailer.java

/**
 * Read new lines./*from  ww  w  .j  av a 2s .c o  m*/
 *
 * @param reader The file to read
 * @return The new position after the lines have been read
 * @throws java.io.IOException if an I/O error occurs.
 */
private long readLines(RandomAccessFile reader) throws IOException {
    long pos = reader.getFilePointer();
    String line = readLine(reader);
    while (line != null) {
        pos = reader.getFilePointer();
        listener.handle(line);
        line = readLine(reader);
    }
    reader.seek(pos); // Ensure we can re-read if necessary
    return pos;
}

From source file:dk.netarkivet.common.utils.cdx.BinSearch.java

/** Perform a binary search for a string in a file.
 * Returns the position of a line that begins with 'find'.
 * Note that this may not be the first line, if there be duplicates.
 * @param in the RandomAccessFile//  www  . ja v  a 2s  .c  om
 * @param find The String to look for in the above file
 * @throws IOException If some I/O error occurs
 * @return The index of a line matching find, or -1 if none found.
 */
private static long binSearch(RandomAccessFile in, String find) throws IOException {
    // The starting position for the binary search.  Always
    // at the start of a line that's < the wanted line.
    long startpos = 0;
    // Ensure that startpos isn't a match.
    in.seek(startpos);
    String line = in.readLine();
    if (line == null) {
        return -1;
    }
    if (compare(line, find) == 0) {
        return startpos;
    }
    // The ending position for the binary search.  Always
    // *after* a line that >= the wanted line (which also means
    // at the start of a line that's > the wanted line, or at EOF
    long endpos = in.length();

    // Set file pos to first line after middle.
    findMiddleLine(in, startpos, endpos);

    // When done searching, midpos points to a matching line, if any
    // Until the search is done, both endpos and startpos point
    // at non-matching lines (or EOF), and startpos < prevpos < endpos
    long prevpos = in.getFilePointer();
    do {
        line = in.readLine();
        if (line == null) {
            log.debug("Internal: Ran past end of file in '" + in + "' at " + endpos);
            return -1;
        }
        int cmp = compare(line, find);
        if (cmp > 0) {
            endpos = prevpos;
        } else if (cmp < 0) {
            startpos = prevpos;
        } else {
            return prevpos;
        }
        if (startpos == endpos) {
            return -1;
        }
        prevpos = findMiddleLine(in, startpos, endpos);
        if (prevpos == -1) {
            return -1;
        }
    } while (true);
}

From source file:dk.netarkivet.common.utils.cdx.BinSearch.java

/**
 * Return the index of the first line in the file to match 'find'. If the
 * lines in the file are roughly equal length, it reads
 * O(sqrt(n)) lines, where n is the distance from matchingline to the first
 * line.//w  w w.  j a  v a  2s. c  om
 *
 * @param in
 *            The file to search in
 * @param find
 *            The string to match against the first line
 * @param matchingline
 *            The index to start searching from.  This index must be at
 *            the start of a line that matches 'find'
 * @return The offset into the file of the first line matching 'find'.
 *         Guaranteed to be <= matchingline.
 * @throws IOException If the matchingLine < 0 or some I/O error occurs.
 */
private static long findFirstLine(RandomAccessFile in, String find, long matchingline) throws IOException {
    in.seek(matchingline);
    String line = in.readLine();
    if (line == null || compare(line, find) != 0) {
        final String msg = "Internal: Called findFirstLine without a " + "matching line in '" + in + "' byte "
                + matchingline;
        log.warn(msg);
        throw new ArgumentNotValid(msg);
    }
    // Skip backwards in quadratically increasing steps.
    int linelength = line.length();
    long offset = linelength;
    for (int i = 1; matchingline - offset > 0; i++, offset = i * i * linelength) {
        skipToLine(in, matchingline - offset);
        line = in.readLine();
        if (line == null || compare(line, find) != 0) {
            break;
        }
    }
    // Either found start-of-file or a non-matching line
    long pos;
    if (matchingline - offset <= 0) {
        pos = 0;
        in.seek(0);
    } else {
        pos = in.getFilePointer();
    }
    // Seek forwards line by line until the first matching line.
    // This takes no more than sqrt(n) steps since we know there is
    // a matching line that far away by the way we skipped to here.
    while ((line = in.readLine()) != null) {
        if (compare(line, find) == 0) {
            return pos;
        }
        pos = in.getFilePointer();
    }

    return -1;
}

From source file:org.mitre.mpf.mvc.util.tailer.MpfLogTailer.java

/**
 * Read new lines./* www.j av a2  s  . c om*/
 *
 * @param reader The file to read
 * @param maxLines The maximum number of lines to read
 * @return The number of lines read
 * @throws IOException if an I/O error occurs.
 */
private int readLines(RandomAccessFile reader, int maxLines) throws IOException {
    int numLines = 0;
    StringBuilder sb = new StringBuilder();

    long pos = reader.getFilePointer();
    long rePos = pos; // position to re-read

    byte ch;
    boolean seenCR = false;
    while (((ch = (byte) (reader.read())) != -1) && (numLines < maxLines)) {
        switch (ch) {
        case '\n':
            seenCR = false; // swallow CR before LF
            if (listener.handle(sb.toString())) {
                numLines++;
            }
            sb.setLength(0);
            rePos = pos + 1;
            break;
        case '\r':
            if (seenCR) {
                sb.append('\r');
            }
            seenCR = true;
            break;
        default:
            if (seenCR) {
                seenCR = false; // swallow final CR
                if (listener.handle(sb.toString())) {
                    numLines++;
                }
                sb.setLength(0);
                rePos = pos + 1;
            }
            sb.append((char) ch); // add character, not its ascii value
        }

        pos = reader.getFilePointer();
    }

    reader.seek(rePos); // Ensure we can re-read if necessary
    position = rePos;

    return numLines;
}

From source file:com.datasayer.meerkat.MeerJobRunner.java

@SuppressWarnings("unchecked")
@Override/*w w w  .  ja v a2s.  c o  m*/
public void bsp(final BSPPeer<Writable, Writable, Writable, Writable, Writable> peer)
        throws IOException, SyncException, InterruptedException {

    while (true) {
        try {
            long currentTime = System.currentTimeMillis();
            FileSystem fs = FileSystem.get(conf);
            if (!fs.isFile(logPath)) {
                System.out.println("can not read input file");
                return;
            }
            RandomAccessFile file = new RandomAccessFile(logPath.toString(), "r");
            long fileLength = file.length();

            if (fileLength > filePointer) {
                file.seek(filePointer);
                String line = null;
                while (file.length() > file.getFilePointer()) {
                    line = file.readLine();
                    line = new String(line.getBytes("8859_1"), "utf-8");
                    guardMeer.observe(line);
                }
                filePointer = file.getFilePointer();
            } else {
                // nothing to do
            }
            file.close();

            long timeDiff = currentTime - this.lastAggregatedTime;
            if (timeDiff >= this.aggregationInterval) {

                peer.sync();

                if (peer.getPeerName().equals(masterName)) {
                    bossMeer.masterCompute(new Iterator<Writable>() {

                        private final int producedMessages = peer.getNumCurrentMessages();
                        private int consumedMessages = 0;

                        @Override
                        public boolean hasNext() {
                            return producedMessages > consumedMessages;
                        }

                        @Override
                        public Writable next() throws NoSuchElementException {
                            if (consumedMessages >= producedMessages) {
                                throw new NoSuchElementException();
                            }

                            try {
                                consumedMessages++;
                                return peer.getCurrentMessage();
                            } catch (IOException e) {
                                throw new NoSuchElementException();
                            }
                        }

                        @Override
                        public void remove() {
                            // BSPPeer.getCurrentMessage originally deletes a message.
                            // Thus, it doesn't need to throw exception.
                            // throw new UnsupportedOperationException();
                        }

                    }, signalMeer);
                    this.lastAggregatedTime = currentTime;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}