Example usage for java.io RandomAccessFile length

List of usage examples for java.io RandomAccessFile length

Introduction

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

Prototype

public native long length() throws IOException;

Source Link

Document

Returns the length of this file.

Usage

From source file:com.sun.faban.harness.webclient.ResultAction.java

private String editResultInfo(String runID) throws FileNotFoundException, IOException {
    RunId runId = new RunId(runID);
    String ts = null;/*from   w w w .jav a 2s.c  om*/
    String[] status = new String[2];
    File file = new File(Config.OUT_DIR + runID + '/' + Config.RESULT_INFO);
    RandomAccessFile rf = new RandomAccessFile(file, "rwd");
    long size = rf.length();
    byte[] buffer = new byte[(int) size];
    rf.readFully(buffer);
    String content = new String(buffer, 0, (int) size);
    int idx = content.indexOf('\t');
    if (idx != -1) {
        status[0] = content.substring(0, idx).trim();
        status[1] = content.substring(++idx).trim();
    } else {
        status[0] = content.trim();
        int lastIdxln = status[0].lastIndexOf("\n");
        if (lastIdxln != -1)
            status[0] = status[0].substring(0, lastIdxln - 1);
    }
    if (status[1] != null) {
        ts = status[1];
    } else {
        String paramFileName = runId.getResultDir().getAbsolutePath() + File.separator + "run.xml";
        File paramFile = new File(paramFileName);
        long dt = paramFile.lastModified();
        ts = dateFormat.format(new Date(dt));
        rf.seek(rf.length());
        rf.writeBytes('\t' + ts.trim());
    }
    rf.close();
    return ts;
}

From source file:com.roamtouch.menuserver.utils.FileUtils.java

