Search in sources :

Example 76 with RandomAccessFile

use of java.io.RandomAccessFile in project tdi-studio-se by Talend.

the class SortedMultipleHashFile method next.

/**
     * DOC amaumont Comment method "next".
     * 
     * @param hashcode
     * @throws IOException
     */
public Object next(int hashcode) throws IOException {
    Object objectToReturn = null;
    int fileNumber = getFileNumber(hashcode);
    Object next = nextObjectsArray[fileNumber];
    objectToReturn = next;
    RandomAccessFile ra = raArray[fileNumber];
    DataInputStream dis = disArray[fileNumber];
    long disPosition = disPositionsArray[fileNumber];
    long raPosition = ra.getFilePointer();
    if (raPosition < disPosition && next == null) {
    } else {
        if (raPosition > disPosition) {
            dis.skip(raPosition - disPosition);
        }
        try {
            // readInt = ra.readInt();
            // byteArray = new byte[readInt];
            // ra.read(byteArray);
            int readInt = dis.readInt();
            byte[] byteArray = new byte[readInt];
            dis.read(byteArray);
            disPositionsArray[fileNumber] += 4 + byteArray.length;
            nextObjectsArray[fileNumber] = iLightSerializable.createInstance(byteArray);
        } catch (IOException e) {
            // EOF
            nextObjectsArray[fileNumber] = null;
        }
    }
    lastRetrievedObjectArray[fileNumber] = objectToReturn;
    return objectToReturn;
}
Also used : RandomAccessFile(java.io.RandomAccessFile) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream)

Example 77 with RandomAccessFile

use of java.io.RandomAccessFile in project tdi-studio-se by Talend.

the class SortedMultipleHashFile method get.

public Object get(String container, long cursorPosition, int hashcode) throws IOException, ClassNotFoundException {
    if (raArray == null) {
        //$NON-NLS-1$
        throw new IllegalStateException("Call initGet(..) one time before all call on get(..) method");
    }
    // System.out.println("GET cursorPosition="+cursorPosition + " hashcode="+hashcode);
    int fileNumber = getFileNumber(hashcode);
    // System.out.println(fileNumber);
    RandomAccessFile ra = raArray[fileNumber];
    if (cursorPosition != lastRetrievedCursorPositionArray[fileNumber]) {
        ra.seek(cursorPosition);
        int readInt = ra.readInt();
        byte[] byteArray = new byte[readInt];
        ra.read(byteArray);
        lastRetrievedObjectArray[fileNumber] = iLightSerializable.createInstance(byteArray);
        lastRetrievedCursorPositionArray[fileNumber] = cursorPosition;
        DataInputStream dis = disArray[fileNumber];
        long disPosition = disPositionsArray[fileNumber];
        long raPosition = ra.getFilePointer();
        if (raPosition < disPosition) {
        } else {
            if (raPosition > disPosition) {
                dis.skip(raPosition - disPosition);
            }
            try {
                // readInt = ra.readInt();
                // byteArray = new byte[readInt];
                // ra.read(byteArray);
                readInt = dis.readInt();
                byteArray = new byte[readInt];
                dis.read(byteArray);
                disPositionsArray[fileNumber] += 4 + byteArray.length;
                nextObjectsArray[fileNumber] = iLightSerializable.createInstance(byteArray);
            } catch (IOException e) {
                // EOF
                nextObjectsArray[fileNumber] = null;
            }
        }
    }
    // System.out.println("Found:" + lastRetrievedObjectArray[fileNumber]);
    return lastRetrievedObjectArray[fileNumber];
}
Also used : RandomAccessFile(java.io.RandomAccessFile) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream)

Example 78 with RandomAccessFile

use of java.io.RandomAccessFile in project voltdb by VoltDB.

the class Files method map.

/**
   * Maps a file in to memory as per
   * {@link FileChannel#map(java.nio.channels.FileChannel.MapMode, long, long)} using the requested
   * {@link MapMode}.
   *
   * <p>Files are mapped from offset 0 to {@code size}.
   *
   * <p>If the mode is {@link MapMode#READ_WRITE} and the file does not exist, it will be created
   * with the requested {@code size}. Thus this method is useful for creating memory mapped files
   * which do not yet exist.
   *
   * <p>This only works for files <= {@link Integer#MAX_VALUE} bytes.
   *
   * @param file the file to map
   * @param mode the mode to use when mapping {@code file}
   * @return a buffer reflecting {@code file}
   * @throws IOException if an I/O error occurs
   *
   * @see FileChannel#map(MapMode, long, long)
   * @since 2.0
   */
