Search in sources :

Example 1 with RandomAccessFile

use of java.io.RandomAccessFile in project vert.x by eclipse.

the class FileSystemImpl method truncateInternal.

private BlockingAction<Void> truncateInternal(String p, long len, Handler<AsyncResult<Void>> handler) {
    Objects.requireNonNull(p);
    return new BlockingAction<Void>(handler) {

        public Void perform() {
            RandomAccessFile raf = null;
            try {
                String path = vertx.resolveFile(p).getAbsolutePath();
                if (len < 0) {
                    throw new FileSystemException("Cannot truncate file to size < 0");
                }
                if (!Files.exists(Paths.get(path))) {
                    throw new FileSystemException("Cannot truncate file " + path + ". Does not exist");
                }
                try {
                    raf = new RandomAccessFile(path, "rw");
                    raf.setLength(len);
                } finally {
                    if (raf != null)
                        raf.close();
                }
            } catch (IOException e) {
                throw new FileSystemException(e);
            }
            return null;
        }
    };
}
Also used : FileSystemException(io.vertx.core.file.FileSystemException) RandomAccessFile(java.io.RandomAccessFile) IOException(java.io.IOException)

Example 2 with RandomAccessFile

use of java.io.RandomAccessFile in project druid by druid-io.

the class FileUtilsTest method testMap.

@Test
public void testMap() throws IOException {
    File dataFile = folder.newFile("data");
    long buffersMemoryBefore = BufferUtils.totalMemoryUsedByDirectAndMappedBuffers();
    try (RandomAccessFile raf = new RandomAccessFile(dataFile, "rw")) {
        raf.write(42);
        // 1 MB
        raf.setLength(1 << 20);
    }
    try (MappedByteBufferHandler mappedByteBufferHandler = FileUtils.map(dataFile)) {
        Assert.assertEquals(42, mappedByteBufferHandler.get().get(0));
    }
    long buffersMemoryAfter = BufferUtils.totalMemoryUsedByDirectAndMappedBuffers();
    Assert.assertEquals(buffersMemoryBefore, buffersMemoryAfter);
}
Also used : RandomAccessFile(java.io.RandomAccessFile) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) Test(org.junit.Test)

Example 3 with RandomAccessFile

use of java.io.RandomAccessFile in project druid by druid-io.

the class SmooshedFileMapperTest method testDeterministicFileUnmapping.

@Test
public void testDeterministicFileUnmapping() throws IOException {
    File baseDir = folder.newFolder("base");
    long totalMemoryUsedBeforeAddingFile = BufferUtils.totalMemoryUsedByDirectAndMappedBuffers();
    try (FileSmoosher smoosher = new FileSmoosher(baseDir)) {
        File dataFile = folder.newFile("data.bin");
        try (RandomAccessFile raf = new RandomAccessFile(dataFile, "rw")) {
            // 1 MB
            raf.setLength(1 << 20);
        }
        smoosher.add(dataFile);
    }
    long totalMemoryUsedAfterAddingFile = BufferUtils.totalMemoryUsedByDirectAndMappedBuffers();
    // Assert no hanging file mappings left by either smoosher or smoosher.add(file)
    Assert.assertEquals(totalMemoryUsedBeforeAddingFile, totalMemoryUsedAfterAddingFile);
}
Also used : RandomAccessFile(java.io.RandomAccessFile) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) Test(org.junit.Test)

Example 4 with RandomAccessFile

use of java.io.RandomAccessFile in project druid by druid-io.

the class GenericIndexedWriter method bagSizePower.

/**
   * Tries to get best value split(number of elements in each value file) which can be expressed as power of 2.
   *
   * @return Returns the size of value file splits as power of 2.
   *
   * @throws IOException
   */
private int bagSizePower() throws IOException {
    long avgObjectSize = (valuesOut.getCount() + numWritten - 1) / numWritten;
    File f = ioPeon.getFile(makeFilename("headerLong"));
    Preconditions.checkNotNull(f, "header file missing.");
    try (RandomAccessFile headerFile = new RandomAccessFile(f, "r")) {
        for (int i = 31; i >= 0; --i) {
            if ((1L << i) * avgObjectSize <= fileSizeLimit) {
                if (actuallyFits(i, headerFile)) {
                    return i;
                }
            }
        }
    }
    throw new ISE("no value split found with fileSizeLimit [%d], avgObjectSize [%d] while serializing [%s]", fileSizeLimit, avgObjectSize, filenameBase);
}
Also used : RandomAccessFile(java.io.RandomAccessFile) ISE(io.druid.java.util.common.ISE) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 5 with RandomAccessFile

use of java.io.RandomAccessFile in project druid by druid-io.

the class GenericIndexedWriter method writeToChannelVersionTwo.

private void writeToChannelVersionTwo(WritableByteChannel channel, FileSmoosher smoosher) throws IOException {
    if (smoosher == null) {
        throw new IAE("version 2 GenericIndexedWriter requires FileSmoosher.");
    }
    int bagSizePower = bagSizePower();
    OutputStream metaOut = Channels.newOutputStream(channel);
    metaOut.write(GenericIndexed.VERSION_TWO);
    metaOut.write(objectsSorted ? 0x1 : 0x0);
    metaOut.write(Ints.toByteArray(bagSizePower));
    metaOut.write(Ints.toByteArray(Ints.checkedCast(numWritten)));
    metaOut.write(Ints.toByteArray(fileNameByteArray.length));
    metaOut.write(fileNameByteArray);
    try (RandomAccessFile headerFile = new RandomAccessFile(ioPeon.getFile(makeFilename("headerLong")), "r")) {
        Preconditions.checkNotNull(headerFile, "header file missing.");
        long previousValuePosition = 0;
        int bagSize = 1 << bagSizePower;
        int numberOfFilesRequired = GenericIndexed.getNumberOfFilesRequired(bagSize, numWritten);
        byte[] buffer = new byte[1 << 16];
        try (InputStream is = new FileInputStream(ioPeon.getFile(makeFilename("values")))) {
            int counter = -1;
            for (int i = 0; i < numberOfFilesRequired; i++) {
                if (i != numberOfFilesRequired - 1) {
                    // 8 for long bytes.
                    headerFile.seek((bagSize + counter) * Longs.BYTES);
                    counter = counter + bagSize;
                } else {
                    // for remaining items.
                    headerFile.seek((numWritten - 1) * Longs.BYTES);
                }
                long valuePosition = Long.reverseBytes(headerFile.readLong());
                long numBytesToPutInFile = valuePosition - previousValuePosition;
                try (SmooshedWriter smooshChannel = smoosher.addWithSmooshedWriter(generateValueFileName(filenameBase, i), numBytesToPutInFile)) {
                    writeBytesIntoSmooshedChannel(numBytesToPutInFile, buffer, smooshChannel, is);
                    previousValuePosition = valuePosition;
                }
            }
        }
        writeHeaderLong(smoosher, headerFile, bagSizePower, buffer);
    }
}
Also used : SmooshedWriter(io.druid.java.util.common.io.smoosh.SmooshedWriter) RandomAccessFile(java.io.RandomAccessFile) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) CountingOutputStream(com.google.common.io.CountingOutputStream) OutputStream(java.io.OutputStream) IAE(io.druid.java.util.common.IAE) FileInputStream(java.io.FileInputStream)

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