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: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();
    }//w w w.  j  av  a  2s.  c  om
    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;
}

From source file:com.limegroup.gnutella.metadata.MP3DataEditor.java

public int commitMetaData(String filename) {
    if (LOG.isDebugEnabled())
        LOG.debug("committing mp3 file");
    if (!LimeXMLUtils.isMP3File(filename))
        return LimeXMLReplyCollection.INCORRECT_FILETYPE;
    File f = null;//from ww  w .ja v  a 2s  .c  o m
    RandomAccessFile file = null;
    try {
        try {
            f = new File(filename);
            FileUtils.setWriteable(f);
            file = new RandomAccessFile(f, "rw");
        } catch (IOException e) {
            return LimeXMLReplyCollection.FILE_DEFECTIVE;
        }
        long length = 0;
        try {
            length = file.length();
            if (length < 128) //could not write - file too small
                return LimeXMLReplyCollection.FILE_DEFECTIVE;
            file.seek(length - 128);
        } catch (IOException ee) {
            return LimeXMLReplyCollection.RW_ERROR;
        }
        //1. Try to write out the ID3v2 data first
        int ret = -1;
        try {
            ret = writeID3V2DataToDisk(f);
        } catch (IOException iox) {
            return LimeXMLReplyCollection.RW_ERROR;
        } catch (ID3v2Exception e) { //catches both ID3v2 related exceptions
            ret = writeID3V1DataToDisk(file);
        }
        return ret;
    } finally {
        if (file != null) {
            try {
                file.close();
            } catch (IOException ignored) {
            }
        }
    }
}

From source file:org.apache.james.mailrepository.file.MBoxMailRepository.java

/**
 * Load the message keys and file pointer offsets from disk
 *//*from   w ww. ja v a  2s  .c o m*/
private synchronized void loadKeys() {
    if (mList != null) {
        return;
    }
    RandomAccessFile ins = null;
    try {
        ins = new RandomAccessFile(mboxFile, "r");
        long initialCapacity = (ins.length() > MLISTPRESIZEFACTOR ? ins.length() / MLISTPRESIZEFACTOR : 0);
        if (initialCapacity < DEFAULTMLISTCAPACITY) {
            initialCapacity = DEFAULTMLISTCAPACITY;
        }
        if (initialCapacity > Integer.MAX_VALUE) {
            initialCapacity = Integer.MAX_VALUE - 1;
        }
        this.mList = new Hashtable<String, Long>((int) initialCapacity);
        this.parseMboxFile(ins, new MessageAction() {
            public boolean isComplete() {
                return false;
            }

            public MimeMessage messageAction(String messageSeparator, String bodyText, long messageStart) {
                try {
                    String key = generateKeyValue(bodyText);
                    mList.put(key, messageStart);
                    if ((getLogger().isDebugEnabled())) {
                        getLogger().debug(this.getClass().getName() + " Key " + key + " at " + messageStart);
                    }

                } catch (NoSuchAlgorithmException e) {
                    getLogger().error("MD5 not supported! ", e);
                }
                return null;
            }
        });
        // System.out.println("Done Load keys!");
    } catch (FileNotFoundException e) {
        getLogger().error("Unable to save(open) file (File not found) " + mboxFile, e);
        this.mList = new Hashtable<String, Long>((int) DEFAULTMLISTCAPACITY);
    } catch (IOException e) {
        getLogger().error("Unable to write file (General I/O problem) " + mboxFile, e);
    } finally {
        if (ins != null)
            try {
                ins.close();
            } catch (IOException e) {
                getLogger().error("Unable to close file (General I/O problem) " + mboxFile, e);
            }
    }
}

From source file:com.android.volley.toolbox.DownloadNetwork.java

