Example usage for java.io RandomAccessFile RandomAccessFile

List of usage examples for java.io RandomAccessFile RandomAccessFile

Introduction

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

Prototype

public RandomAccessFile(File file, String mode) throws FileNotFoundException 

Source Link

Document

Creates a random access file stream to read from, and optionally to write to, the file specified by the File argument.

Usage

From source file:com.linkedin.pinot.perf.ForwardIndexReaderBenchmark.java

public static void singleValuedReadBenchMarkV2(File file, int numDocs, int numBits) throws Exception {
    boolean signed = false;
    boolean isMmap = false;
    long start, end;
    boolean fullScan = true;

    boolean batchRead = true;
    boolean singleRead = true;

    PinotDataBuffer heapBuffer = PinotDataBuffer.fromFile(file, ReadMode.heap, FileChannel.MapMode.READ_ONLY,
            "benchmarking");
    com.linkedin.pinot.core.io.reader.impl.v2.FixedBitSingleValueReader reader = new com.linkedin.pinot.core.io.reader.impl.v2.FixedBitSingleValueReader(
            heapBuffer, numDocs, numBits, signed);

    if (fullScan) {
        DescriptiveStatistics stats = new DescriptiveStatistics();
        ByteBuffer buffer = ByteBuffer.allocateDirect((int) file.length());
        RandomAccessFile raf = new RandomAccessFile(file, "r");
        raf.getChannel().read(buffer);//from w  w w .ja v a2 s. c  om
        raf.close();
        int[] input = new int[numBits];
        int[] output = new int[32];
        int numBatches = (numDocs + 31) / 32;
        for (int run = 0; run < MAX_RUNS; run++) {
            start = System.currentTimeMillis();
            for (int i = 0; i < numBatches; i++) {
                for (int j = 0; j < numBits; j++) {
                    input[j] = buffer.getInt(i * numBits * 4 + j * 4);
                }
                BitPacking.fastunpack(input, 0, output, 0, numBits);
            }
            end = System.currentTimeMillis();
            stats.addValue((end - start));
        }
        System.out.println(" v2 full scan stats for " + file.getName());
        System.out.println(
                stats.toString().replaceAll("\n", ", ") + " raw:" + Arrays.toString(stats.getValues()));
    }
    if (singleRead) {
        DescriptiveStatistics stats = new DescriptiveStatistics();
        // sequential read
        for (int run = 0; run < MAX_RUNS; run++) {
            start = System.currentTimeMillis();
            for (int i = 0; i < numDocs; i++) {
                int value = reader.getInt(i);
            }
            end = System.currentTimeMillis();
            stats.addValue((end - start));
        }
        System.out.println(" v2 sequential single read for " + file.getName());
        System.out.println(
                stats.toString().replaceAll("\n", ", ") + " raw:" + Arrays.toString(stats.getValues()));
    }
    if (batchRead) {
        DescriptiveStatistics stats = new DescriptiveStatistics();
        int batchSize = Math.min(5000, numDocs);
        int[] output = new int[batchSize];
        int[] rowIds = new int[batchSize];

        // sequential read
        for (int run = 0; run < MAX_RUNS; run++) {
            start = System.currentTimeMillis();
            int rowId = 0;
            while (rowId < numDocs) {
                int length = Math.min(batchSize, numDocs - rowId);
                for (int i = 0; i < length; i++) {
                    rowIds[i] = rowId + i;
                }
                reader.getIntBatch(rowIds, output, length);
                rowId = rowId + length;
            }
            end = System.currentTimeMillis();
            stats.addValue((end - start));
        }
        System.out.println("v2 sequential batch read stats for " + file.getName());
        System.out.println(
                stats.toString().replaceAll("\n", ", ") + " raw:" + Arrays.toString(stats.getValues()));
    }
    reader.close();

}

From source file:au.org.ala.delta.io.BinFile.java

