Example usage for java.io RandomAccessFile seek

List of usage examples for java.io RandomAccessFile seek

Introduction

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

Prototype

public void seek(long pos) throws IOException 

Source Link

Document

Sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs.

Usage

From source file:fr.bmartel.speedtest.SpeedTestTask.java

/**
 * Start FTP upload.// w ww  . j a v a  2 s .  co m
 *
 * @param hostname      ftp host
 * @param port          ftp port
 * @param uri           upload uri
 * @param fileSizeOctet file size in octet
 * @param user          username
 * @param password      password
 */
public void startFtpUpload(final String hostname, final int port, final String uri, final int fileSizeOctet,
        final String user, final String password) {

    mSpeedTestMode = SpeedTestMode.UPLOAD;

    mUploadFileSize = new BigDecimal(fileSizeOctet);
    mForceCloseSocket = false;
    mErrorDispatched = false;

    if (mWriteExecutorService == null || mWriteExecutorService.isShutdown()) {
        mWriteExecutorService = Executors.newSingleThreadExecutor();
    }

    mWriteExecutorService.execute(new Runnable() {
        @Override
        public void run() {

            final FTPClient ftpClient = new FTPClient();
            final RandomGen randomGen = new RandomGen();

            RandomAccessFile uploadFile = null;

            try {
                ftpClient.connect(hostname, port);
                ftpClient.login(user, password);
                ftpClient.enterLocalPassiveMode();
                ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

                byte[] fileContent = new byte[] {};

                if (mSocketInterface.getUploadStorageType() == UploadStorageType.RAM_STORAGE) {
                    /* generate a file with size of fileSizeOctet octet */
                    fileContent = randomGen.generateRandomArray(fileSizeOctet);
                } else {
                    uploadFile = randomGen.generateRandomFile(fileSizeOctet);
                    uploadFile.seek(0);
                }

                mFtpOutputstream = ftpClient.storeFileStream(uri);

                if (mFtpOutputstream != null) {

                    mUploadTempFileSize = 0;

                    final int uploadChunkSize = mSocketInterface.getUploadChunkSize();

                    final int step = fileSizeOctet / uploadChunkSize;
                    final int remain = fileSizeOctet % uploadChunkSize;

                    mTimeStart = System.currentTimeMillis();
                    mTimeEnd = 0;

                    if (mRepeatWrapper.isFirstUpload()) {
                        mRepeatWrapper.setFirstUploadRepeat(false);
                        mRepeatWrapper.setStartDate(mTimeStart);
                    }

                    if (mRepeatWrapper.isRepeatUpload()) {
                        mRepeatWrapper.updatePacketSize(mUploadFileSize);
                    }

                    if (mForceCloseSocket) {
                        SpeedTestUtils.dispatchError(mForceCloseSocket, mListenerList, false, "");
                    } else {
                        for (int i = 0; i < step; i++) {

                            final byte[] chunk = SpeedTestUtils.readUploadData(
                                    mSocketInterface.getUploadStorageType(), fileContent, uploadFile,
                                    mUploadTempFileSize, uploadChunkSize);

                            mFtpOutputstream.write(chunk, 0, uploadChunkSize);

                            mUploadTempFileSize += uploadChunkSize;

                            if (mRepeatWrapper.isRepeatUpload()) {
                                mRepeatWrapper.updateTempPacketSize(uploadChunkSize);
                            }

                            if (!mReportInterval) {

                                final SpeedTestReport report = mSocketInterface.getLiveUploadReport();

                                for (int j = 0; j < mListenerList.size(); j++) {
                                    mListenerList.get(j).onUploadProgress(report.getProgressPercent(), report);
                                }
                            }
                        }

                        if (remain != 0) {

                            final byte[] chunk = SpeedTestUtils.readUploadData(
                                    mSocketInterface.getUploadStorageType(), fileContent, uploadFile,
                                    mUploadTempFileSize, remain);

                            mFtpOutputstream.write(chunk, 0, remain);

                            mUploadTempFileSize += remain;

                            if (mRepeatWrapper.isRepeatUpload()) {
                                mRepeatWrapper.updateTempPacketSize(remain);
                            }
                        }
                        if (!mReportInterval) {
                            final SpeedTestReport report = mSocketInterface.getLiveUploadReport();

                            for (int j = 0; j < mListenerList.size(); j++) {
                                mListenerList.get(j).onUploadProgress(SpeedTestConst.PERCENT_MAX.floatValue(),
                                        report);

                            }
                        }
                        mTimeEnd = System.currentTimeMillis();
                    }
                    mFtpOutputstream.close();

                    mReportInterval = false;
                    final SpeedTestReport report = mSocketInterface.getLiveUploadReport();

                    for (int i = 0; i < mListenerList.size(); i++) {
                        mListenerList.get(i).onUploadFinished(report);
                    }

                    if (!mRepeatWrapper.isRepeatUpload()) {
                        closeExecutors();
                    }

                } else {
                    mReportInterval = false;
                    SpeedTestUtils.dispatchError(mForceCloseSocket, mListenerList, false, "cant create stream "
                            + "from uri " + uri + " with reply code : " + ftpClient.getReplyCode());
                }
            } catch (SocketTimeoutException e) {
                //e.printStackTrace();
                mReportInterval = false;
                mErrorDispatched = true;
                if (!mForceCloseSocket) {
                    SpeedTestUtils.dispatchSocketTimeout(mForceCloseSocket, mListenerList, false,
                            SpeedTestConst.SOCKET_WRITE_ERROR);
                } else {
                    SpeedTestUtils.dispatchError(mForceCloseSocket, mListenerList, false, e.getMessage());
                }
                closeSocket();
                closeExecutors();
            } catch (IOException e) {
                //e.printStackTrace();
                mReportInterval = false;
                mErrorDispatched = true;
                SpeedTestUtils.dispatchError(mForceCloseSocket, mListenerList, false, e.getMessage());
                closeExecutors();
            } finally {
                mErrorDispatched = false;
                mSpeedTestMode = SpeedTestMode.NONE;
                disconnectFtp(ftpClient);
                if (uploadFile != null) {
                    try {
                        uploadFile.close();
                        randomGen.deleteFile();
                    } catch (IOException e) {
                        //e.printStackTrace();
                    }
                }
            }
        }
    });
}

