Example usage for java.io RandomAccessFile close

List of usage examples for java.io RandomAccessFile close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes this random access file stream and releases any system resources associated with the stream.

Usage

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

/**
 * loadCache - load local cache from disk 
 * @param activeLogPath/* www.j  a  v  a  2 s. com*/
 * @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:okuyama.imdst.util.FileBaseDataMap.java

/**
 * ??value??.<br>//from ww w  . j a  v  a 2s. co  m
 *
 * @param key 
 * @param hashCode This is a key value hash code
 * @return 
 * @throws
 */
public String get(String key, int hashCode) {
    byte[] tmpBytes = null;

    String ret = null;
    byte[] keyBytes = key.getBytes();
    byte[] equalKeyBytes = new byte[keyBytes.length + 1];
    byte[] lineBufs = new byte[this.getDataSize];
    boolean matchFlg = true;

    // ???
    for (int idx = 0; idx < keyBytes.length; idx++) {
        equalKeyBytes[idx] = keyBytes[idx];
    }

    equalKeyBytes[equalKeyBytes.length - 1] = new Integer(FileBaseDataMap.paddingSymbol).byteValue();

    try {

        File file = dataFileList[hashCode % numberOfDataFiles];

        CacheContainer accessor = (CacheContainer) innerCache.get(file.getAbsolutePath());
        RandomAccessFile raf = null;
        BufferedWriter wr = null;

        if (accessor == null || accessor.isClosed) {

            raf = new RandomAccessFile(file, "rwd");
            wr = new BufferedWriter(new FileWriter(file, true));
            accessor = new CacheContainer();
            accessor.raf = raf;
            accessor.wr = wr;
            accessor.file = file;
            innerCache.put(file.getAbsolutePath(), accessor);
        } else {

            raf = accessor.raf;
        }

        for (int tryIdx = 0; tryIdx < 2; tryIdx++) {

            try {
                raf.seek(0);
                int readLen = -1;
                while ((readLen = SystemUtil.diskAccessSync(raf, lineBufs)) != -1) {

                    matchFlg = true;

                    int loop = readLen / lineDataSize;

                    for (int loopIdx = 0; loopIdx < loop; loopIdx++) {

                        int assist = (lineDataSize * loopIdx);

                        matchFlg = true;

                        if (equalKeyBytes[equalKeyBytes.length - 1] == lineBufs[assist
                                + (equalKeyBytes.length - 1)]) {

                            for (int i = 0; i < equalKeyBytes.length; i++) {

                                if (equalKeyBytes[i] != lineBufs[assist + i]) {
                                    matchFlg = false;
                                    break;
                                }
                            }
                        } else {

                            matchFlg = false;
                        }

                        // ???????
                        if (matchFlg) {

                            tmpBytes = new byte[lineDataSize];

                            for (int i = 0; i < lineDataSize; i++) {

                                tmpBytes[i] = lineBufs[assist + i];
                            }
                            break;
                        }
                    }
                    if (matchFlg)
                        break;
                }
                break;
            } catch (IOException ie) {

                // IOException???1????
                if (tryIdx == 1)
                    throw ie;

                try {
                    if (raf != null)
                        raf.close();
                    if (wr != null)
                        wr.close();

                    raf = new RandomAccessFile(file, "rwd");
                    wr = new BufferedWriter(new FileWriter(file, true));
                    accessor = new CacheContainer();
                    accessor.raf = raf;
                    accessor.wr = wr;
                    accessor.file = file;
                    innerCache.put(file.getAbsolutePath(), accessor);
                } catch (Exception e) {
                    throw e;
                }
            }
        }

        // ?
        if (tmpBytes != null) {

            if (tmpBytes[keyDataLength] != FileBaseDataMap.paddingSymbol) {

                int i = keyDataLength;
                int counter = 0;

                for (; i < tmpBytes.length; i++) {

                    if (tmpBytes[i] == FileBaseDataMap.paddingSymbol)
                        break;
                    counter++;
                }

                ret = new String(tmpBytes, keyDataLength, counter, "UTF-8");
            }
        }
    } catch (Exception e) {

        e.printStackTrace();
    }

    return ret;
}