public void open(BinFileMode mode) {
    // System.out.println("opening "+_filename);
    _fileMode = mode;/*ww w  .j  av a 2s  .  c  o m*/
    File f = new File(_filename);
    _filename = f.getAbsolutePath();
    try {
        String ra_mode = "r";
        switch (mode) {
        case FM_APPEND:
        case FM_NEW:
        case FM_EXISTING:
        case FM_TEMPORARY:
            ra_mode = "rw";
            break;
        }
        _file = new RandomAccessFile(f, ra_mode);
        _channel = _file.getChannel();

        // Attempt to lock the file to prevent concurrent access.
        if (ra_mode.contains("w")) {
            FileLock lock = _channel.tryLock();
            if (lock == null) {
                IOUtils.closeQuietly(_file);
                throw new RuntimeException(
                        "Unable to open file for writing.  Possibly another program has this file open.");
            }
        }

    } catch (IOException ioex) {
        IOUtils.closeQuietly(_file);
        throw new RuntimeException(ioex.getMessage(), ioex);
    }
}

From source file:com.polyvi.xface.util.XFileUtils.java

/**
 * ?/*from w ww.  j  av  a 2 s.c om*/
 *
 * @param filePath
 *            ?
 * @param size
 *            size?
 * @return 
 */
public static long truncateFile(String fileName, long size) throws FileNotFoundException, IOException {
    RandomAccessFile raf = new RandomAccessFile(fileName, "rw");

    if (raf.length() >= size) {
        FileChannel channel = raf.getChannel();
        channel.truncate(size);
        return size;
    }

    return raf.length();
}

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

public MemorySegment loadHeaderOnly(String fn) throws IOException {
    File theFile = new File(directory, fn);
    RandomAccessFile raFile = new RandomAccessFile(theFile, "r");
    try {//from  ww  w  . j a va2s  . c  o  m
        MemorySegment segment = new MemorySegment();
        segment.readHeaderFromDisk(raFile);
        return segment;
    } finally {
        raFile.close();
    }
}

From source file:IntergrationTest.OCSPIntegrationTest.java

@Before
public void setUp() throws Exception {
    OCSP_URL = new URI("http://localhost:" + serverPort + "/verify-mocked-good");
    OCSP_MODE_URL = new URI("http://localhost:" + serverPort + "/set-response-mode");
    RandomAccessFile raf = new RandomAccessFile("certs/client/client.cer.pem", "r");
    byte[] buf = new byte[(int) raf.length()];
    raf.readFully(buf);//from   w  ww  .  j  ava2 s. com
    raf.close();
    clientCert = readPemCert(buf);
    MatcherAssert.assertThat(clientCert, CoreMatchers.notNullValue());
    issuerCert = getX509Certificate(httpGetBin(getIssuerCertURL(clientCert), true));
    MatcherAssert.assertThat(issuerCert, CoreMatchers.notNullValue());
}

From source file:info.ajaxplorer.client.http.AjxpFileBody.java