From source file:org.kawanfw.file.servlet.util.FileTransferManager.java

public void upload(FileConfigurator fileConfigurator, InputStream inputStream, String username, String filename,
        long chunkLength) throws IOException {

    debug(new Date() + " UPLOAD SESSION BEGIN ");

    filename = HttpConfigurationUtil.addRootPath(fileConfigurator, username, filename);

    // is it a file chunk? If yes append to filename
    if (filename.endsWith(".kawanfw.chunk") || filename.endsWith(".kawanfw.chunk.LASTCHUNK")) {

        RandomAccessFile raf = null;

        try {/*from www  .ja v  a 2s.  c  om*/

            boolean lastChunk = false;

            if (filename.endsWith(".LASTCHUNK")) {
                debug(new Date() + " RENAME DONE");
                filename = StringUtils.substringBeforeLast(filename, ".LASTCHUNK");
                lastChunk = true;
            }

            initFileIfFirstChunk(username, filename);

            String rawFilename = StringUtils.substringBeforeLast(filename, ".kawanfw.chunk");
            String indexStr = StringUtils.substringAfterLast(rawFilename, ".");

            // Remove the number
            rawFilename = StringUtils.substringBeforeLast(rawFilename, ".");

            int index = Integer.parseInt(indexStr);

            File file = new File(rawFilename);

            debug(new Date() + " SESSION INDEX" + " " + index);

            // We must create, if necessary, the path to the file
            createParentDir(file);

            debug(new Date() + " BEFORE CREATE RAF");
            raf = new RandomAccessFile(file, "rw");
            debug(new Date() + " AFTER CREATE RAF");

            // We seek the total length of previous files, because client
            // method
            // is idempotent and may be replayed
            long lengthToSeek = (index - 1) * chunkLength;

            // debug("index       : " + index);
            // debug("chunkLength : " + chunkLength);
            // debug("lengthToSeek: " + lengthToSeek);

            debug(new Date() + " BEFORE SEEK ");
            raf.seek(lengthToSeek);

            debug(new Date() + " BEFORE COPY ");
            copy(inputStream, raf, new byte[DEFAULT_BUFFER_SIZE]);
            debug(new Date() + " AFTER COPY ");

            IOUtils.closeQuietly(raf);

            if (lastChunk) {
                // End of operations
                // Do nothing with Random Access Files
            }

        } finally {
            IOUtils.closeQuietly(raf);
        }

    } else {

        OutputStream out = null;

        try {
            File file = new File(filename);

            // We must create, if necessary, the path to the file
            createParentDir(file);

            out = new BufferedOutputStream(new FileOutputStream(file));
            IOUtils.copy(inputStream, out);

            debug("file created : " + file);
            debug("file.length(): " + file.length());
        } finally {
            IOUtils.closeQuietly(out);
        }
    }

}

From source file:org.commoncrawl.service.listcrawler.CrawlHistoryManager.java

/**
 * updateLogFileHeader - update the log file header called via the log file
 * writer thread .../* w  ww  .  ja va2s . c o  m*/
 * 
 * @throws IOException
 */
void updateLogFileHeader(File logFileName, long newlyAddedItemsCount, long newItemsFileSize)
        throws IOException {

    RandomAccessFile file = new RandomAccessFile(logFileName, "rw");

    try {

        synchronized (_header) {
            // update cached header ...
            _header._fileSize += newItemsFileSize;
            _header._itemCount += newlyAddedItemsCount;
            // set the position at zero ..
            file.seek(0);
            // and write header to disk ...
            _header.writeHeader(file);
        }
    } finally {
        // major bottle neck..
        // file.getFD().sync();
        file.close();
    }
}

From source file:org.commoncrawl.service.listcrawler.CrawlList.java

