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.hly.component.download.DownloadTransaction.java

public void run() {
    // TODO ?Daemon
    InputStream is = null;//from w  w  w . ja v  a 2 s  .  com
    HttpURLConnection conn = null;
    RandomAccessFile randomFile = null;
    File tmpFile = null;
    try {
        // ?uri
        tmpFile = new File(mTempLocalUri);
        File parentFile = tmpFile.getParentFile();
        if (!parentFile.exists()) {
            parentFile.mkdirs();
        }
        if (!tmpFile.exists()) {
            tmpFile.createNewFile();
        }
        randomFile = new RandomAccessFile(mTempLocalUri, "rw");
        long fileLength = randomFile.length();
        completeSize = fileLength;

        if (isCancel) {
            return;
        }
        String connUrl = mUri;
        // ?uri????
        // getRedirectUrl(connUrl);
        if (!T.ckIsEmpty(mRedirectUri)) {
            connUrl = mRedirectUri;
        }
        conn = getHttpConnetion(connUrl);
        conn.setRequestProperty("range", "bytes=" + fileLength + "-");
        conn.connect();

        int contentLength = conn.getContentLength();
        totalSize = completeSize + contentLength;
        if (contentLength == -1 || contentLength > 0) {
            //    
            randomFile.seek(fileLength);
            byte[] buffer = new byte[8192];
            is = conn.getInputStream();
            int length = -1;
            while ((length = is.read(buffer)) != -1) {
                if (isCancel) {
                    return;
                }
                randomFile.write(buffer, 0, length);
                completeSize += length;
                notifyProgress(length);
            }
        }
        mTransactionState.setState(TransactionState.SUCCESS);
    } catch (Throwable t) {
        Log.w(TAG, Log.getStackTraceString(t));
    } finally {
        isRunning = false;
        isCancel = false;
        try {
            if (randomFile != null) {
                randomFile.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            conn.disconnect();
        }

        if (mTransactionState.getState() != TransactionState.SUCCESS) {
            mTransactionState.setState(TransactionState.FAILED);
            Log.e(TAG, "Delivery failed.");
        } else {
            if (tmpFile == null) {
                mTransactionState.setState(TransactionState.FAILED);
            } else {
                File localFile = new File(this.mLocalUri);
                boolean flag = tmpFile.renameTo(localFile);
                if (flag) {
                    Log.d(TAG, "rename pic succ" + this.mLocalUri);
                } else {
                    mTransactionState.setState(TransactionState.FAILED);
                    Log.d(TAG, "rename pic failed" + this.mLocalUri);
                }
            }
        }
        notifyObservers();
    }
}

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

public static void multiValuedReadBenchMarkV1(File file, int numDocs, int totalNumValues, int maxEntriesPerDoc,
        int columnSizeInBits) throws Exception {
    System.out.println("******************************************************************");
    System.out.println("Analyzing " + file.getName() + " numDocs:" + numDocs + ", totalNumValues:"
            + totalNumValues + ", maxEntriesPerDoc:" + maxEntriesPerDoc + ", numBits:" + columnSizeInBits);
    long start, end;
    boolean readFile = true;
    boolean randomRead = true;
    boolean contextualRead = true;
    boolean signed = false;
    boolean isMmap = false;
    PinotDataBuffer heapBuffer = PinotDataBuffer.fromFile(file, ReadMode.mmap, FileChannel.MapMode.READ_ONLY,
            "benchmarking");
    BaseSingleColumnMultiValueReader reader = new com.linkedin.pinot.core.io.reader.impl.v1.FixedBitMultiValueReader(
            heapBuffer, numDocs, totalNumValues, columnSizeInBits, signed);

    int[] intArray = new int[maxEntriesPerDoc];
    File outfile = new File("/tmp/" + file.getName() + ".raw");
    FileWriter fw = new FileWriter(outfile);
    for (int i = 0; i < numDocs; i++) {
        int length = reader.getIntArray(i, intArray);
        StringBuilder sb = new StringBuilder();
        String delim = "";
        for (int j = 0; j < length; j++) {
            sb.append(delim);//  w w  w .  j  av  a  2 s . c  om
            sb.append(intArray[j]);
            delim = ",";
        }
        fw.write(sb.toString());
        fw.write("\n");
    }
    fw.close();

    // sequential read
    if (readFile) {
        DescriptiveStatistics stats = new DescriptiveStatistics();
        RandomAccessFile raf = new RandomAccessFile(file, "rw");
        ByteBuffer buffer = ByteBuffer.allocateDirect((int) file.length());
        raf.getChannel().read(buffer);
        for (int run = 0; run < MAX_RUNS; run++) {
            long length = file.length();
            start = System.currentTimeMillis();
            for (int i = 0; i < length; i++) {
                byte b = buffer.get(i);
            }
            end = System.currentTimeMillis();
            stats.addValue((end - start));
        }
        System.out.println("v1 multi value read bytes stats for " + file.getName());
        System.out.println(
                stats.toString().replaceAll("\n", ", ") + " raw:" + Arrays.toString(stats.getValues()));

        raf.close();
    }
    if (randomRead) {
        DescriptiveStatistics stats = new DescriptiveStatistics();
        for (int run = 0; run < MAX_RUNS; run++) {
            start = System.currentTimeMillis();
            for (int i = 0; i < numDocs; i++) {
                int length = reader.getIntArray(i, intArray);
            }
            end = System.currentTimeMillis();
            stats.addValue((end - start));
        }
        System.out.println("v1 multi value sequential read one stats for " + file.getName());
        System.out.println(
                stats.toString().replaceAll("\n", ", ") + " raw:" + Arrays.toString(stats.getValues()));
    }

    if (contextualRead) {
        DescriptiveStatistics stats = new DescriptiveStatistics();
        for (int run = 0; run < MAX_RUNS; run++) {
            MultiValueReaderContext context = (MultiValueReaderContext) reader.createContext();
            start = System.currentTimeMillis();
            for (int i = 0; i < numDocs; i++) {
                int length = reader.getIntArray(i, intArray, context);
            }
            end = System.currentTimeMillis();
            // System.out.println("RUN:" + run + "Time:" + (end-start));
            stats.addValue((end - start));
        }
        System.out.println("v1 multi value sequential read one with context stats for " + file.getName());
        System.out.println(
                stats.toString().replaceAll("\n", ", ") + " raw:" + Arrays.toString(stats.getValues()));

    }
    reader.close();
    heapBuffer.close();
    System.out.println("******************************************************************");

}

From source file:com.example.android.vault.EncryptedDocument.java

/**
 * Encrypt and write both the metadata and content sections of this
 * document, reading the content from the given pipe. Internally uses
 * {@link ParcelFileDescriptor#checkError()} to verify that content arrives
 * without errors. Writes to temporary file to keep atomic view of contents,
 * swapping into place only when write is successful.
 * <p/>/*w  w w . j  a  va2s  . c o m*/
 * Pipe is left open, so caller is responsible for calling
 * {@link ParcelFileDescriptor#close()} or
 * {@link ParcelFileDescriptor#closeWithError(String)}.
 *
 * @param contentIn read end of a pipe.
 */
public void writeMetadataAndContent(JSONObject meta, ParcelFileDescriptor contentIn)
        throws IOException, GeneralSecurityException {
    // Write into temporary file to provide an atomic view of existing
    // contents during write, and also to recover from failed writes.
    final String tempName = mFile.getName() + ".tmp_" + Thread.currentThread().getId();
    final File tempFile = new File(mFile.getParentFile(), tempName);

    RandomAccessFile f = new RandomAccessFile(tempFile, "rw");
    try {
        // Truncate any existing data
        f.setLength(0);

        // Write content first to detect size
        if (contentIn != null) {
            f.seek(CONTENT_OFFSET);
            final int plainLength = writeSection(f, new FileInputStream(contentIn.getFileDescriptor()));
            meta.put(Document.COLUMN_SIZE, plainLength);

            // Verify that remote side of pipe finished okay; if they
            // crashed or indicated an error then this throws and we
            // leave the original file intact and clean up temp below.
            contentIn.checkError();
        }

        meta.put(Document.COLUMN_DOCUMENT_ID, mDocId);
        meta.put(Document.COLUMN_LAST_MODIFIED, System.currentTimeMillis());

        // Rewind and write metadata section
        f.seek(0);
        f.writeInt(MAGIC_NUMBER);

        final ByteArrayInputStream metaIn = new ByteArrayInputStream(
                meta.toString().getBytes(StandardCharsets.UTF_8));
        writeSection(f, metaIn);

        if (f.getFilePointer() > CONTENT_OFFSET) {
            throw new IOException("Metadata section was too large");
        }

        // Everything written fine, atomically swap new data into place.
        // fsync() before close would be overkill, since rename() is an
        // atomic barrier.
        f.close();
        tempFile.renameTo(mFile);

    } catch (JSONException e) {
        throw new IOException(e);
    } finally {
        // Regardless of what happens, always try cleaning up.
        f.close();
        tempFile.delete();
    }
}

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

/**
 * Follows changes in the file, calling the TailerListener's handle method for each new line.
 *//*from  ww  w . j  av  a 2 s  .co m*/
public void run() {
    RandomAccessFile reader = null;
    try {
        long last = 0; // The last time the file was checked for changes
        long position = 0; // position within the file
        // Open the file
        while (run && reader == null) {
            try {
                reader = new RandomAccessFile(file, "r");
            } catch (FileNotFoundException e) {
                listener.fileNotFound();
            }

            if (reader == null) {
                try {
                    Thread.sleep(delay);
                } catch (InterruptedException e) {
                }
            } else {
                // The current position in the file
                position = end ? file.length() : startPosition;
                last = System.currentTimeMillis();
                reader.seek(position);
                readLine(reader);
                position = reader.getFilePointer();
            }
        }

        while (run) {

            // Check the file length to see if it was rotated
            long length = file.length();

            if (length < position) {

                // File was rotated
                listener.fileRotated();

                // Reopen the reader after rotation
                try {
                    // Ensure that the old file is closed iff we re-open it successfully
                    RandomAccessFile save = reader;
                    reader = new RandomAccessFile(file, "r");
                    position = 0;
                    // close old file explicitly rather than relying on GC picking up previous RAF
                    IOUtils.closeQuietly(save);
                } catch (FileNotFoundException e) {
                    // in this case we continue to use the previous reader and position values
                    listener.fileNotFound();
                }
                continue;
            } else {

                // File was not rotated

                // See if the file needs to be read again
                if (length > position) {

                    // The file has more content than it did last time
                    last = System.currentTimeMillis();
                    position = readLines(reader);

                } else if (FileUtils.isFileNewer(file, last)) {

                    /* This can happen if the file is truncated or overwritten
                     * with the exact same length of information. In cases like
                     * this, the file position needs to be reset
                     */
                    position = 0;
                    reader.seek(position); // cannot be null here

                    // Now we can read new lines
                    last = System.currentTimeMillis();
                    position = readLines(reader);
                }
            }
            try {
                Thread.sleep(delay);
            } catch (InterruptedException e) {
            }
        }

    } catch (Exception e) {

        listener.handle(e);

    } finally {
        try {
            reader.close();
        } catch (Exception e) {
            Logger.error(this.getClass(), "Unable to close: " + e.getMessage());
        }

    }
}

From source file:com.polyvi.xface.extension.advancedfiletransfer.FileDownloader.java

@Override
public void transfer(CallbackContext callbackCtx) {
    initDownloadInfo();//w  w  w  . ja  va  2  s.  co  m
    if (mState == DOWNLOADING) {
        return;
    }
    mCallbackCtx = callbackCtx;
    if (null == mDownloadInfo) {
        onError(CONNECTION_ERR);
    } else {
        setState(DOWNLOADING);
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpURLConnection connection = null;
                RandomAccessFile randomAccessFile = null;
                InputStream is = null;
                int retry = RETRY;
                // TODO:?????
                do {
                    int completeSize = mDownloadInfo.getCompleteSize();
                    try {
                        URL url = new URL(mUrl);
                        connection = (HttpURLConnection) url.openConnection();
                        connection.setConnectTimeout(TIME_OUT_MILLISECOND);
                        connection.setRequestMethod("GET");
                        // ?Rangebytes x-;
                        connection.setRequestProperty("Range", "bytes=" + completeSize + "-");
                        // cookie
                        setCookieProperty(connection, mUrl);
                        // ?.temp
                        randomAccessFile = new RandomAccessFile(mLocalFilePath + TEMP_FILE_SUFFIX, "rwd");

                        randomAccessFile.seek(completeSize);
                        // ???
                        is = connection.getInputStream();
                        byte[] buffer = new byte[mBufferSize];
                        int length = -1;
                        while ((length = is.read(buffer)) != -1) {
                            try {
                                randomAccessFile.write(buffer, 0, length);
                            } catch (Exception e) {
                                retry = -1;
                                break;
                            }
                            completeSize += length;
                            onProgressUpdated(completeSize, mDownloadInfo.getTotalSize());
                            mDownloadInfo.setCompleteSize(completeSize);
                            if (PAUSE == mState) {
                                break;
                            }
                        }
                        if (mDownloadInfo.isDownloadCompleted()) {
                            // ??.temp
                            renameFile(mLocalFilePath + TEMP_FILE_SUFFIX, mLocalFilePath);
                            onSuccess();
                            break;
                        }
                    } catch (FileNotFoundException e) {
                        onError(FILE_NOT_FOUND_ERR);
                        XLog.e(CLASS_NAME, e.getMessage());
                    } catch (IOException e) {
                        if (retry <= 0) {
                            onError(CONNECTION_ERR);
                            XLog.e(CLASS_NAME, e.getMessage());
                        }
                        // ,?1?
                        try {
                            Thread.sleep(RETRY_INTERVAL);
                        } catch (InterruptedException ex) {
                            XLog.e(CLASS_NAME, "sleep be interrupted", ex);
                        }
                    } finally {
                        try {
                            if (null != is) {
                                is.close();
                            }
                            if (null != randomAccessFile) {
                                // new URL??randomAccessFilenull
                                randomAccessFile.close();
                            }
                            if (null != connection) {
                                // new URL??connectionnull
                                connection.disconnect();
                            }
                        } catch (IOException e) {
                            XLog.e(CLASS_NAME, e.getMessage());
                        }
                    }
                } while ((DOWNLOADING == mState) && (0 < retry--));
            }
        }).start();
    }
}