public void writeTo(OutputStream out) {
    InputStream in;/*w  w w  . ja v  a  2s.  c  om*/
    try {
        if (this.chunkSize > 0) {
            //System.out.println("Uploading file part " + this.chunkIndex);
            RandomAccessFile raf = new RandomAccessFile(getFile(), "r");
            int start = chunkIndex * this.chunkSize;
            int count = 0;
            int limit = chunkSize;
            if (chunkIndex == (totalChunks - 1)) {
                limit = lastChunkSize;
            }
            raf.seek(start);
            while (count < limit) {
                int byt = raf.read();
                out.write(byt);
                count++;
            }
            raf.close();
            //System.out.println("Sent " + count);            
        } else {
            in = new FileInputStream(getFile());
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            in.close();
        }
        this.chunkIndex++;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:net.sf.jvifm.Main.java

public static boolean createFileLock() {
    File lockFile = new File(FilenameUtils.concat(HomeLocator.getConfigHome(), ".lock"));
    try {//from   w  w  w. j  a  v a  2s.  c om
        FileChannel channel = new RandomAccessFile(lockFile, "rw").getChannel();
        fileLock = channel.tryLock();
        if (fileLock != null) {
            return true;
        }
    } catch (Exception e) {
    }
    return false;
}

From source file:com.polarion.pso.license.FetchUserLicenseTypeServlet.java

@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp)
        throws ServletException, IOException {
    ISecurityService securityService = (ISecurityService) PlatformContext.getPlatform()
            .lookupService(ISecurityService.class);
    ITrackerService trackerService = (ITrackerService) PlatformContext.getPlatform()
            .lookupService(ITrackerService.class);

    String userId = securityService.getCurrentUser();
    String userName = trackerService.getTrackerUser(userId).getName();

    long cutoff = System.currentTimeMillis() - (1000);
    File directory = new File("./logs/main");

    FileFilter wcFilter = new WildcardFileFilter("log4j-licensing-*.log");
    AgeFileFilter ageFilter = new AgeFileFilter(cutoff);
    FileFilter andFilter = new org.apache.commons.io.filefilter.AndFileFilter((IOFileFilter) ageFilter,
            (IOFileFilter) wcFilter);/*from  w  w  w . ja  v a2  s.c o m*/

    Collection<File> matches = FileUtils.listFiles(directory, (IOFileFilter) andFilter, null);

    if (!matches.isEmpty()) {

        File myFile = matches.iterator().next();
        RandomAccessFile licFile = new RandomAccessFile(myFile, "r");

        // Read the last 1024 bytes
        Long fileLength = licFile.length();
        Long offSet = fileLength - 1024;
        byte[] bStr = new byte[1024];
        licFile.seek(offSet);
        licFile.read(bStr);
        licFile.close();

        String logString = new java.lang.String(bStr);

        String[] lineArray = logString.split("\n");

        String searchString = "INFO  PolarionLicensing  - User \'" + userId + "\' logged in";
        Boolean found = false;
        Integer size = lineArray.length - 1;
        String licType = directory.toString();

        for (int i = size; i >= 0; i--) {
            String line = lineArray[i];
            if (line.contains(searchString) && found == false) {
                found = true;
                i = -1;
                Integer startIndex = line.indexOf(searchString) + searchString.length() + 6;
                licType = line.substring(startIndex);
                licType = licType.replace("\r", "");
                licType = licType.trim();
            }
        }

        req.setAttribute("userId", userName);
        req.setAttribute("licType", licType);
    } else {
        req.setAttribute("userId", userName);
        req.setAttribute("licType", "Not Found");
    }

    getServletContext().getRequestDispatcher("/currentUserLicenseType.jsp").forward(req, resp);
}

From source file:com.linkedin.pinot.core.startree.MmapLinkedListStarTreeTable.java

/** Rolls to next buffer if necessary */
private void checkBuffer() {
    if (backingBuffer == null || backingBuffer.position() == backingBuffer.limit()) {
        try {//from   w  w  w  . ja  v a 2  s  . c  o  m
            if (!backingBufferDir.exists()) {
                FileUtils.forceMkdir(backingBufferDir);
            }
            File backingBufferFile = new File(backingBufferDir, String.valueOf(fileCount));
            FileChannel backingFileChannel = new RandomAccessFile(backingBufferFile, "rw").getChannel();
            backingBuffer = backingFileChannel.map(FileChannel.MapMode.READ_WRITE, 0,
                    documentIncrement * rowSize);
            fileCount++;
            documentCount = 0;
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
    }
}

From source file:com.github.neoio.nio.util.NIOUtils.java

public static FileChannel openFileChannel(File file) throws NetFileNotFoundException {
    try {/*from   w w w .ja  va  2s .com*/
        return new RandomAccessFile(file, "rw").getChannel();
    } catch (FileNotFoundException e) {
        throw new NetFileNotFoundException(file + " not found", e);
    }
}