public static void dumpUnCrawledItems(File dataDir, long listId, File outputFilePath,
        boolean includeRobotsExcludedItems) throws IOException {

    File fixedDataFile = new File(dataDir, LIST_VALUE_MAP_PREFIX + Long.toString(listId));
    File variableDataFile = new File(dataDir, LIST_STRING_MAP_PREFIX + Long.toString(listId));

    LOG.info("FixedDataFile is:" + fixedDataFile);
    LOG.info("VariableDataFile is:" + variableDataFile);

    RandomAccessFile fixedDataReader = new RandomAccessFile(fixedDataFile, "r");
    RandomAccessFile stringDataReader = new RandomAccessFile(variableDataFile, "r");

    JsonWriter writer = new JsonWriter(new BufferedWriter(new FileWriter(outputFilePath), 1024 * 1024 * 10));

    writer.setIndent(" ");

    try {/* w  w w .ja v  a2  s. com*/
        writer.beginObject();
        writer.name("urls");
        writer.beginArray();
        try {

            OnDiskCrawlHistoryItem item = new OnDiskCrawlHistoryItem();
            URLFP fingerprint = new URLFP();

            while (fixedDataReader.getFilePointer() != fixedDataReader.length()) {

                long position = fixedDataReader.getFilePointer();

                item.deserialize(fixedDataReader);

                // seek to string data 
                stringDataReader.seek(item._stringsOffset);
                // and skip buffer length 
                WritableUtils.readVInt(stringDataReader);
                // and read primary string 
                String url = stringDataReader.readUTF();
                // setup fingerprint 
                fingerprint.setDomainHash(item._domainHash);
                fingerprint.setUrlHash(item._urlFingerprint);

                // any item that has not been crawled needs to be queued 
                boolean queueItem = !item.isFlagSet(OnDiskCrawlHistoryItem.FLAG_HAS_CRAWL_STATUS);

                // if item is not queued, check to see if we need to retry the item 
                if (!queueItem && item.isFlagSet(OnDiskCrawlHistoryItem.FLAG_HAS_CRAWL_STATUS)) {

                    if (item.isFlagSet(OnDiskCrawlHistoryItem.FLAG_HAS_REDIRECT_STATUS)) {

                        queueItem = (item._redirectStatus != 0);

                        if (!queueItem) {
                            if (item._redirectHttpResult != 200 && item._redirectHttpResult != 404) {
                                queueItem = true;
                            }
                        }
                    } else {
                        queueItem = (item._crawlStatus != 0);

                        if (!queueItem) {
                            if (item._httpResultCode != 200 && item._httpResultCode != 404) {
                                queueItem = true;
                            }
                        }
                    }
                }

                if (queueItem) {
                    // ok if queue item is set ... 
                    writer.beginObject();
                    writer.name("url");
                    writer.value(url);
                    writer.name("redirected");
                    writer.value((boolean) item.isFlagSet(OnDiskCrawlHistoryItem.FLAG_HAS_REDIRECT_STATUS));
                    writer.name("lastStatus");
                    if (item.isFlagSet(OnDiskCrawlHistoryItem.FLAG_HAS_REDIRECT_STATUS)) {
                        if (item._redirectStatus == 0) {
                            writer.value("HTTP-" + item._redirectHttpResult);
                        } else {
                            writer.value(CrawlURL.FailureReason.toString(item._redirectHttpResult));
                        }
                    } else {
                        if (item.isFlagSet(OnDiskCrawlHistoryItem.FLAG_HAS_CRAWL_STATUS)) {
                            if (item._crawlStatus == 0) {
                                writer.value("HTTP-" + item._httpResultCode);
                            } else {
                                writer.value(CrawlURL.FailureReason.toString(item._crawlStatus));
                            }
                        } else {
                            writer.value("UNCRAWLED");
                        }
                    }
                    writer.name("updateTime");
                    writer.value(item._updateTimestamp);
                    writer.endObject();
                }
            }
        } catch (IOException e) {
            LOG.error("Encountered Exception Queueing Items for List:" + listId + " Exception:"
                    + CCStringUtils.stringifyException(e));
        } finally {
            fixedDataReader.close();
            stringDataReader.close();
        }

        writer.endArray();
        writer.endObject();
    } catch (Exception e) {
        LOG.error(CCStringUtils.stringifyException(e));
        throw new IOException(e);
    } finally {
        writer.flush();
        writer.close();
    }

}

From source file:ar.com.qbe.siniestros.model.utils.MimeMagic.MagicMatcher.java

/**
 * test to see if this match or any submatches match
 *
 * @param f the file that should be used to test the match
 * @param onlyMimeMatch DOCUMENT ME!/*w  ww.j a  v  a  2s .co m*/
 *
 * @return the deepest magic match object that matched
 *
 * @throws IOException DOCUMENT ME!
 * @throws UnsupportedTypeException DOCUMENT ME!
 */