From source file:okuyama.imdst.util.FileBaseDataMap.java

/**
 * ?????.<br>/*from  ww  w.  ja  v  a  2 s . c  o  m*/
 * ????null?.<br>
 *
 * @param
 * @return 
 * @throws
 */
public List getAllOneFileInKeys() {

    List keys = null;
    byte[] datas = null;
    StringBuilder keysBuf = null;
    RandomAccessFile raf = null;

    try {
        for (int retryCnt = 0; retryCnt < 2; retryCnt++) {

            if (this.nowIterationFileIndex < this.dataFileList.length) {
                int assistIdx = 0;
                for (assistIdx = this.nowIterationFileIndex; assistIdx < this.dataFileList.length; assistIdx++) {
                    if (this.dataFileList[assistIdx].length() > 1L)
                        break;
                }

                if (assistIdx < this.dataFileList.length)
                    this.nowIterationFileIndex = assistIdx;

                keys = new ArrayList();

                long oneFileLength = new Long(this.dataFileList[this.nowIterationFileIndex].length())
                        .longValue();

                long readSize = lineDataSize * 10;
                int readLoop = new Long(oneFileLength / readSize).intValue();
                if ((oneFileLength % readSize) > 0)
                    readLoop++;
                raf = new RandomAccessFile(this.dataFileList[this.nowIterationFileIndex], "rwd");
                raf.seek(0);

                for (int readLoopIdx = 0; readLoopIdx < readLoop; readLoopIdx++) {
                    datas = new byte[new Long(readSize).intValue()];

                    int readLen = -1;
                    readLen = SystemUtil.diskAccessSync(raf, datas);

                    if (readLen > 0) {

                        int loop = readLen / lineDataSize;

                        for (int loopIdx = 0; loopIdx < loop; loopIdx++) {

                            int assist = (lineDataSize * loopIdx);
                            keysBuf = new StringBuilder(ImdstDefine.stringBufferLarge_3Size);

                            int idx = 0;

                            while (true) {

                                if (datas[assist + idx] != FileBaseDataMap.paddingSymbol) {
                                    keysBuf.append(new String(datas, assist + idx, 1));
                                } else {
                                    break;
                                }
                                idx++;
                            }
                            String keyStr = keysBuf.toString();
                            if (!keyStr.equals(FileBaseDataMap.sizeSaveKey)) {
                                keys.add(keyStr);
                            }
                            keysBuf = null;
                        }
                    }
                }
            }
            this.nowIterationFileIndex++;
            if (keys != null && keys.size() == 0)
                continue;
            retryCnt = 2;
        }
    } catch (Exception e) {

        e.printStackTrace();
    } finally {

        try {
            if (raf != null)
                raf.close();

            raf = null;
            datas = null;
        } catch (Exception e2) {
            e2.printStackTrace();
        }
    }

    if (keys != null && keys.size() == 0)
        keys = null;
    return keys;
}

From source file:au.org.ala.layers.intersect.Grid.java