public static MappedByteBuffer map(File file, MapMode mode, long size) throws FileNotFoundException, IOException {
    checkNotNull(file);
    checkNotNull(mode);
    Closer closer = Closer.create();
    try {
        RandomAccessFile raf = closer.register(new RandomAccessFile(file, mode == MapMode.READ_ONLY ? "r" : "rw"));
        return map(raf, mode, size);
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile)

Example 79 with RandomAccessFile

use of java.io.RandomAccessFile in project voltdb by VoltDB.

the class FileTxnLog method truncate.

/**
     * truncate the current transaction logs
     * @param zxid the zxid to truncate the logs to
     * @return true if successful false if not
     */
public boolean truncate(long zxid) throws IOException {
    FileTxnIterator itr = new FileTxnIterator(this.logDir, zxid);
    PositionInputStream input = itr.inputStream;
    long pos = input.getPosition();
    // now, truncate at the current position
    RandomAccessFile raf = new RandomAccessFile(itr.logFile, "rw");
    raf.setLength(pos);
    raf.close();
    while (itr.goToNextLog()) {
        if (!itr.logFile.delete()) {
            LOG.warn("Unable to truncate " + itr.logFile);
        }
    }
    return true;
}
Also used : RandomAccessFile(java.io.RandomAccessFile)

Example 80 with RandomAccessFile

use of java.io.RandomAccessFile in project voltdb by VoltDB.

the class Util method isValidSnapshot.

/**
     * Verifies that the file is a valid snapshot. Snapshot may be invalid if
     * it's incomplete as in a situation when the server dies while in the process
     * of storing a snapshot. Any file that is not a snapshot is also
     * an invalid snapshot.
     *
     * @param f file to verify
     * @return true if the snapshot is valid
     * @throws IOException
     */
public static boolean isValidSnapshot(File f) throws IOException {
    if (f == null || Util.getZxidFromName(f.getName(), "snapshot") == -1)
        return false;
    // Check for a valid snapshot
    RandomAccessFile raf = new RandomAccessFile(f, "r");
    // the snapshot should be atleast 10 bytes
    if (raf.length() < 10) {
        return false;
    }
    try {
        raf.seek(raf.length() - 5);
        byte[] bytes = new byte[5];
        int readlen = 0;
        int l;
        while (readlen < 5 && (l = raf.read(bytes, readlen, bytes.length - readlen)) >= 0) {
            readlen += l;
        }
        if (readlen != bytes.length) {
            LOG.info("Invalid snapshot " + f + " too short, len = " + readlen);
            return false;
        }
        ByteBuffer bb = ByteBuffer.wrap(bytes);
        int len = bb.getInt();
        byte b = bb.get();
        if (len != 1 || b != '/') {
            LOG.info("Invalid snapshot " + f + " len = " + len + " byte = " + (b & 0xff));
            return false;
        }
    } finally {
        raf.close();
    }
    return true;
}
Also used : RandomAccessFile(java.io.RandomAccessFile) ByteBuffer(java.nio.ByteBuffer)

Aggregations

RandomAccessFile (java.io.RandomAccessFile)1607 IOException (java.io.IOException)761 File (java.io.File)691 FileChannel (java.nio.channels.FileChannel)284 ByteBuffer (java.nio.ByteBuffer)174 FileNotFoundException (java.io.FileNotFoundException)167 Test (org.junit.Test)152 FileOutputStream (java.io.FileOutputStream)91 FileLock (java.nio.channels.FileLock)91 MappedByteBuffer (java.nio.MappedByteBuffer)83 InputStream (java.io.InputStream)68 FileInputStream (java.io.FileInputStream)63 EOFException (java.io.EOFException)55 ArrayList (java.util.ArrayList)45 ByteArrayOutputStream (java.io.ByteArrayOutputStream)39 BufferedInputStream (java.io.BufferedInputStream)35 ByteArrayInputStream (java.io.ByteArrayInputStream)35 Random (java.util.Random)33 HashMap (java.util.HashMap)24 BinaryMapIndexReader (net.osmand.binary.BinaryMapIndexReader)23