public MagicMatch test(File f, boolean onlyMimeMatch) throws IOException, UnsupportedTypeException {
    log.debug("test(File)");

    int offset = match.getOffset();
    String description = match.getDescription();
    String type = match.getType();
    String mimeType = match.getMimeType();

    log.debug("test(File): testing '" + f.getName() + "' for '" + description + "'");

    log.debug("test(File): \n=== BEGIN MATCH INFO ==");
    log.debug(match.print());
    log.debug("test(File): \n=== END MATCH INFO ====\n");

    RandomAccessFile file = null;
    file = new RandomAccessFile(f, "r");

    try {
        int length = 0;

        if (type.equals("byte")) {
            length = 1;
        } else if (type.equals("short") || type.equals("leshort") || type.equals("beshort")) {
            length = 4;
        } else if (type.equals("long") || type.equals("lelong") || type.equals("belong")) {
            length = 8;
        } else if (type.equals("string")) {
            length = match.getTest().capacity();
        } else if (type.equals("regex")) {

            final int matchLength = match.getLength();
            length = (matchLength == 0) ? (int) file.length() - offset : matchLength;

            if (length < 0) {
                length = 0;
            }
        } else if (type.equals("detector")) {
            length = (int) file.length() - offset;

            if (length < 0) {
                length = 0;
            }
        } else {
            throw new UnsupportedTypeException("unsupported test type '" + type + "'");
        }

        // we know this match won't work since there isn't enough data for the test
        if (length > (file.length() - offset)) {
            return null;
        }

        byte[] buf = new byte[length];
        file.seek(offset);

        int bytesRead = 0;
        int size = 0;
        boolean gotAllBytes = false;
        boolean done = false;

        while (!done) {
            size = file.read(buf, 0, length - bytesRead);

            if (size == -1) {
                throw new IOException("reached end of file before all bytes were read");
            }

            bytesRead += size;

            if (bytesRead == length) {
                gotAllBytes = true;
                done = true;
            }
        }

        log.debug("test(File): stream size is '" + buf.length + "'");

        MagicMatch match = null;
        MagicMatch submatch = null;

        if (testInternal(buf)) {
            // set the top level match to this one
            try {
                match = getMatch() != null ? (MagicMatch) getMatch().clone() : null;
            } catch (CloneNotSupportedException e) {
                // noop
            }

            log.debug("test(File): testing matched '" + description + "'");

            // set the data on this match
            if ((onlyMimeMatch == false) && (subMatchers != null) && (subMatchers.size() > 0)) {
                log.debug(
                        "test(File): testing " + subMatchers.size() + " submatches for '" + description + "'");

                for (int i = 0; i < subMatchers.size(); i++) {
                    log.debug("test(File): testing submatch " + i);

                    MagicMatcher m = (MagicMatcher) subMatchers.get(i);

                    if ((submatch = m.test(f, false)) != null) {
                        log.debug("test(File): submatch " + i + " matched with '" + submatch.getDescription()
                                + "'");
                        match.addSubMatch(submatch);
                    } else {
                        log.debug("test(File): submatch " + i + " doesn't match");
                    }
                }
            }
        }

        return match;
    } finally {
        try {
            file.close();
        } catch (Exception fce) {
        }
    }
}

From source file:au.org.ala.layers.dao.ObjectDAOImpl.java

@Override
public List<Objects> getObjectsById(String id, int start, int pageSize) {
    logger.info("Getting object info for fid = " + id);
    String limit_offset = " limit " + (pageSize < 0 ? "all" : pageSize) + " offset " + start;
    String sql = "select o.pid as pid, o.id as id, o.name as name, o.desc as description, "
            + "o.fid as fid, f.name as fieldname, o.bbox, o.area_km, "
            + "ST_AsText(ST_Centroid(o.the_geom)) as centroid,"
            + "GeometryType(o.the_geom) as featureType from objects o, fields f "
            + "where o.fid = ? and o.fid = f.id order by o.pid " + limit_offset;
    List<Objects> objects = jdbcTemplate.query(sql,
            ParameterizedBeanPropertyRowMapper.newInstance(Objects.class), id);

    updateObjectWms(objects);//from ww  w  . j  av  a  2 s  .c  o m

    // get grid classes
    if (objects == null || objects.isEmpty()) {
        objects = new ArrayList<Objects>();
        IntersectionFile f = layerIntersectDao.getConfig().getIntersectionFile(id);
        if (f != null && f.getClasses() != null) {
            //shape position
            int pos = 0;

            for (Entry<Integer, GridClass> c : f.getClasses().entrySet()) {
                File file = new File(f.getFilePath() + File.separator + c.getKey() + ".wkt.index.dat");
                if (f.getType().equals("a") || !file.exists()) { // class pid
                    if (pageSize == -1 || (pos >= start && pos - start < pageSize)) {
                        Objects o = new Objects();
                        o.setPid(f.getLayerPid() + ":" + c.getKey());
                        o.setId(f.getLayerPid() + ":" + c.getKey());
                        o.setName(c.getValue().getName());
                        o.setFid(f.getFieldId());
                        o.setFieldname(f.getFieldName());
                        o.setBbox(c.getValue().getBbox());
                        o.setArea_km(c.getValue().getArea_km());
                        o.setWmsurl(getGridClassWms(f.getLayerName(), c.getValue()));
                        objects.add(o);
                    }
                    pos++;

                    if (pageSize != -1 && pos >= start + pageSize) {
                        break;
                    }
                } else { // polygon pid
                    RandomAccessFile raf = null;
                    try {
                        raf = new RandomAccessFile(file, "r");
                        long itemSize = (4 + 4 + 4 * 4 + 4);
                        long len = raf.length() / itemSize; // group

                        if (pageSize != -1 && pos + len < start) {
                            pos += len;
                        } else {
                            // number,
                            // character
                            // offset,
                            // minx,
                            // miny,
                            // maxx,
                            // maxy,
                            // area
                            // sq
                            // km
                            int i = 0;
                            if (pageSize != -1 && pos < start) {
                                //the first object requested is in this file, seek to the start
                                i = start - pos;
                                pos += i;
                                raf.seek(i * itemSize);
                            }
                            for (; i < len; i++) {
                                int n = raf.readInt();
                                /* int charoffset = */
                                raf.readInt();
                                float minx = raf.readFloat();
                                float miny = raf.readFloat();
                                float maxx = raf.readFloat();
                                float maxy = raf.readFloat();
                                float area = raf.readFloat();

                                if (pageSize == -1 || (pos >= start && pos - start < pageSize)) {
                                    Objects o = new Objects();
                                    o.setPid(f.getLayerPid() + ":" + c.getKey() + ":" + n);
                                    o.setId(f.getLayerPid() + ":" + c.getKey() + ":" + n);
                                    o.setName(c.getValue().getName());
                                    o.setFid(f.getFieldId());
                                    o.setFieldname(f.getFieldName());

                                    o.setBbox("POLYGON((" + minx + " " + miny + "," + minx + " " + maxy + ","
                                            + +maxx + " " + maxy + "," + +maxx + " " + miny + "," + +minx + " "
                                            + miny + "))");
                                    o.setArea_km(1.0 * area);

                                    o.setWmsurl(getGridPolygonWms(f.getLayerName(), n));

                                    objects.add(o);
                                }

                                pos++;

                                if (pageSize != -1 && pos >= start + pageSize) {
                                    break;
                                }
                            }
                        }
                    } catch (Exception e) {
                        logger.error(e.getMessage(), e);
                    } finally {
                        if (raf != null) {
                            try {
                                raf.close();
                            } catch (Exception e) {
                                logger.error(e.getMessage(), e);
                            }
                        }
                    }

                    if (pageSize != -1 && pos >= start + pageSize) {
                        break;
                    }
                }
            }
        }
    }
    return objects;
}