public float[] getGrid() {
    int maxArrayLength = Integer.MAX_VALUE - 10;

    if (grid_data != null) {
        return grid_data;
    }//from w  ww .jav  a  2  s.c o  m

    Grid loadedAlready = getLoadedGrid(filename);
    if (loadedAlready != null && loadedAlready.grid_data != null) {
        return loadedAlready.grid_data;
    }

    int length = nrows * ncols;

    float[] ret = new float[length];

    RandomAccessFile afile = null;
    File f2 = new File(filename + ".GRI");

    try { //read of random access file can throw an exception
        if (!f2.exists()) {
            afile = new RandomAccessFile(filename + ".gri", "r");
        } else {
            afile = new RandomAccessFile(filename + ".GRI", "r");
        }

        byte[] b = new byte[(int) Math.min(afile.length(), maxArrayLength)];

        int i = 0;
        int max = 0;
        int len;
        while ((len = afile.read(b)) > 0) {
            ByteBuffer bb = ByteBuffer.wrap(b);

            if (byteorderLSB) {
                bb.order(ByteOrder.LITTLE_ENDIAN);
            }

            if (datatype.equalsIgnoreCase("UBYTE")) {
                max += len;
                max = Math.min(max, ret.length);
                for (; i < max; i++) {
                    ret[i] = bb.get();
                    if (ret[i] < 0) {
                        ret[i] += 256;
                    }
                }
            } else if (datatype.equalsIgnoreCase("BYTE")) {
                max += len;
                max = Math.min(max, ret.length);
                for (; i < max; i++) {
                    ret[i] = bb.get();
                }
            } else if (datatype.equalsIgnoreCase("SHORT")) {
                max += len / 2;
                max = Math.min(max, ret.length);
                for (; i < max; i++) {
                    ret[i] = bb.getShort();
                }
            } else if (datatype.equalsIgnoreCase("INT")) {
                max += len / 4;
                max = Math.min(max, ret.length);
                for (; i < max; i++) {
                    ret[i] = bb.getInt();
                }
            } else if (datatype.equalsIgnoreCase("LONG")) {
                max += len / 8;
                max = Math.min(max, ret.length);
                for (; i < max; i++) {
                    ret[i] = bb.getLong();
                }
            } else if (datatype.equalsIgnoreCase("FLOAT")) {
                max += len / 4;
                max = Math.min(max, ret.length);
                for (; i < max; i++) {
                    ret[i] = bb.getFloat();
                }
            } else if (datatype.equalsIgnoreCase("DOUBLE")) {
                max += len / 8;
                max = Math.min(max, ret.length);
                for (; i < max; i++) {
                    ret[i] = (float) bb.getDouble();
                }
            } else {
                // / should not happen; catch anyway...
                max += len / 4;
                for (; i < max; i++) {
                    ret[i] = Float.NaN;
                }
            }
        }

        //replace not a number
        for (i = 0; i < length; i++) {
            if ((float) ret[i] == (float) nodatavalue) {
                ret[i] = Float.NaN;
            } else {
                ret[i] *= rescale;
            }
        }
    } catch (Exception e) {
        logger.error("An error has occurred - probably a file error", e);
    } finally {
        if (afile != null) {
            try {
                afile.close();
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
        }
    }
    grid_data = ret;
    return ret;
}

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 {//from   w w w .  j  ava 2 s  .c o m
        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:com.aliyun.android.oss.task.GetObjectTask.java

/**
 * ???/*from  w  ww.j a  va 2  s.c om*/
 * @param {@link OSSObject}???
 * @return {@link OSSClientUtil}
 * @throws OSSException
 */
public int getResult(OSSObject object) throws OSSException {
    int result = CloudUtil.CLOUDCLIENT_RESULT_OK;

    HttpResponse response = null;
    RandomAccessFile randomAccessFile = null;
    InputStream inputStream = null;
    object = new OSSObject(this.bucketName, this.objectKey);
    try {
        mLogMission.i("getResult", "before network request", missionObject);
        response = this.execute();
        mLogMission.i("getResult", "network response", missionObject);
        Header rangeHeader = response.getFirstHeader("Content-Range");
        if (rangeHeader != null) {
            String range = rangeHeader.getValue();
            mLogMission.i("getResult", "range:" + range.toString(), missionObject);
        }
        object.setObjectMetaData(OSSHttpTool.getObjectMetadataFromResponse(response));
        if (response.getEntity() != null) {
            mLogMission.i("getResult",
                    "the content length get from server:" + response.getEntity().getContentLength(),
                    missionObject);
            if (missionObject.getFileLength() <= 0L) {
                missionObject.setFileLength(response.getEntity().getContentLength());
                mLogMission.i("getResult",
                        "get content length and set file length. fileLength:" + missionObject.getFileLength(),
                        missionObject);
            }
            missionObject.setPaused(false);
            inputStream = response.getEntity().getContent();
            if (listener != null) {
                listener.setTotalSize(missionObject.getFileLength());
            }
        }
        result = mDatabaseAccessManager.updateDownloadFile(missionObject);
        if (result != CloudUtil.CLOUDCLIENT_RESULT_OK) {
            return result;
        }

        randomAccessFile = new RandomAccessFile(filePath, "rwd");
        long offset = 0;
        if (range != null) {
            offset = range.getStart();
            mLogMission.i("getResult", "set offset before write file, offset:" + offset, missionObject);
        }
        randomAccessFile.seek(offset);
        mLogMission.i("getResult", "before write to local, file offset:" + offset, missionObject);
        byte[] buffer = new byte[1024 * 4];
        long readTotal = offset;
        int readLength = 0;
        while (true) {
            if (readTotal == missionObject.getFileLength()) {
                missionObject.setFinished(true);
                result = CloudUtil.CLOUDCLIENT_RESULT_OK;
                mLogMission.i("getResult", "readTotal == missionObject's fileLength, readTotal:" + readTotal,
                        missionObject);
                break;
            }
            if (listener != null && listener.isCancel()) {
                missionObject.setPaused(true);
                result = CloudUtil.CLOUD_FILE_MISSION_CANCEL;
                mLogMission.i("getResult", "cancel download missionObject", missionObject);
                break;
            }
            readLength = inputStream.read(buffer);
            mLogMission.i("getResult", "read buffer length:" + readLength, missionObject);
            if (readLength == -1) {
                missionObject.setFinished(true);
                result = CloudUtil.CLOUDCLIENT_RESULT_OK;
                mLogMission.i("getResult", "buffer read finish, readLength:" + readLength, missionObject);
                break;
            }

            mLogMission.i("getResult", "write to local, length:" + readLength, missionObject);
            randomAccessFile.write(buffer, 0, readLength);
            readTotal += readLength;
            mLogMission.i("getResult", "readTotal:" + readTotal, missionObject);
            missionObject.setTransferredLength(readTotal);
            missionObject.setLastTime(System.currentTimeMillis());
            //                result = mDatabaseAccessManager.updateDownloadFile(missionObject);
            //                if (result != CloudUtil.CLOUDCLIENT_RESULT_OK) {
            //                    break;
            //                }
            if (listener != null) {
                listener.transferred(readTotal);
            }
        }
    } catch (OSSException osse) {
        throw osse;
    } catch (ParseException pe) {
        OSSException ossException = new OSSException(pe);
        ossException.setErrorCode(OSSErrorCode.PARSE_METADATA_ERROR);
        throw ossException;
    } catch (IOException ioe) {
        OSSException ossException = new OSSException(ioe);
        ossException.setErrorCode(OSSErrorCode.GET_ENTITY_CONTENT_ERROR);
        throw ossException;
    } finally {
        mDatabaseAccessManager.updateDownloadFile(missionObject);
        mLogMission.i("getResult", "finally, update db", missionObject);
        this.releaseHttpClient();
        if (randomAccessFile != null) {
            try {
                inputStream.close();
                randomAccessFile.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return result;
}

From source file:au.org.ala.layers.intersect.Grid.java

public void mergeMissingValues(Grid sourceOfMissingValues, boolean hideMissing) {
    float[] cells = sourceOfMissingValues.getGrid();

    float[] actual = getGrid();

    int length = actual.length;

    int i;//from  w  ww.  java  2  s .  c o  m
    RandomAccessFile afile = null;
    File f2 = new File(filename + ".GRI");

    try { //read of random access file can throw an exception
        if (!f2.exists()) {
            afile = new RandomAccessFile(filename + ".gri", "rw");
        } else {
            afile = new RandomAccessFile(filename + ".GRI", "rw");
        }

        byte[] b = new byte[(int) afile.length()];

        ByteBuffer bb = ByteBuffer.wrap(b);

        if (byteorderLSB) {
            bb.order(ByteOrder.LITTLE_ENDIAN);
        }

        afile.seek(0);

        if (datatype.equalsIgnoreCase("UBYTE")) {
            for (i = 0; i < length; i++) {
                if (hideMissing == Float.isNaN(cells[i])) {
                    if (nodatavalue >= 128) {
                        bb.put((byte) (nodatavalue - 256));
                    } else {
                        bb.put((byte) nodatavalue);
                    }
                } else {
                    if (actual[i] >= 128) {
                        bb.put((byte) (actual[i] - 256));
                    } else {
                        bb.put((byte) actual[i]);
                    }
                }
            }
        } else if (datatype.equalsIgnoreCase("BYTE")) {
            for (i = 0; i < length; i++) {
                bb.put((byte) actual[i]);
            }
        } else if (datatype.equalsIgnoreCase("SHORT")) {
            for (i = 0; i < length; i++) {
                if (hideMissing == Float.isNaN(cells[i])) {
                    bb.putShort((short) nodatavalue);
                } else {
                    bb.putShort((short) actual[i]);
                }
            }
        } else if (datatype.equalsIgnoreCase("INT")) {
            for (i = 0; i < length; i++) {
                if (hideMissing == Float.isNaN(cells[i])) {
                    bb.putInt((int) nodatavalue);
                } else {
                    bb.putInt((int) actual[i]);
                }
            }
        } else if (datatype.equalsIgnoreCase("LONG")) {
            for (i = 0; i < length; i++) {
                if (hideMissing == Float.isNaN(cells[i])) {
                    bb.putLong((long) nodatavalue);
                } else {
                    bb.putLong((long) actual[i]);
                }
            }
        } else if (datatype.equalsIgnoreCase("FLOAT")) {
            for (i = 0; i < length; i++) {
                if (hideMissing == Float.isNaN(cells[i])) {
                    bb.putFloat((float) nodatavalue);
                } else {
                    bb.putFloat(actual[i]);
                }
            }
        } else if (datatype.equalsIgnoreCase("DOUBLE")) {
            for (i = 0; i < length; i++) {
                if (hideMissing == Float.isNaN(cells[i])) {
                    bb.putDouble((double) nodatavalue);
                } else {
                    bb.putDouble((double) actual[i]);
                }
            }
        } else {
            // should not happen
            logger.error("unsupported grid data type: " + datatype);
        }

        afile.write(bb.array());
    } catch (Exception e) {
        logger.error("error getting grid file values", e);
    } finally {
        if (afile != null) {
            try {
                afile.close();
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
        }
    }
}

From source file:au.org.ala.layers.intersect.Grid.java

public void getClassInfo(Map<Float, float[]> info) {

    long length = ((long) nrows) * ((long) ncols);

    RandomAccessFile afile = null;
    File f2 = new File(filename + ".GRI");

    try { //read of random access file can throw an exception
        if (!f2.exists()) {
            afile = new RandomAccessFile(filename + ".gri", "r");
        } else {/*ww w .j  a v a2  s. c  o m*/
            afile = new RandomAccessFile(filename + ".GRI", "r");
        }

        byte[] b = new byte[65536];

        long i = 0;
        long max = 0;
        long len;
        float v;
        float ndv = (float) nodatavalue;

        while ((len = afile.read(b)) > 0) {
            ByteBuffer bb = ByteBuffer.wrap(b);

            if (byteorderLSB) {
                bb.order(ByteOrder.LITTLE_ENDIAN);
            }

            if (datatype.equalsIgnoreCase("UBYTE")) {
                max += len;
                max = Math.min(max, length);
                for (; i < max; i++) {
                    v = bb.get();
                    if (v < 0)
                        v += 256;
                    if (v != ndv)
                        updatesStats(info, i, v * rescale);
                }
            } else if (datatype.equalsIgnoreCase("BYTE")) {
                max += len;
                max = Math.min(max, length);
                for (; i < max; i++) {
                    v = bb.get();
                    if (v != ndv)
                        updatesStats(info, i, v * rescale);
                }
            } else if (datatype.equalsIgnoreCase("SHORT")) {
                max += len / 2;
                max = Math.min(max, length);
                for (; i < max; i++) {
                    v = bb.getShort();
                    if (v != ndv)
                        updatesStats(info, i, v * rescale);
                }
            } else if (datatype.equalsIgnoreCase("INT")) {
                max += len / 4;
                max = Math.min(max, length);
                for (; i < max; i++) {
                    v = bb.getInt();
                    if (v != ndv)
                        updatesStats(info, i, v * rescale);
                }
            } else if (datatype.equalsIgnoreCase("LONG")) {
                max += len / 8;
                max = Math.min(max, length);
                for (; i < max; i++) {
                    v = bb.getLong();
                    if (v != ndv)
                        updatesStats(info, i, v * rescale);
                }
            } else if (datatype.equalsIgnoreCase("FLOAT")) {
                max += len / 4;
                max = Math.min(max, length);
                for (; i < max; i++) {
                    v = bb.getFloat();
                    if (v != ndv)
                        updatesStats(info, i, v * rescale);
                }
            } else if (datatype.equalsIgnoreCase("DOUBLE")) {
                max += len / 8;
                max = Math.min(max, length);
                for (; i < max; i++) {
                    v = (float) bb.getDouble();
                    if (v != ndv)
                        updatesStats(info, i, v * rescale);
                }
            } else {
                max += len / 4;
                for (; i < max; i++) {
                    // should not happen; catch anyway...
                }
            }
        }
    } catch (Exception e) {
        logger.error("An error has occurred getting grid class stats", e);
    } finally {
        if (afile != null) {
            try {
                afile.close();
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
        }
    }

}

From source file:edu.umass.cs.gigapaxos.SQLPaxosLogger.java

private byte[] getJournaledMessage(String logfile, long offset, int length, RandomAccessFile raf)
        throws IOException {
    assert (logfile != null);
    if (!new File(logfile).exists())
        return null;
    boolean locallyOpened = false;
    if (raf == null) {
        locallyOpened = true;/*  w w  w. j  a  v a 2  s .  co m*/
        raf = new RandomAccessFile(logfile, "r");
    }
    boolean error = false;
    String msg = null;
    byte[] buf = null;
    try {
        raf.seek(offset);
        assert (raf.length() > offset) : this + " " + raf.length() + " <= " + offset + " while reading logfile "
                + logfile;
        int readLength = raf.readInt();
        try {
            assert (readLength == length) : this + " : " + readLength + " != " + length;
        } catch (Error e) {
            error = true;
            log.severe(this + ": " + e);
            e.printStackTrace();
        }
        int bufLength = length;
        buf = new byte[bufLength];
        raf.readFully(buf);
        if (JOURNAL_COMPRESSION)
            buf = inflate(buf);
        msg = new String(buf, CHARSET);
    } catch (IOException | Error e) {
        log.log(Level.INFO, "{0} incurred IOException while retrieving journaled message {1}:{2}",
                new Object[] { this, logfile, offset + ":" + length });
        e.printStackTrace();
        if (locallyOpened)
            raf.close();
        throw e;
    }
    log.log(error ? Level.INFO : Level.FINEST, "{0} returning journaled message from {1}:{2} = [{3}]",
            new Object[] { this, logfile, offset + ":" + length, msg });
    return buf;// msg;
}

From source file:au.org.ala.layers.intersect.Grid.java

public void replaceValues(Map<Integer, Integer> translation) {

    long length = ((long) nrows) * ((long) ncols);

    Integer minv = null;//from ww  w  . j a v  a  2  s. com
    Integer maxv = null;
    for (Integer i : translation.values()) {
        if (minv == null || i < minv)
            minv = i;
        if (maxv == null || i > maxv)
            maxv = i;
    }

    RandomAccessFile afile = null;
    RandomAccessFile out = null;
    File f2 = new File(filename + ".GRI");
    File newGrid = new File(filename + ".gri.new");

    try { //read of random access file can throw an exception
        out = new RandomAccessFile(newGrid, "rw");

        if (!f2.exists()) {
            afile = new RandomAccessFile(filename + ".gri", "r");
        } else {
            afile = new RandomAccessFile(filename + ".GRI", "r");
        }

        byte[] b = new byte[65536];
        byte[] bout = new byte[65536];

        long i = 0;
        long max = 0;
        long len;
        float v;
        float ndv = (float) nodatavalue;

        while ((len = afile.read(b)) > 0) {
            ByteBuffer bb = ByteBuffer.wrap(b);
            ByteBuffer bbout = ByteBuffer.wrap(bout);

            if (byteorderLSB) {
                bb.order(ByteOrder.LITTLE_ENDIAN);
                bbout.order(ByteOrder.LITTLE_ENDIAN);
            }

            if (datatype.equalsIgnoreCase("UBYTE")) {
                throw new Exception("UBYTE translation not supported");
            } else if (datatype.equalsIgnoreCase("BYTE")) {
                throw new Exception("BYTE translation not supported");
            } else if (datatype.equalsIgnoreCase("SHORT")) {
                max += len / 2;
                max = Math.min(max, length);
                for (; i < max; i++) {
                    v = bb.getShort();
                    if (v != ndv && translation.get((int) (v * rescale)) == null) {
                        v = v;
                    }
                    if (v != ndv && translation.get((int) (v * rescale)) != null)
                        v = translation.get((int) (v * rescale));
                    bbout.putShort((short) v);
                }
            } else if (datatype.equalsIgnoreCase("INT")) {
                max += len / 4;
                max = Math.min(max, length);
                for (; i < max; i++) {
                    v = bb.getInt();
                    if (v != ndv && translation.get((int) (v * rescale)) != null)
                        v = translation.get((int) (v * rescale));
                    bbout.putInt((int) v);
                }
            } else if (datatype.equalsIgnoreCase("LONG")) {
                max += len / 8;
                max = Math.min(max, length);
                for (; i < max; i++) {
                    v = bb.getLong();
                    if (v != ndv && translation.get((int) (v * rescale)) != null)
                        v = translation.get((int) (v * rescale));
                    bbout.putLong((long) v);
                }
            } else if (datatype.equalsIgnoreCase("FLOAT")) {
                throw new Exception("FLOAT translation not supported");
            } else if (datatype.equalsIgnoreCase("DOUBLE")) {
                throw new Exception("DOUBLE translation not supported");
            } else {
                max += len / 4;
                for (; i < max; i++) {
                    // should not happen; catch anyway...
                }
            }

            out.write(bout, 0, (int) len);
        }

        writeHeader(filename + ".new", xmin, ymin, xmin + xres * ncols, ymin + yres * nrows, xres, yres, nrows,
                ncols, minv, maxv, datatype, nodatavalue + "");
    } catch (Exception e) {
        logger.error("An error has occurred getting grid class stats", e);
    } finally {
        if (afile != null) {
            try {
                afile.close();
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
        }

        if (out != null) {
            try {
                out.close();
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
        }
    }

    try {
        if (!new File(filename + ".gri.old").exists())
            FileUtils.moveFile(new File(filename + ".gri"), new File(filename + ".gri.old"));
        if (!new File(filename + ".grd.old").exists())
            FileUtils.moveFile(new File(filename + ".grd"), new File(filename + ".grd.old"));

        FileUtils.moveFile(new File(filename + ".gri.new"), new File(filename + ".gri"));
        FileUtils.moveFile(new File(filename + ".new.grd"), new File(filename + ".grd"));
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
}