From source file:jm.web.Archivo.java

public String getArchivo(String path, String tabla, String clave, String campoNombre, String campoBytea) {
    this._archivoNombre = "";
    try {/*from   w w w  . j  a  v  a 2  s  . c  o  m*/
        ResultSet res = this.consulta(
                "select * from " + tabla + " where " + tabla.replace("tbl_", "id_") + "=" + clave + ";");
        if (res.next()) {
            this._archivoNombre = res.getString(campoNombre) != null ? res.getString(campoNombre) : "";
            if (this._archivoNombre.compareTo("") != 0) {
                try {
                    this._archivo = new File(path, this._archivoNombre);
                    if (!this._archivo.exists()) {
                        byte[] bytes = (res.getString(campoBytea) != null) ? res.getBytes(campoBytea) : null;
                        RandomAccessFile archivo = new RandomAccessFile(path + this._archivoNombre, "rw");
                        archivo.write(bytes);
                        archivo.close();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            res.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return this._archivoNombre;
}

From source file:com.edgenius.wiki.service.impl.SitemapServiceImpl.java

private void appendSitemapIndex(String sitemap) throws IOException {
    File sitemapIndexFile = new File(mapResourcesRoot.getFile(), SITEMAP_INDEX_NAME);
    if (!sitemapIndexFile.exists()) {
        //if a new sitemap file
        List<String> lines = new ArrayList<String>();
        lines.add("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
        lines.add("<sitemapindex xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">");
        lines.add("</sitemapindex>");
        FileUtils.writeLines(sitemapIndexFile, lines);
    }// ww  w.j  a  va  2  s  .c  o m

    RandomAccessFile rfile = new RandomAccessFile(sitemapIndexFile, "rw");
    FileChannel channel = rfile.getChannel();

    //this new content will append to end of file before XML end tag
    StringBuilder lines = new StringBuilder();
    lines.append("   <sitemap>\n");
    lines.append("     <loc>" + WebUtil.getHostAppURL() + SITEMAP_URL_CONTEXT + sitemap + "</loc>\n");
    lines.append("     <lastmod>" + TIME_FORMAT.format(new Date()) + " </lastmod>\n");
    lines.append("   </sitemap>\n");
    //the last tag will be overwrite, so append it again to new content. 
    lines.append(SITEMAP_INDEX_TAIL_FLAG);
    byte[] content = lines.toString().getBytes();

    ByteBuffer byteBuf = ByteBuffer.allocate(512);
    // seek first
    int len = 0, headIdx = 0;
    long tailIdx = channel.size() - 512;
    tailIdx = tailIdx < 0 ? 0 : tailIdx;

    long headPos = -1;
    StringBuilder header = new StringBuilder();
    while ((len = channel.read(byteBuf, tailIdx)) > 0) {
        byteBuf.rewind();
        byte[] dst = new byte[len];
        byteBuf.get(dst, 0, len);
        header.append(new String(dst, "UTF8"));
        headIdx = header.indexOf(SITEMAP_INDEX_TAIL_FLAG);
        if (headIdx != -1) {
            headPos = channel.size() - header.substring(headIdx).getBytes().length;
            break;
        }
    }
    FileLock lock = channel.tryLock(headPos, content.length, false);
    try {
        channel.write(ByteBuffer.wrap(content), headPos);
    } finally {
        lock.release();
    }

    channel.force(false);
    rfile.close();

}

From source file:com.googlecode.psiprobe.Utils.java

public static void sendFile(HttpServletRequest request, HttpServletResponse response, File file)
        throws IOException {
    OutputStream out = response.getOutputStream();
    RandomAccessFile raf = new RandomAccessFile(file, "r");
    try {/*ww w.  j  av  a2s .  co m*/
        long fileSize = raf.length();
        long rangeStart = 0;
        long rangeFinish = fileSize - 1;

        // accept attempts to resume download (if any)
        String range = request.getHeader("Range");
        if (range != null && range.startsWith("bytes=")) {
            String pureRange = range.replaceAll("bytes=", "");
            int rangeSep = pureRange.indexOf("-");

            try {
                rangeStart = Long.parseLong(pureRange.substring(0, rangeSep));
                if (rangeStart > fileSize || rangeStart < 0) {
                    rangeStart = 0;
                }
            } catch (NumberFormatException e) {
                // ignore the exception, keep rangeStart unchanged
            }

            if (rangeSep < pureRange.length() - 1) {
                try {
                    rangeFinish = Long.parseLong(pureRange.substring(rangeSep + 1));
                    if (rangeFinish < 0 || rangeFinish >= fileSize) {
                        rangeFinish = fileSize - 1;
                    }
                } catch (NumberFormatException e) {
                    // ignore the exception
                }
            }
        }

        // set some headers
        response.setContentType("application/x-download");
        response.setHeader("Content-Disposition", "attachment; filename=" + file.getName());
        response.setHeader("Accept-Ranges", "bytes");
        response.setHeader("Content-Length", Long.toString(rangeFinish - rangeStart + 1));
        response.setHeader("Content-Range", "bytes " + rangeStart + "-" + rangeFinish + "/" + fileSize);

        // seek to the requested offset
        raf.seek(rangeStart);

        // send the file
        byte[] buffer = new byte[4096];

        long len;
        int totalRead = 0;
        boolean nomore = false;
        while (true) {
            len = raf.read(buffer);
            if (len > 0 && totalRead + len > rangeFinish - rangeStart + 1) {
                // read more then required?
                // adjust the length
                len = rangeFinish - rangeStart + 1 - totalRead;
                nomore = true;
            }

            if (len > 0) {
                out.write(buffer, 0, (int) len);
                totalRead += len;
                if (nomore) {
                    break;
                }
            } else {
                break;
            }
        }
    } finally {
        raf.close();
    }
}

From source file:it.drwolf.ridire.session.async.JobDBDataUpdater.java

private String getJobStatus(String encodedJobName) throws HttpException, IOException, DocumentException {
    // back compatibility Heritrix 2
    if (encodedJobName.startsWith("completed-")) {
        return CrawlStatus.FINISHED.toString();
    }//from   w ww. j a  v  a2s.c  o  m
    File jobDir = new File(this.jobsDir + CrawlerManager.FILE_SEPARATOR + encodedJobName);
    String[] files = jobDir.list();
    if (files == null || files.length < 2) {
        return CrawlStatus.CREATED.toString();
    }
    String ret = CrawlStatus.CREATED.toString();
    RandomAccessFile progressStatistics = null;
    Calendar now = new GregorianCalendar();
    Date comparingDate = DateUtils.addDays(now.getTime(), -3);
    try {
        progressStatistics = new RandomAccessFile(
                this.jobsDir + CrawlerManager.FILE_SEPARATOR + encodedJobName + CrawlerManager.FILE_SEPARATOR
                        + "logs" + CrawlerManager.FILE_SEPARATOR + "progress-statistics.log",
                "r");
        if (progressStatistics != null) {
            progressStatistics.seek(Math.max(0, progressStatistics.length() - 3000));
            String line = progressStatistics.readLine();
            StringBuffer buffer = new StringBuffer();
            while (line != null) {
                buffer.append(line + "\n");
                line = progressStatistics.readLine();
            }
            String progressStatisticsContent = buffer.toString();
            Matcher m = this.progressStatisticsDatePattern.matcher(progressStatisticsContent);
            int start = 0;
            String lastDateString = "";
            while (m.find(start)) {
                start = m.end();
                lastDateString = m.group();
            }
            Date lastDate = this.progressStatisticsDateFormat.parse(lastDateString);
            if (!progressStatisticsContent.contains("CRAWL ENDED - Finished")
                    && lastDate.after(comparingDate)) {
                ret = CrawlStatus.RUNNING.toString();
            } else {
                ret = CrawlStatus.FINISHED.toString();
            }

        }
    } catch (FileNotFoundException e) {
        // TODO: handle exception
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        if (progressStatistics != null) {
            progressStatistics.close();
        }
    }
    // File crawlReport = new File(this.jobsDir + FILE_SEPARATOR
    // + encodedJobName + FILE_SEPARATOR + "reports" + FILE_SEPARATOR
    // + "crawl-report.txt");
    // if (crawlReport != null && crawlReport.canRead()) {
    // String crawlReportContent = FileUtils.readFileToString(crawlReport);
    // if (crawlReportContent.contains("crawl status: Finished")) {
    // ret = CrawlStatus.FINISHED.toString();
    // }
    // }
    return ret;
}