From source file:org.commoncrawl.service.listcrawler.CacheManager.java

/**
 * loadCache - load local cache from disk 
 * @param activeLogPath//from w ww.  j  a  v a2  s  . c  om
 * @param logFileHeader
 * @throws IOException
 */
private synchronized void loadCache(File activeLogPath, LocalLogFileHeader logFileHeader) throws IOException {

    RandomAccessFile file = new RandomAccessFile(getActiveLogFilePath(), "rw");

    byte[] syncCheck = new byte[_header._sync.length];

    try {

        long lastValidPos = LocalLogFileHeader.SIZE;
        long currentPos = lastValidPos;
        long endPos = file.length();

        CacheItemHeader itemHeader = new CacheItemHeader();

        // start read 
        while (currentPos < endPos) {

            if ((endPos - currentPos) < LocalLogFileHeader.SYNC_BYTES_SIZE)
                break;

            // seek to current position ... 
            file.seek(currentPos);

            boolean headerLoadFailed = false;

            try {
                // read the item header ... assuming things are good so far ... 
                itemHeader.readHeader(file);
            } catch (IOException e) {
                LOG.error("### Item Header Load Failed With Exception:" + CCStringUtils.stringifyException(e));
                headerLoadFailed = true;
            }

            if (headerLoadFailed) {
                LOG.error("### Item File Corrupt at position:" + currentPos + " Seeking Next Sync Point");
                currentPos += LocalLogFileHeader.SYNC_BYTES_SIZE;
            }

            // if header sync bytes don't match .. then seek to next sync position ... 
            if (headerLoadFailed || !Arrays.equals(itemHeader._sync, _header._sync)) {

                LOG.error("### Item File Corrupt at position:" + currentPos + " Seeking Next Sync Point");

                // reseek to current pos 
                file.seek(currentPos);
                // read in a sync.length buffer amount 
                file.readFully(syncCheck);

                int syncLen = _header._sync.length;

                // start scan for next sync position ...
                for (int i = 0; file.getFilePointer() < endPos; i++) {
                    int j = 0;
                    for (; j < syncLen; j++) {
                        if (_header._sync[j] != syncCheck[(i + j) % syncLen])
                            break;
                    }
                    if (j == syncLen) {
                        file.seek(file.getFilePointer() - LocalLogFileHeader.SYNC_BYTES_SIZE); // position before sync
                        break;
                    }
                    syncCheck[i % syncLen] = file.readByte();
                }
                // whatever, happened file pointer is at current pos 
                currentPos = file.getFilePointer();

                if (currentPos < endPos) {
                    LOG.info("### Item Loader Found another sync point at:" + currentPos);
                } else {
                    LOG.error("### No more sync points found!");
                }
            } else {
                // ok figure out next steps based on header ... 
                // for now, just add item to our list ... 
                _fingerprintToLocalLogPos.put(itemHeader._fingerprint, _localLogStartOffset + currentPos);
                // now seek past data
                currentPos += CacheItemHeader.SIZE + itemHeader._dataLength + ITEM_RECORD_TRAILING_BYTES;
            }
        }
    } finally {
        if (file != null) {
            file.close();
        }
    }
}

From source file:com.xperia64.rompatcher.MainActivity.java