private void removeLastCharacter(File fileName) {

    RandomAccessFile f;
    long length;/*from  w w w.j a  va 2 s.  c  o m*/

    try {

        f = new RandomAccessFile(fileName, "rw");
        length = f.length();
        long l;

        if (length > 0)
            l = length - 1;
        else
            l = length;

        f.setLength(l);
        f.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:com.frand.easyandroid.http.FFFileRespHandler.java

public int copy(InputStream input, RandomAccessFile out) throws IOException {
    interrupt = false;//  w  w w  .j  a  va2s.  c o  m
    if (input == null || out == null) {
        return -1;
    }
    byte[] buffer = new byte[BUFFER_SIZE];
    BufferedInputStream in = new BufferedInputStream(input, BUFFER_SIZE);
    int count = 0, n = 0;
    long errorBlockTimePreviousTime = -1, expireTime = 0;
    try {
        out.seek(out.length());
        previousTime = System.currentTimeMillis();
        while (!interrupt) {
            n = in.read(buffer, 0, BUFFER_SIZE);
            if (n == -1) {
                break;
            }
            out.write(buffer, 0, n);
            count += n;
            if (networkSpeed == 0) {
                if (errorBlockTimePreviousTime > 0) {
                    expireTime = System.currentTimeMillis() - errorBlockTimePreviousTime;
                    if (expireTime > TIME_OUT) {
                        throw new ConnectTimeoutException("connection time out.");
                    }
                } else {
                    errorBlockTimePreviousTime = System.currentTimeMillis();
                }
            } else {
                expireTime = 0;
                errorBlockTimePreviousTime = -1;
            }
        }
    } finally {
        try {
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return count;
}

From source file:org.chililog.server.workbench.StaticFileRequestHandler.java

/**
 * Process the message/*from ww w .j a va 2  s . c  om*/
 */
@Override
public void processMessage(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
    HttpRequest request = (HttpRequest) e.getMessage();

    // We don't handle 100 Continue because we only allow GET method.
    if (request.getMethod() != HttpMethod.GET) {
        sendError(ctx, e, METHOD_NOT_ALLOWED, null);
        return;
    }

    // Check
    final String filePath = convertUriToPhysicalFilePath(request.getUri());
    if (filePath == null) {
        sendError(ctx, e, FORBIDDEN, null);
        return;
    }
    File file = new File(filePath);
    if (file.isHidden() || !file.exists()) {
        sendError(ctx, e, NOT_FOUND, String.format("%s not exist", file.getCanonicalPath()));
        return;
    }
    if (!file.isFile()) {
        sendError(ctx, e, FORBIDDEN, String.format("%s not a file", file.getCanonicalPath()));
        return;
    }

    // Cache Validation
    String ifModifiedSince = request.getHeader(HttpHeaders.Names.IF_MODIFIED_SINCE);
    if (!StringUtils.isBlank(ifModifiedSince)) {
        SimpleDateFormat dateFormatter = new SimpleDateFormat(HTTP_DATE_FORMAT, Locale.US);
        Date ifModifiedSinceDate = dateFormatter.parse(ifModifiedSince);

        // Only compare up to the second because the datetime format we send to the client does not have milliseconds 
        long ifModifiedSinceDateSeconds = ifModifiedSinceDate.getTime() / 1000;
        long fileLastModifiedSeconds = file.lastModified() / 1000;
        if (ifModifiedSinceDateSeconds == fileLastModifiedSeconds) {
            sendNotModified(ctx, e);
            return;
        }
    }

    // Open file for sending back
    RandomAccessFile raf;
    try {
        raf = new RandomAccessFile(file, "r");
    } catch (FileNotFoundException fnfe) {
        sendError(ctx, e, NOT_FOUND, null);
        return;
    }
    long fileLength = raf.length();

    // Log
    writeLogEntry(e, OK, null);

    // Create the response
    HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
    setContentLength(response, fileLength);
    setContentTypeHeader(response, file);
    setDateAndCacheHeaders(response, file);

    // Write the content.
    Channel ch = e.getChannel();
    ChannelFuture writeFuture;
    if (AppProperties.getInstance().getWorkbenchSslEnabled()) {
        // Cannot use zero-copy with HTTPS

        // Write the initial line and the header.
        ch.write(response);

        // Write chunks
        writeFuture = ch.write(new ChunkedFile(raf, 0, fileLength, 8192));
    } else {
        // Now that we are using Execution Handlers, we cannot do zero-copy.
        // Do as per with compression (which is what most browser will ask for)
        byte[] buffer = new byte[(int) fileLength];
        raf.readFully(buffer);
        raf.close();

        response.setContent(ChannelBuffers.copiedBuffer(buffer));
        writeFuture = ch.write(response);

        /*
         * // No encryption - use zero-copy. // However zero-copy does not seem to work with compression // Only use
         * zero-copy for large files like movies and music // Write the initial line and the header.
         * ch.write(response); // Zero-copy final FileRegion region = new DefaultFileRegion(raf.getChannel(), 0,
         * fileLength); writeFuture = ch.write(region); writeFuture.addListener(new ChannelFutureProgressListener()
         * { public void operationComplete(ChannelFuture future) { region.releaseExternalResources(); } public void
         * operationProgressed(ChannelFuture future, long amount, long current, long total) {
         * _logger.debug("Zero-Coping file %s: %d / %d (+%d) bytes", filePath, current, total, amount); } });
         */
    }

    // Decide whether to close the connection or not.
    if (!isKeepAlive(request)) {
        // Close the connection when the whole content is written out.
        writeFuture.addListener(ChannelFutureListener.CLOSE);
    }
}

From source file:com.koda.integ.hbase.storage.LRUStorageRecycler.java

/**
 * Format of a block in a file:/*from ww  w.j a  va 2  s  . c o m*/
 * 0..3  - total record size (-4)
 * 4..7  - size of a key in bytes (16 if use hash128)
 * 8 .. x - key data
 * x+1 ..x+1- IN_MEMORY flag ( 1- in memory, 0 - not)
 * x+2 ... block, serialized and compressed
 *
 * @param file the file
 * @throws IOException Signals that an I/O exception has occurred.
 * @throws NativeMemoryException the native memory exception
 */
private void processFile(RandomAccessFile file) throws IOException, NativeMemoryException {

    FileChannel fc = file.getChannel();
    // make sure that file size < 2G
    LOG.info("File length=" + file.length());
    MappedByteBuffer buffer = fc.map(MapMode.READ_ONLY, 0, file.length());

    long fileLength = file.length();
    long saved = 0;
    long startTime = System.currentTimeMillis();

    while (buffer.position() < fileLength) {
        int oldOffset = buffer.position();
        //LOG.info(oldOffset);
        // check IO throttle
        ioThrottle(startTime, oldOffset);

        NumericHistogram histogram = refCache.getObjectHistogram();

        int blockSize = buffer.getInt();
        int keySize = buffer.getInt();

        //LOG.info("block size="+blockSize+" key size="+keySize);

        byte[] key = new byte[keySize];
        // STATISTICS
        totalScannedBytes.addAndGet(blockSize + 4);

        // read key
        // WE HAVE TO USE byte[] keys
        long data = refCache.getEvictionData(key);
        if (data < 0) {
            // not found in in_memory cache
            buffer.position(oldOffset + blockSize + 4);
            continue;
        }

        double quantValue = histogram.quantile(evictionThreshold);
        if (data > quantValue) {
            // save block
            saved = blockSize + 4;
            buffer.position(oldOffset);
            StorageHandle handle = storage.storeData(buffer);
            refCache.put(key, handle);

        } else {
            // STATISTICS
            totalPurgedBytes.addAndGet(blockSize + 4);
        }

        if (oldOffset + blockSize + 4 < fileLength) {
            // Advance pointer
            buffer.position(oldOffset + blockSize + 4);

        } else {
            break;
        }
        // Check panic. W/o adaptive processing support - killing file entirely 
        // is the only option to keep up with the load.
        if (storage.getCurrentStorageSize() >= panicLevelWatermark * storage.getMaxStorageSize()) {
            LOG.warn("[PANIC DELETE]. Storage size exceeded " + panicLevelWatermark + " mark.");
            // STATISTICS
            totalPanicEvents.incrementAndGet();
        }
    }

    // Unmap mapped ByteBuffer
    fc.close();
    FileUtils.unmapMmaped(buffer);
    ;
    LOG.info("Stats: total length=" + fileLength + "; purged data=" + (fileLength - saved)
            + " with eviction threshold=" + evictionThreshold + "; purged ratio=["
            + (((double) (fileLength - saved)) / fileLength) + "]");

}

From source file:org.kchine.r.server.RListener.java

public static void pager(String fileName, String header, String title, String deleteFile) {
    HashMap<String, Object> attributes = new HashMap<String, Object>();

    byte[] buffer = null;
    try {/*from w w  w  .j a  va2 s  . com*/
        RandomAccessFile raf = new RandomAccessFile(fileName, "r");
        buffer = new byte[(int) raf.length()];
        raf.readFully(buffer);
        raf.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    attributes.put("fileName", new File(fileName).getName());
    attributes.put("content", buffer);
    attributes.put("header", header);
    attributes.put("title", title);
    attributes.put("deleteFile", new Boolean(deleteFile));

    DirectJNI.getInstance().notifyRActionListeners(new RConsoleAction("PAGER", attributes));

}

From source file:ro.ieugen.fileserver.http.HttpStaticFileServerHandler.java

@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
    HttpRequest request = (HttpRequest) e.getMessage();

    final String path = sanitizeUri(request.getUri());
    LOG.info("Requested URI is {}, sanitised path is {}", request.getUri(), path);
    if (path == null) {
        sendError(ctx, FORBIDDEN);/*from   www  .java  2 s . c om*/
        return;
    }

    File file = new File(path);
    if (file.isHidden() || !file.exists()) {
        sendError(ctx, NOT_FOUND);
        return;
    }

    if (!(file.isFile() || file.isDirectory())) {
        sendError(ctx, FORBIDDEN);
        return;
    }

    Channel ch = e.getChannel();
    ChannelFuture writeFuture = null;

    if (file.isDirectory()) {
        ch.write(directoryListingResponse(file));
    } else {
        // Cache Validation
        if (sendRetrieveFromCacheIfNotModified(ctx, request, file))
            return;
        RandomAccessFile raf = openFileOrSendError(ctx, file);
        if (raf == null)
            return;
        // Write the initial line and the header.
        ch.write(fileDownloadResponse(file, raf));

        // Write the content.
        if (ch.getPipeline().get(SslHandler.class) != null) {
            // Cannot use zero-copy with HTTPS.
            writeFuture = ch.write(new ChunkedFile(raf, 0, raf.length(), 8192));
        } else {
            // No encryption - use zero-copy.
            final FileRegion region = new DefaultFileRegion(raf.getChannel(), 0, raf.length());
            writeFuture = ch.write(region);
            writeFuture.addListener(new ChannelFutureProgressListener() {
                public void operationComplete(ChannelFuture future) {
                    region.releaseExternalResources();
                }

                public void operationProgressed(ChannelFuture future, long amount, long current, long total) {
                    LOG.info("{}", String.format("%s: %d / %d (+%d)%n", path, current, total, amount));
                }
            });
        }
    }

    // Decide whether to close the connection or not.
    if (!isKeepAlive(request)) {
        // Close the connection when the whole content is written out.
        writeFuture.addListener(ChannelFutureListener.CLOSE);
    }
}

From source file:org.apache.hadoop.hive.ql.io.TestRCFile.java

@Test
public void testReadCorruptFile() throws IOException, SerDeException {
    cleanup();//w w w .  ja va  2s . c  om

    byte[][] record = { null, null, null, null, null, null, null, null };

    RCFileOutputFormat.setColumnNumber(conf, expectedFieldsData.length);
    RCFile.Writer writer = new RCFile.Writer(fs, conf, file, null, new DefaultCodec());
    BytesRefArrayWritable bytes = new BytesRefArrayWritable(record.length);
    final int recCount = 100;
    Random rand = new Random();
    for (int recIdx = 0; recIdx < recCount; recIdx++) {
        for (int i = 0; i < record.length; i++) {
            record[i] = new Integer(rand.nextInt()).toString().getBytes("UTF-8");
        }
        for (int i = 0; i < record.length; i++) {
            BytesRefWritable cu = new BytesRefWritable(record[i], 0, record[i].length);
            bytes.set(i, cu);
        }
        writer.append(bytes);
        bytes.clear();
    }
    writer.close();

    // Insert junk in middle of file. Assumes file is on local disk.
    RandomAccessFile raf = new RandomAccessFile(file.toUri().getPath(), "rw");
    long corruptOffset = raf.length() / 2;
    LOG.info("corrupting " + raf + " at offset " + corruptOffset);
    raf.seek(corruptOffset);
    raf.writeBytes("junkjunkjunkjunkjunkjunkjunkjunk");
    raf.close();

    // Set the option for tolerating corruptions. The read should succeed.
    Configuration tmpConf = new Configuration(conf);
    tmpConf.setBoolean("hive.io.rcfile.tolerate.corruptions", true);
    RCFile.Reader reader = new RCFile.Reader(fs, file, tmpConf);

    LongWritable rowID = new LongWritable();

    while (true) {
        boolean more = reader.next(rowID);
        if (!more) {
            break;
        }
        BytesRefArrayWritable cols = new BytesRefArrayWritable();
        reader.getCurrentRow(cols);
        cols.resetValid(8);
    }

    reader.close();
}

From source file:app.utils.ACache.java

public byte[] getAsBinary(String key) {
    RandomAccessFile RAFile = null;
    boolean removeFile = false;
    try {//w  w  w .  j av a 2s.co m
        File file = mCache.get(key);
        if (!file.exists())
            return null;
        RAFile = new RandomAccessFile(file, "r");
        byte[] byteArray = new byte[(int) RAFile.length()];
        RAFile.read(byteArray);
        if (!Utils.isDue(byteArray)) {
            return Utils.clearDateInfo(byteArray);
        } else {
            removeFile = true;
            return null;
        }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    } finally {
        if (RAFile != null) {
            try {
                RAFile.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (removeFile)
            remove(key);
    }
}

From source file:edu.sfsu.csc780.chathub.ui.ChannelActivity.java

private Uri saveAudio(String mFileName) {
    File returnAudioFile = null;//from   w  w w .  j a v  a2  s  . c  om

    try {
        returnAudioFile = createAudioFile();
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (returnAudioFile == null) {
        Log.d(TAG, "Error creating media file");
        return null;
    }

    try {
        RandomAccessFile f = new RandomAccessFile(mFileName, "r");
        byte[] b = new byte[(int) f.length()];
        f.readFully(b);
        FileOutputStream fos = new FileOutputStream(returnAudioFile);
        fos.write(b);
        fos.close();
    } catch (FileNotFoundException e) {
        Log.d(TAG, "File not found: " + e.getMessage());
    } catch (IOException e) {
        Log.d(TAG, "Error accessing file: " + e.getMessage());
    }
    return Uri.fromFile(returnAudioFile);
}