@Override
public NetworkResponse performRequest(Request<?> request) throws VolleyError {
    long requestStart = SystemClock.elapsedRealtime();
    while (true) {
        HttpResponse httpResponse = null;
        byte[] responseContents = null;
        Map<String, String> responseHeaders = Collections.emptyMap();
        RandomAccessFile acessfile = null;
        File file = null;/*w  ww . jav  a 2 s .c o  m*/
        try {
            if (!(request instanceof DownOrUpRequest)) {
                throw new IllegalArgumentException("request object mast be DownOrUpRequest???");
            }
            DownOrUpRequest requestDown = (DownOrUpRequest) request;
            // Gather headers.
            Map<String, String> headers = new HashMap<String, String>();
            // Download have no cache
            file = getFile(requestDown);
            acessfile = new RandomAccessFile(file, "rws");

            long length = acessfile.length();
            acessfile.seek(length);
            if (length != 0) {
                headers.put("Range", "bytes=" + length + "-");//
            }
            httpResponse = mHttpStack.performRequest(requestDown, headers);
            StatusLine statusLine = httpResponse.getStatusLine();
            int statusCode = statusLine.getStatusCode();

            responseHeaders = convertHeaders(httpResponse.getAllHeaders());
            // if the request is slow, log it.
            long requestLifetime = SystemClock.elapsedRealtime() - requestStart;
            logSlowRequests(requestLifetime, requestDown, responseContents, statusLine);

            if (statusCode < 200 || statusCode > 299) {
                acessfile.close();
                throw new IOException();
            }
            // Some responses such as 204s do not have content.  We must check.
            if (httpResponse.getEntity() != null) {
                responseContents = entityToBytes(httpResponse.getEntity(), requestDown, acessfile);
            } else {
                // Add 0 byte response as a way of honestly representing a
                // no-content request.
                responseContents = new byte[0];
            }
            acessfile.close();
            String re = null;
            if (!requestDown.isCanceled() || requestDown.getmMaxLength() == requestDown.getmCurLength()) {
                String path = file.getAbsolutePath();
                String re_name = ((DownOrUpRequest) request).getmDownloadName();
                if (re_name != null) {
                    re = path.substring(0, path.lastIndexOf('/')) + "/" + re_name;
                } else {
                    re = path.substring(0, path.lastIndexOf("."));
                }
                File rename = new File(re);
                boolean result = file.renameTo(rename);
                if (!result) {
                    Log.e(this.getClass().getName(),
                            "?????:"
                                    + rename);
                    throw new IOException(
                            "????????");
                }
                requestDown.setDownloadFile(rename);
            } else {
                re = file.getAbsolutePath();
                statusCode = 209;
            }
            return new NetworkResponse(statusCode, re.getBytes(), responseHeaders, false,
                    SystemClock.elapsedRealtime() - requestStart);
        } catch (SocketTimeoutException e) {
            attemptRetryOnException("socket", request, new TimeoutError());
        } catch (ConnectTimeoutException e) {
            attemptRetryOnException("connection", request, new TimeoutError());
        } catch (MalformedURLException e) {
            throw new RuntimeException("Bad URL " + request.getUrl(), e);
        } catch (IOException e) {
            if (acessfile != null) {
                try {
                    acessfile.close();
                    file.delete();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            int statusCode = 0;
            NetworkResponse networkResponse = null;
            if (httpResponse != null) {
                statusCode = httpResponse.getStatusLine().getStatusCode();
            } else {
                throw new NoConnectionError(e);
            }
            VolleyLog.e("Unexpected response code %d for %s", statusCode, request.getUrl());
            throw new NetworkError(networkResponse);
        }
    }
}

From source file:org.andrewberman.sync.PDFDownloader.java

void retrievePDFsFromHTML() throws Exception {
    if (!baseDir.exists() || !baseDir.canWrite() || !baseDir.isDirectory()) {
        throw new Exception("Error: Destination is read-only or does not exist.");
    }//from ww  w. j  a  va2s .co m
    String base = baseDir.getCanonicalPath() + sep;

    syncBibTex();

    do {
        Thread.sleep(InheritMe.BETWEEN_PDF_DOWNLOADS_SLEEP_TIME);
        getArticleInfo();

        /*
         * First thing's first, create folders and insert the URL links if necessary.
         */
        if (subTags) {
            status("Creating folders and links...");
            createTagFoldersAndLinks(tagSet);
            status("Folders and links created!");
            //            Thread.sleep(PAUSE_TIME/10);
        }

        List<CiteULikeReference> articlesWithoutPDFs = this.getArticlesWithoutPDFs(this.refs);
        List<CiteULikeReference> articlesWithPDFs = new ArrayList<CiteULikeReference>();
        articlesWithPDFs.addAll(this.refs);
        articlesWithPDFs.removeAll(articlesWithoutPDFs);

        itemMax = articlesWithPDFs.size();
        itemNum = 0;

        int i = -1;
        for (CiteULikeReference ref : articlesWithPDFs) {
            System.out.println(ref.userfiles);
            itemNum++;
            i++;
            waitOrExit();

            setArticleLink("Current article ID: " + ref.article_id, ref.href);

            try {
                waitOrExit();
                // Grab the article page's text to get the date.
                Date remote = null;
                //               if (this.uploadNewer) {
                String articleContent = get(ref.href);
                remote = getStampFromArticlePage(articleContent);
                System.out.println("Remote timestamp: " + remote);
                //               }

                status("Checking for existing file...");

                for (String fileName : ref.userfiles.keySet()) {
                    waitOrExit();
                    String fullPath = ref.userfiles.get(fileName);
                    System.out.println(fileName + " -> " + fullPath);

                    /*
                     * Try and put the year first.
                     */
                    String[] bits = fileName.split("_");
                    String yearS = "";
                    String otherFilename = "";
                    String targetFilename = "";
                    if (bits.length == 3) {
                        String flipped = bits[1] + "_" + bits[0] + "_" + bits[2];
                        if (flipFilename) {
                            otherFilename = fileName;
                            targetFilename = flipped;
                        } else {
                            targetFilename = fileName;
                            otherFilename = flipped;
                        }
                        if (subYears) {
                            yearS = String.valueOf(bits[1]);
                        }
                    } else {
                        targetFilename = fileName;
                        otherFilename = fileName;
                    }

                    /*
                     * If we're organized by tags, add a destination file for each of this PDf's tags.
                     * If not, then just add the one outputFile to the outputFiles array. 
                     */
                    ArrayList<File> outputFiles = new ArrayList<File>();
                    ArrayList<File> maybeNewFiles = new ArrayList<File>();
                    File altFile = null;
                    File myFile = null;

                    // Keep track of the newest file and its timestamp.
                    File newestFile = null;
                    long newestStamp = 0;

                    if (subTags && ref.getTags().size() > 0) {
                        for (String tag : ref.getTags()) {
                            if (tag.equalsIgnoreCase("no-tag"))
                                tag = "";
                            String curDir = base + tag + sep;
                            curDir += yearS;
                            myFile = new File(curDir + sep + targetFilename);
                            altFile = new File(curDir + sep + otherFilename);
                            if (myFile.exists()) {
                                newestFile = returnNewer(newestFile, myFile);
                            } else if (altFile.exists()) {
                                try {
                                    altFile.renameTo(myFile);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                                //                        outputFiles.add(myFile);
                            } else {
                                outputFiles.add(myFile);
                            }
                        }
                    } else {
                        myFile = new File(base + yearS + sep + targetFilename);
                        altFile = new File(base + yearS + sep + otherFilename);
                        maybeNewFiles.add(myFile);
                        maybeNewFiles.add(altFile);
                        if (myFile.exists()) {
                            newestFile = returnNewer(newestFile, myFile);
                        } else if (altFile.exists()) {
                            try {
                                altFile.renameTo(myFile);
                            } catch (Exception e) {
                                e.printStackTrace();
                                System.exit(0);
                            }
                            //                     outputFiles.add(myFile);
                        } else {
                            outputFiles.add(myFile);
                        }
                    }

                    // If we have a newest file, check against the server to see if it's much newer.
                    if (newestFile != null && remote != null) {
                        Date local = new Date(newestFile.lastModified());
                        long lT = local.getTime();
                        long rT = remote.getTime();
                        if (lT - rT > (1000) * (60) * (1)) // Fudge factor of 1 minute.
                        {
                            if (uploadNewer) {
                                System.err.println("Local file is newer than remote! Uploading...");
                                status("Local file was modified! Uploading...");

                                // OK. Since CiteULike now uses hashing to evaluate the identity of files, we have to first delete
                                // the existing attachment with this filename.
                                String url = this.BASE_URL + "personal_pdf_delete?";
                                url += "username=" + this.username;
                                url += "&article_id=" + ref.article_id;

                                // Parse the userfile_id from the filename:
                                String userFileId = ref.userfileIds.get(fileName);
                                url += "&userfile_id=" + userFileId;
                                System.out.println(url);
                                get(url);

                                uploadPDF(ref.article_id, newestFile);

                                // Re-collect the timestamp, and re-stamp the local file. This is done so they match after re-uploading.
                                String newContent = get(ref.href);
                                Date remote2 = getStampFromArticlePage(newContent);
                                newestFile.setLastModified(remote2.getTime());
                            }
                        }
                    }
                    if (outputFiles.size() == 0) {
                        status("Already up-to-date!");
                        utd++;
                        continue;
                    }

                    Thread.sleep(200);
                    /*
                     * Download the PDF to the first file.
                     */
                    waitOrExit();
                    status("Downloading...");
                    File f = outputFiles.remove(0);

                    try {
                        String fileUrl = this.BASE_URL + fullPath;
                        this.retriesLeft = 2;
                        downloadURLToFile(fileUrl, f);
                        if (remote != null)
                            f.setLastModified(remote.getTime());
                    } catch (Exception e) {
                        e.printStackTrace();
                        f.delete();
                        throw e;
                    }
                    dl++;

                    /*
                     * Go through rest of tags, and copy file accordingly.
                     * NOTE: This is only entered if this file needs to be copied over locally.
                     */
                    for (int j = 0; j < outputFiles.size(); j++) {
                        status("Copying PDF...");
                        File f2 = outputFiles.get(j);
                        if (f2.exists()) {
                            if (f2.lastModified() > newestStamp) {
                                newestFile = f2;
                                newestStamp = f2.lastModified();
                            }
                            continue;
                        }
                        f2.getParentFile().mkdirs();
                        f2.createNewFile();
                        RandomAccessFile in = new RandomAccessFile(f, "r");
                        RandomAccessFile out = new RandomAccessFile(f2, "rw");

                        byte[] b = new byte[(int) in.length()];
                        in.readFully(b);
                        out.write(b);

                        in.close();
                        out.close();

                        f2.setLastModified(remote.getTime());
                    }
                }
            } catch (Exception e) {
                err++;
                e.printStackTrace();
                status("Failed. See the Java console for more info.");
                Thread.sleep(PAUSE_TIME / 2);
                continue;
            }
        }
    } while (this.refs.size() > 0);
    this.pageNum = 0;
    status("Finished. " + dl + " new, " + utd + " up-to-date and " + err + " failed.");
    out.println("Done!");
}

From source file:com.pheromone.plugins.FileUtils.java

/**
 * Truncate the file to size/*from   www .  j  a  v  a 2 s. c o m*/
 *
 * @param filename
 * @param size
 * @throws FileNotFoundException, IOException
 */
private 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.MustacheMonitor.MustacheMonitor.FileUtils.java

/**
 * Truncate the file to size//from ww w.j ava  2  s .  c o m
 *
 * @param filename
 * @param size
 * @throws FileNotFoundException, IOException
 */
private long truncateFile(String filename, long size) throws FileNotFoundException, IOException {
    filename = stripFileProtocol(filename);

    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:net.ontopia.infoset.content.FileContentStore.java

private void allocateNewBlock() throws ContentStoreException {
    RandomAccessFile out = null;
    boolean exception_thrown = false;
    try {// www  .  j  av a 2 s. com
        out = new RandomAccessFile(key_file, "rws");

        for (int i = 0; i < MAX_SPINS; i++) {
            // acquire exclusive lock
            FileLock l = out.getChannel().tryLock();

            if (l == null) {
                // wait a little before trying again
                try {
                    Thread.sleep(SPIN_TIMEOUT);
                } catch (InterruptedException e) {
                }
                continue;

            } else {
                try {
                    // allocate new key
                    int old_key;
                    int new_key;
                    String content = null;

                    if (out.length() == 0) {
                        old_key = 0;
                        new_key = old_key + KEY_BLOCK_SIZE;

                    } else {
                        try {
                            content = out.readUTF();
                            old_key = Integer.parseInt(content);
                            new_key = old_key + KEY_BLOCK_SIZE;
                        } catch (NumberFormatException e) {
                            if (content.length() > 100)
                                content = content.substring(0, 100) + "...";
                            throw new ContentStoreException(
                                    "Content store key file corrupted. Contained: '" + content + "'");
                        }
                    }

                    // truncate key file and write out new key
                    out.seek(0);
                    out.writeUTF(Integer.toString(new_key));

                    end_of_key_block = new_key;
                    last_key = old_key;
                    return;
                } finally {
                    // release file lock
                    try {
                        l.release();
                    } catch (Throwable t) {
                        throw new ContentStoreException("Could not release key file lock.", t);
                    }
                }
            }
        }
        throw new ContentStoreException("Block allocation timed out.");

    } catch (ContentStoreException e) {
        exception_thrown = true;
        throw e;

    } catch (Throwable t) {
        exception_thrown = true;
        throw new ContentStoreException(t);

    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                if (!exception_thrown)
                    throw new ContentStoreException("Problems occurred when closing content store.", e);
            }
        }
    }
}

From source file:w2v.WordToVec.java

private int readWordIndex(RandomAccessFile raf) throws IOException {
    String word = readWord(raf);//from   ww w .j  a  v  a 2s  .  c  om
    if (raf.length() == raf.getFilePointer()) {
        return -1;
    }
    return searchVocab(word);
}

From source file:org.gcaldaemon.core.ldap.ContactLoader.java

public final synchronized GmailContact[] getContacts() {
    if (contacts == null) {
        try {/*from  w w w  .j  a v a  2s .  c  o  m*/

            // Network down - load contacts from vcards
            File file = new File(vcardDirectory, "contacts.csv");
            if (!file.isFile()) {
                return new GmailContact[0];
            }
            RandomAccessFile raf = new RandomAccessFile(file, "r");
            byte[] bytes = new byte[(int) raf.length()];
            raf.readFully(bytes);
            raf.close();
            String csv = StringUtils.decodeToString(bytes, StringUtils.UTF_8);
            contacts = parseCSV(csv);
            log.debug(contacts.length + " contacts loaded successfully.");
        } catch (Exception ioError) {
            log.warn(ioError);
        }
    }
    return contacts;
}