public void patch(final boolean c, final boolean d, final boolean r, final String ed) {
    final ProgressDialog myPd_ring = ProgressDialog.show(MainActivity.this,
            getResources().getString(R.string.wait), getResources().getString(R.string.wait_desc), true);
    myPd_ring.setCancelable(false);//from   ww w.j  a  va 2 s .  c  o m
    new Thread(new Runnable() {
        public void run() {
            if (new File(Globals.patchToApply).exists() && new File(Globals.fileToPatch).exists()
                    && !Globals.fileToPatch.toLowerCase(Locale.US).endsWith(".ecm")) {
                String msg = getResources().getString(R.string.success);
                if (!new File(Globals.fileToPatch).canWrite()) {
                    Globals.msg = msg = "Can not write to output file. If you are on KitKat or Lollipop, move the file to your internal storage.";
                    return;
                }
                if (Globals.patchToApply.toLowerCase(Locale.US).endsWith(".ups")) {
                    int e = upsPatchRom(Globals.fileToPatch, Globals.patchToApply, Globals.fileToPatch + ".new",
                            r ? 1 : 0);
                    if (e != 0) {
                        msg = parseError(e, Globals.TYPE_UPS);
                    }
                } else if (Globals.patchToApply.toLowerCase(Locale.US).endsWith(".xdelta")
                        || Globals.patchToApply.toLowerCase(Locale.US).endsWith(".xdelta3")
                        || Globals.patchToApply.toLowerCase(Locale.US).endsWith(".vcdiff")) {
                    RandomAccessFile f = null;
                    try {
                        f = new RandomAccessFile(Globals.patchToApply, "r");
                    } catch (FileNotFoundException e1) {
                        e1.printStackTrace();
                        Globals.msg = msg = getResources().getString(R.string.fnf);
                        return;
                    }
                    StringBuilder s = new StringBuilder();
                    try {
                        if (f.length() >= 9) {
                            for (int i = 0; i < 8; i++) {
                                s.append((char) f.readByte());
                                f.seek(i + 1);
                            }
                        }
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                    try {
                        f.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                    // Header of xdelta patch determines version
                    if (s.toString().equals("%XDELTA%") || s.toString().equals("%XDZ000%")
                            || s.toString().equals("%XDZ001%") || s.toString().equals("%XDZ002%")
                            || s.toString().equals("%XDZ003%") || s.toString().equals("%XDZ004%")) {
                        int e = xdelta1PatchRom(Globals.fileToPatch, Globals.patchToApply,
                                Globals.fileToPatch + ".new");
                        if (e != 0) {
                            msg = parseError(e, Globals.TYPE_XDELTA1);
                        }
                    } else {
                        int e = xdelta3PatchRom(Globals.fileToPatch, Globals.patchToApply,
                                Globals.fileToPatch + ".new");
                        if (e != 0) {
                            msg = parseError(e, Globals.TYPE_XDELTA3);
                        }
                    }
                } else if (Globals.patchToApply.toLowerCase(Locale.US).endsWith(".bps")) {
                    int e = bpsPatchRom(Globals.fileToPatch, Globals.patchToApply, Globals.fileToPatch + ".new",
                            r ? 1 : 0);
                    if (e != 0) {
                        msg = parseError(e, Globals.TYPE_BPS);
                    }
                } else if (Globals.patchToApply.toLowerCase(Locale.US).endsWith(".dps")) {
                    int e = dpsPatchRom(Globals.fileToPatch, Globals.patchToApply,
                            Globals.fileToPatch + ".new");
                    if (e != 0) {
                        msg = parseError(e, Globals.TYPE_DPS);
                    }
                } else if (Globals.patchToApply.toLowerCase(Locale.US).endsWith(".bsdiff")) {
                    int e = bsdiffPatchRom(Globals.fileToPatch, Globals.patchToApply,
                            Globals.fileToPatch + ".new");
                    if (e != 0) {
                        msg = parseError(e, Globals.TYPE_BSDIFF);
                    }

                } else if (Globals.patchToApply.toLowerCase(Locale.US).endsWith(".aps")) {
                    File f = new File(Globals.fileToPatch);
                    File f2 = new File(Globals.fileToPatch + ".bak");
                    try {
                        Files.copy(f, f2);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    // Wow.
                    byte[] gbaSig = { 0x41, 0x50, 0x53, 0x31, 0x00 };
                    byte[] n64Sig = { 0x41, 0x50, 0x53, 0x31, 0x30 };
                    byte[] realSig = new byte[5];
                    RandomAccessFile raf = null;
                    System.out.println("APS Patch");
                    try {
                        raf = new RandomAccessFile(Globals.patchToApply, "r");
                    } catch (FileNotFoundException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                        Globals.msg = msg = getResources().getString(R.string.fnf);
                        return;
                    }
                    try {
                        raf.read(realSig);
                        raf.close();
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }

                    if (Arrays.equals(realSig, gbaSig)) {
                        System.out.println("GBA APS");
                        APSGBAPatcher aa = new APSGBAPatcher();
                        aa.crcTableInit();
                        int e = 0;
                        try {
                            e = aa.ApplyPatch(Globals.patchToApply, Globals.fileToPatch, r);
                        } catch (IOException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                            e = -5;
                        }
                        System.out.println("e: " + e);
                        if (e != 0) {
                            msg = parseError(e, Globals.TYPE_APSGBA);
                        }
                    } else if (Arrays.equals(realSig, n64Sig)) {
                        System.out.println("N64 APS");
                        int e = apsN64PatchRom(Globals.fileToPatch, Globals.patchToApply);
                        if (e != 0) {
                            msg = parseError(e, Globals.TYPE_APSN64);
                        }
                    } else {
                        msg = parseError(-131, -10000);
                    }

                } else if (Globals.patchToApply.toLowerCase(Locale.US).endsWith(".ppf")) {
                    File f = new File(Globals.fileToPatch);
                    File f2 = new File(Globals.fileToPatch + ".bak");
                    try {
                        Files.copy(f, f2);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    int e = ppfPatchRom(Globals.fileToPatch, Globals.patchToApply, r ? 1 : 0);
                    if (e != 0) {
                        msg = parseError(e, Globals.TYPE_PPF);
                    }

                } else if (Globals.patchToApply.toLowerCase(Locale.US).endsWith(".patch")) {
                    int e = xdelta1PatchRom(Globals.fileToPatch, Globals.patchToApply,
                            Globals.fileToPatch + ".new");
                    if (e != 0) {
                        msg = parseError(e, Globals.TYPE_XDELTA1);
                    }
                } else if (Globals.patchToApply.toLowerCase(Locale.US).endsWith(".asm")) {
                    File f = new File(Globals.fileToPatch);
                    File f2 = new File(Globals.fileToPatch + ".bak");
                    try {
                        Files.copy(f, f2);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    int e;
                    if (Globals.asar)
                        e = asarPatchRom(Globals.fileToPatch, Globals.patchToApply, r ? 1 : 0);
                    else
                        e = asmPatchRom(Globals.fileToPatch, Globals.patchToApply);
                    if (e != 0) {
                        msg = parseError(e, Globals.TYPE_ASM);
                    }
                } else if (Globals.patchToApply.toLowerCase(Locale.US).endsWith(".dldi")) {
                    File f = new File(Globals.fileToPatch);
                    File f2 = new File(Globals.fileToPatch + ".bak");
                    try {
                        Files.copy(f, f2);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    int e = dldiPatchRom(Globals.fileToPatch, Globals.patchToApply);
                    if (e != 0) {
                        msg = parseError(e, Globals.TYPE_DLDI);
                    }
                } else if (Globals.patchToApply.toLowerCase(Locale.US).endsWith(".xpc")) {
                    int e = xpcPatchRom(Globals.fileToPatch, Globals.patchToApply,
                            Globals.fileToPatch + ".new");
                    if (e != 0) {
                        msg = parseError(e, Globals.TYPE_XPC);
                    }

                } else if (Globals.patchToApply.toLowerCase(Locale.US).endsWith(".nmp")) {

                    String drm = MainActivity.this.getPackageName();
                    System.out.println("Drm is: " + drm);
                    if (drm.equals("com.xperia64.rompatcher.donation")) {
                        if (c) {
                            File f = new File(Globals.fileToPatch);
                            File f2 = new File(Globals.fileToPatch + ".bak");
                            try {
                                Files.copy(f, f2);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                        NitroROMFilesystem fs;
                        try {
                            fs = new NitroROMFilesystem(Globals.fileToPatch);
                            ROM.load(fs);
                            RealPatch.patch(Globals.patchToApply, new Object());
                            ROM.close();
                        } catch (Exception e1) {
                            // TODO Auto-generated catch block
                            //e1.printStackTrace();
                            Globals.msg = msg = String.format(getResources().getString(R.string.nmpDefault),
                                    e1.getStackTrace()[0].getFileName(), e1.getStackTrace()[0].getLineNumber());
                        }
                        if (c && d && !TextUtils.isEmpty(ed)) {
                            File f = new File(Globals.fileToPatch);
                            File f3 = new File(Globals.fileToPatch + ".bak");
                            File f2 = new File(
                                    Globals.fileToPatch.substring(0, Globals.fileToPatch.lastIndexOf('/') + 1)
                                            + ed);
                            f.renameTo(f2);
                            f3.renameTo(f);
                        }
                    } else {
                        Globals.msg = msg = getResources().getString(R.string.drmwarning);
                        MainActivity.this.runOnUiThread(new Runnable() {
                            public void run() {
                                AlertDialog.Builder b = new AlertDialog.Builder(MainActivity.this);
                                b.setTitle(getResources().getString(R.string.drmwarning));
                                b.setIcon(
                                        ResourcesCompat.getDrawable(getResources(), R.drawable.icon_pro, null));
                                b.setMessage(getResources().getString(R.string.drmwarning_desc));
                                b.setCancelable(false);
                                b.setNegativeButton(getResources().getString(android.R.string.cancel),
                                        new OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface arg0, int arg1) {
                                            }
                                        });
                                b.setPositiveButton(getResources().getString(R.string.nagInfo),
                                        new OnClickListener() {

                                            @Override
                                            public void onClick(DialogInterface arg0, int arg1) {
                                                try {
                                                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(
                                                            "market://details?id=com.xperia64.rompatcher.donation")));
                                                } catch (android.content.ActivityNotFoundException anfe) {
                                                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(
                                                            "http://play.google.com/store/apps/details?id=com.xperia64.rompatcher.donation")));
                                                }
                                            }
                                        });
                                b.create().show();
                            }
                        });
                    }

                } else {
                    RandomAccessFile f = null;
                    try {
                        f = new RandomAccessFile(Globals.patchToApply, "r");
                    } catch (FileNotFoundException e1) {
                        e1.printStackTrace();
                        Globals.msg = msg = getResources().getString(R.string.fnf);
                        return;
                    }
                    StringBuilder s = new StringBuilder();
                    try {
                        if (f.length() >= 6) {
                            for (int i = 0; i < 5; i++) {
                                s.append((char) f.readByte());
                                f.seek(i + 1);
                            }
                        }
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                    try {
                        f.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                    int e;
                    // Two variants of IPS, the normal PATCH type, then this weird one called IPS32 with messily hacked in 32 bit offsets
                    if (s.toString().equals("IPS32")) {
                        e = ips32PatchRom(Globals.fileToPatch, Globals.patchToApply);
                        if (e != 0) {
                            msg = parseError(e, Globals.TYPE_IPS);
                        }
                    } else {
                        e = ipsPatchRom(Globals.fileToPatch, Globals.patchToApply);
                        if (e != 0) {
                            msg = parseError(e, Globals.TYPE_IPS);
                        }
                    }

                }
                if (Globals.patchToApply.toLowerCase(Locale.US).endsWith(".ups")
                        || Globals.patchToApply.toLowerCase(Locale.US).endsWith(".xdelta")
                        || Globals.patchToApply.toLowerCase(Locale.US).endsWith(".xdelta3")
                        || Globals.patchToApply.toLowerCase(Locale.US).endsWith(".vcdiff")
                        || Globals.patchToApply.toLowerCase(Locale.US).endsWith(".patch")
                        || Globals.patchToApply.toLowerCase(Locale.US).endsWith(".bps")
                        || Globals.patchToApply.toLowerCase(Locale.US).endsWith(".bsdiff")
                        || Globals.patchToApply.toLowerCase(Locale.US).endsWith(".dps")
                        || Globals.patchToApply.toLowerCase(Locale.US).endsWith(".xpc")) {
                    File oldrom = new File(Globals.fileToPatch);
                    File bkrom = new File(Globals.fileToPatch + ".bak");
                    oldrom.renameTo(bkrom);
                    File newrom = new File(Globals.fileToPatch + ".new");
                    newrom.renameTo(oldrom);
                }
                if (!c) {
                    File f = new File(Globals.fileToPatch + ".bak");
                    if (f.exists()) {
                        f.delete();
                    }
                } else {
                    if (d) {
                        File one = new File(Globals.fileToPatch + ".bak");
                        File two = new File(Globals.fileToPatch);
                        File three = new File(
                                Globals.fileToPatch.substring(0, Globals.fileToPatch.lastIndexOf('/') + 1)
                                        + ed);
                        two.renameTo(three);
                        File four = new File(Globals.fileToPatch);
                        one.renameTo(four);
                    }
                }
                Globals.msg = msg;
            } else if (Globals.fileToPatch.toLowerCase(Locale.US).endsWith(".ecm")) {
                int e = 0;
                String msg = getResources().getString(R.string.success);
                if (c) {

                    if (d) {
                        e = ecmPatchRom(Globals.fileToPatch,
                                Globals.fileToPatch.substring(0, Globals.fileToPatch.lastIndexOf('/')) + ed, 1);
                    } else {
                        //new File(Globals.fileToPatch).renameTo(new File(Globals.fileToPatch+".bak"));
                        e = ecmPatchRom(Globals.fileToPatch,
                                Globals.fileToPatch.substring(0, Globals.fileToPatch.lastIndexOf('.')), 1);
                    }
                } else {
                    e = ecmPatchRom(Globals.fileToPatch, "", 0);
                }
                if (e != 0) {
                    msg = parseError(e, Globals.TYPE_ECM);
                }
                Globals.msg = msg;
            } else {
                Globals.msg = getResources().getString(R.string.fnf);
            }

        }
    }).start();
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                while (Globals.msg.equals("")) {
                    Thread.sleep(25);
                }
                ;
                myPd_ring.dismiss();
                runOnUiThread(new Runnable() {
                    public void run() {
                        Toast t = Toast.makeText(staticThis, Globals.msg, Toast.LENGTH_SHORT);
                        t.show();
                        Globals.msg = "";
                    }
                });
                if (Globals.msg.equals(getResources().getString(R.string.success))) // Don't annoy people who did something wrong even further
                {
                    final SharedPreferences prefs = PreferenceManager
                            .getDefaultSharedPreferences(MainActivity.this);
                    int x = prefs.getInt("purchaseNag", 5);
                    if (x != -1) {
                        if ((isPackageInstalled("com.xperia64.timidityae", MainActivity.this)
                                || isPackageInstalled("com.xperia64.rompatcher.donation", MainActivity.this))) {
                            prefs.edit().putInt("purchaseNag", -1);
                        } else {
                            if (x >= 5) {

                                prefs.edit().putInt("purchaseNag", 0).commit();
                                /*runOnUiThread(new Runnable() {
                                    public void run() {
                                AlertDialog.Builder b = new AlertDialog.Builder(MainActivity.this);
                                b.setTitle("Like ROM Patcher?");
                                b.setIcon(getResources().getDrawable(R.drawable.icon_pro));
                                b.setMessage(getResources().getString(R.string.nagMsg));
                                b.setCancelable(false);
                                b.setNegativeButton(getResources().getString(android.R.string.cancel), new OnClickListener(){@Override public void onClick(DialogInterface arg0, int arg1) {}});
                                b.setPositiveButton(getResources().getString(R.string.nagInfo), new OnClickListener(){
                                        
                                  @Override
                                  public void onClick(DialogInterface arg0, int arg1) {
                                     try  {
                                         startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=com.xperia64.rompatcher.donation")));
                                     } catch (android.content.ActivityNotFoundException anfe) {
                                         startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=com.xperia64.rompatcher.donation")));
                                     }
                                  }
                                   });
                                      b.create().show();
                                       }
                                   }); // end of UIThread*/
                            } else {
                                prefs.edit().putInt("purchaseNag", x + 1).commit();
                            }
                        }
                    }
                }

            } catch (Exception e) {
            }
        }
    }).start();
}

From source file:org.commoncrawl.service.listcrawler.CrawlHistoryManager.java

/**
 * seek out next instance of sync bytes in the file input stream
 * //from w  w w . j a  v  a 2 s. c om
 * @param file
 * @throws IOException
 */
private boolean seekToNextSyncBytesPos(RandomAccessFile file) throws IOException {
    // read in a sync.length buffer amount
    file.read(_syncByteBuffer);

    int syncLen = _header._sync.length;

    // start scan for next sync position ...
    for (int i = 0; file.getFilePointer() < _header._fileSize; i++) {
        int j = 0;
        for (; j < syncLen; j++) {
            if (_header._sync[j] != _syncByteBuffer[(i + j) % syncLen])
                break;
        }
        if (j == syncLen) {
            // found matching sync bytes - reset file pos to before sync bytes
            file.seek(file.getFilePointer() - LocalLogFileHeader.SYNC_BYTES_SIZE); // position
                                                                                   // before
                                                                                   // sync
            return true;
        }
        _syncByteBuffer[i % syncLen] = file.readByte();
    }
    return false;
}