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:edu.umass.cs.gigapaxos.SQLPaxosLogger.java

private static void compactLogfile(File file, PaxosPacketizer packetizer, MessageLogDiskMap msgLog,
        FileIDMap fidMap) throws IOException, JSONException {
    RandomAccessFile raf = null, rafTmp = null;
    File tmpFile = new File(file.toString() + TMP_FILE_SUFFIX);
    int tmpFileSize = 0;
    boolean compacted = false, neededAtAll = false;
    HashMap<String, ArrayList<LogIndexEntry>> logIndexEntries = new HashMap<String, ArrayList<LogIndexEntry>>();

    // quick delete
    if (fidMap.isRemovable(file.toString(), msgLog)) {
        deleteFile(file, msgLog);// w w w  .j  a  v a2  s . c  om
        log.log(Level.INFO, "{0} quick-garbage-collected file {1}", new Object[] { msgLog.disk, file });
        return;
    } else
        log.log(Level.FINE, "{0} not quick-GCing file {1} because dependent paxosIDs = {2}",
                new Object[] { msgLog.disk, file, fidMap.fidMap.get(file.toString()) });

    if (System.currentTimeMillis() - file.lastModified() < LOGFILE_AGE_THRESHOLD * 1000)
        return;

    try {
        long t = System.currentTimeMillis();
        raf = new RandomAccessFile(file.toString(), "r");
        rafTmp = new RandomAccessFile(tmpFile.toString(), "rw");
        while (raf.getFilePointer() < raf.length()) {
            long offset = rafTmp.getFilePointer();
            int length = raf.readInt();
            byte[] msg = new byte[length];
            raf.readFully(msg);
            PaxosPacket pp = packetizer != null ? packetizer.stringToPaxosPacket(msg
            // new String(msg, CHARSET)
            ) : PaxosPacket.getPaxosPacket(new String(msg, CHARSET));
            if (!logIndexEntries.containsKey(pp.getPaxosID()))
                logIndexEntries.put(pp.getPaxosID(), new ArrayList<LogIndexEntry>());
            logIndexEntries.get(pp.getPaxosID()).add(new LogIndexEntry(getSlot(pp), getBallot(pp).ballotNumber,
                    getBallot(pp).coordinatorID, pp.getType().getInt(), file.toString(), offset, length));

            if (isLogMsgNeeded(pp, msgLog)) {
                ByteBuffer bbuf = ByteBuffer.allocate(length + 4);
                bbuf.putInt(length);
                bbuf.put(msg);
                rafTmp.write(bbuf.array());
                neededAtAll = true;
                tmpFileSize += bbuf.capacity();
            } else {
                compacted = true;
                log.log(Level.FINE, "From logfile {0} garbage collecting {1}",
                        new Object[] { file, pp.getSummary() });
            }
        }
        DelayProfiler.updateDelay("compact", t);
    } finally {
        if (raf != null)
            raf.close();
        if (rafTmp != null) {
            rafTmp.getChannel().force(true);
            rafTmp.close();
        }
    }
    assert (tmpFile.exists() && tmpFile.length() == tmpFileSize) : tmpFile.length() + " != " + tmpFileSize;
    if (compacted && neededAtAll)
        synchronized (msgLog) {
            modifyLogfileAndLogIndex(file, tmpFile, logIndexEntries, msgLog, fidMap);
            log.log(Level.INFO, "{0} compacted logfile {1}", new Object[] { msgLog, file });
        }
    else if (!neededAtAll) {
        log.log(Level.INFO, "Deleting logfile {0} as its log entries are no longer needed",
                new Object[] { file });
        deleteFile(file, msgLog);
    } else
        // !compacted
        log.log(Level.INFO, "Logfile {0} unchanged upon compaction attempt", new Object[] { file });
    assert (tmpFile.toString().endsWith(TMP_FILE_SUFFIX));
    if (tmpFile.exists())
        deleteFile(tmpFile, msgLog);
}

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

/** queue uncrawled urls via the CrawlQueueLoader
 * /*from www.  ja v  a  2  s  .  c  om*/
 * @param loader
 */
public void queueUnCrawledItems(CrawlQueueLoader loader) throws IOException {
    _queueState = QueueState.QUEUEING;

    int metadataVersion = getMetadata().getVersion();

    synchronized (_metadata) {
        // reset metadata PERIOD  
        int urlCount = _metadata.getUrlCount();
        _metadata.clear();
        _metadata.setUrlCount(urlCount);
    }

    RandomAccessFile fixedDataReader = new RandomAccessFile(_fixedDataFile, "rw");
    RandomAccessFile stringDataReader = new RandomAccessFile(_variableDataFile, "rw");
    try {

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

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

            long position = fixedDataReader.getFilePointer();

            //LOG.info("*** TRYING READ LOCK FOR OFFSET:" + position);
            while (true) {
                // get read lock on position ... 
                try {
                    FileLock lock = fixedDataReader.getChannel().tryLock(position,
                            OnDiskCrawlHistoryItem.ON_DISK_SIZE, false);

                    try {
                        //LOG.info("*** GOT READ LOCK FOR OFFSET:" + position);
                        item.deserialize(fixedDataReader);
                        break;
                    } finally {
                        lock.release();
                        //LOG.info("*** RELEASED READ LOCK FOR OFFSET:" + position);
                    }
                } catch (OverlappingFileLockException e) {
                    LOG.error("*** LOCK CONTENTION AT:" + position + " Exception:"
                            + CCStringUtils.stringifyException(e));
                }
            }

            // 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);

            // first, if it has not been crawled ever, crawl it not matter what ... 
            boolean crawlItem = !item.isFlagSet(OnDiskCrawlHistoryItem.FLAG_HAS_CRAWL_STATUS);

            // if it has been crawled ... check list metadata version ... 
            if (!crawlItem && metadataVersion >= 1) {
                // ok this is newer version of the list ... 
                // check refresh time if specified ...
                int refreshIntervalInSeconds = DEFAULT_REFRESH_INTERVAL_IN_SECS;

                if (getMetadata().getRefreshInterval() != 0) {
                    refreshIntervalInSeconds = getMetadata().getRefreshInterval();
                }

                if (item._updateTimestamp > 0) {
                    long timeSinceLastCrawl = item._updateTimestamp;
                    if (System.currentTimeMillis() - timeSinceLastCrawl >= (refreshIntervalInSeconds * 1000)) {
                        crawlItem = true;
                    }
                }
            }

            if (crawlItem) {

                loader.queueURL(fingerprint, url);

                synchronized (_metadata) {
                    // update queued item count 
                    _metadata.setQueuedItemCount(_metadata.getQueuedItemCount() + 1);
                }
            } else {
                updateMetadata(item, _metadata, 0);
            }
            // ok update subdomain stats 
            updateSubDomainMetadataForItemDuringLoad(item, url, fingerprint, crawlItem);
        }

        flushCachedSubDomainMetadata();

        loader.flush();

        _queueState = QueueState.QUEUED;
    } catch (IOException e) {
        LOG.error("Encountered Exception Queueing Items for List:" + _listId + " Exception:"
                + CCStringUtils.stringifyException(e));
        _queueState = QueueState.ERROR;
    } finally {
        fixedDataReader.close();
        stringDataReader.close();
    }
}

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

/**
 * Increase sampleEveryNthPoint to return a smaller grid.
 *
 * Grid max and min values may be skipped.
 *
 * This does not used previously cached data.
 *
 * @param sampleEveryNthPoint//from w  w w  .  jav a  2 s  .  c o m
 * @return
 */
public float[] getGrid(int sampleEveryNthPoint) {
    int maxArrayLength = Integer.MAX_VALUE - 10;

    if (subgrids != null) {
        //sample points
        int size = 1000;
        double[][] points = new double[size * size][2];
        int pos = 0;
        for (int i = 0; i < 1000; i++) {
            for (int j = 0; j < 1000; j++) {
                points[pos][0] = xmin + (xmax - xmin) * j / (double) size;
                points[pos][1] = ymax - (ymax - ymin) * i / (double) size;
                pos++;
            }
        }

        return getValues3(points, 64);
    }

    int length = (nrows / sampleEveryNthPoint) * (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");
        }

        int sz = (int) Math.min(afile.length() / sampleEveryNthPoint / sampleEveryNthPoint, maxArrayLength);
        sz += 8 - sz % 8;
        byte[] b = new byte[sz];

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

        //replace not a number
        for (i = 0; i < length; i++) {
            if ((float) ret[(int) i] == (float) nodatavalue) {
                ret[(int) i] = Float.NaN;
            } else {
                ret[(int) 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:fr.bmartel.speedtest.SpeedTestTask.java

/**
 * Write upload POST request with file generated randomly.
 *//*from  ww w. j a v  a 2  s .c o m*/
public void writeUpload(final String hostname, final int port, final String uri, final int fileSizeOctet) {

    mSpeedTestMode = SpeedTestMode.UPLOAD;

    this.mHostname = hostname;
    this.mPort = port;
    mUploadFileSize = new BigDecimal(fileSizeOctet);
    mForceCloseSocket = false;
    mErrorDispatched = false;
    mUploadTempFileSize = 0;
    mTimeStart = System.currentTimeMillis();

    connectAndExecuteTask(new Runnable() {
        @Override
        public void run() {
            if (mSocket != null && !mSocket.isClosed()) {

                RandomAccessFile uploadFile = null;
                final RandomGen randomGen = new RandomGen();

                try {

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

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

                    final String head = "POST " + uri + " HTTP/1.1\r\n" + "Host: " + hostname + "\r\nAccept: "
                            + "*/*\r\nContent-Length: " + fileSizeOctet + "\r\n\r\n";

                    mUploadTempFileSize = 0;

                    final int uploadChunkSize = mSocketInterface.getUploadChunkSize();

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

                    if (mSocket.getOutputStream() != null) {

                        if (writeFlushSocket(head.getBytes()) != 0) {
                            throw new SocketTimeoutException();
                        }

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

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

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

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

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

                            if (writeFlushSocket(chunk) != 0) {
                                throw new SocketTimeoutException();
                            }

                            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);
                                }
                            }
                        }

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

                        if (remain != 0 && writeFlushSocket(chunk) != 0) {
                            throw new SocketTimeoutException();
                        } else {
                            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);

                            }
                        }
                    }
                } catch (SocketTimeoutException e) {
                    mReportInterval = false;
                    mErrorDispatched = true;
                    closeSocket();
                    closeExecutors();
                    if (!mForceCloseSocket) {
                        SpeedTestUtils.dispatchSocketTimeout(mForceCloseSocket, mListenerList, false,
                                SpeedTestConst.SOCKET_WRITE_ERROR);
                    } else {
                        SpeedTestUtils.dispatchError(mForceCloseSocket, mListenerList, false, e.getMessage());
                    }
                } catch (IOException e) {
                    mReportInterval = false;
                    mErrorDispatched = true;
                    closeExecutors();
                    SpeedTestUtils.dispatchError(mForceCloseSocket, mListenerList, false, e.getMessage());
                } finally {
                    if (uploadFile != null) {
                        try {
                            uploadFile.close();
                            randomGen.deleteFile();
                        } catch (IOException e) {
                            //e.printStackTrace();
                        }
                    }
                }
            }
        }
    }, false);
}

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

float[] getGrid(double xmin, double ymin, double xmax, double ymax) {
    //expects largest y at the top
    //expects input ranges inside of grid ranges

    int width = (int) ((xmax - xmin) / xres);
    int height = (int) ((ymax - ymin) / yres);
    int startx = (int) ((xmin - this.xmin) / xres);
    int endx = startx + width;
    int starty = (int) ((ymin - this.ymin) / yres);
    //int endy = starty + height;

    int length = width * height;

    float[] ret = new float[length];
    int pos = 0;//  ww  w . ja  v  a  2s.  c o m

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

    int size = 4;
    if (datatype.equals("BYTE") || datatype.equals("UBYTE")) {
        size = 1;
    } else if (datatype.equals("SHORT")) {
        size = 2;
    } else if (datatype.equals("INT")) {
        size = 4;
    } else if (datatype.equals("LONG")) {
        size = 8;
    } else if (datatype.equals("FLOAT")) {
        size = 4;
    } else if (datatype.equals("DOUBLE")) {
        size = 8;
    }

    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");
        }

        //seek to first raster
        afile.seek(((long) this.ncols) * starty * size);

        //read relevant rasters
        int readSize = this.ncols * height * size;
        int readLen = this.ncols * height;
        byte[] b = new byte[readSize];
        afile.read(b);
        ByteBuffer bb = ByteBuffer.wrap(b);

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

        if (datatype.equalsIgnoreCase("BYTE")) {
            for (i = 0; i < readLen; i++) {
                int x = i % this.ncols;
                if (x < startx || x >= endx) {
                    bb.get();
                } else {
                    ret[pos++] = bb.get();
                }
            }
        } else if (datatype.equalsIgnoreCase("UBYTE")) {
            for (i = 0; i < readLen; i++) {
                int x = i % this.ncols;
                if (x < startx || x >= endx) {
                    bb.get();
                } else {
                    ret[pos] = bb.get();
                    if (ret[pos] < 0) {
                        ret[pos] += 256;
                    }
                    pos++;
                }
            }
        } else if (datatype.equalsIgnoreCase("SHORT")) {
            for (i = 0; i < readLen; i++) {
                int x = i % this.ncols;
                if (x < startx || x >= endx) {
                    bb.getShort();
                } else {
                    ret[pos++] = bb.getShort();
                }
            }
        } else if (datatype.equalsIgnoreCase("INT")) {
            for (i = 0; i < readLen; i++) {
                int x = i % this.ncols;
                if (x < startx || x >= endx) {
                    bb.getInt();
                } else {
                    ret[pos++] = bb.getInt();
                }
            }
        } else if (datatype.equalsIgnoreCase("LONG")) {
            for (i = 0; i < readLen; i++) {
                int x = i % this.ncols;
                if (x < startx || x >= endx) {
                    bb.getLong();
                } else {
                    ret[pos++] = bb.getLong();
                }
            }
        } else if (datatype.equalsIgnoreCase("FLOAT")) {
            for (i = 0; i < readLen; i++) {
                int x = i % this.ncols;
                if (x < startx || x >= endx) {
                    bb.getFloat();
                } else {
                    ret[pos++] = bb.getFloat();
                }
            }
        } else if (datatype.equalsIgnoreCase("DOUBLE")) {
            for (i = 0; i < readLen; i++) {
                int x = i % this.ncols;
                if (x < startx || x >= endx) {
                    bb.getDouble();
                } else {
                    ret[pos++] = (float) bb.getDouble();
                }
            }
        } else {
            // / should not happen; catch anyway...
            for (i = 0; i < length; 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("GRID: " + e.toString(), e);
    } finally {
        if (afile != null) {
            try {
                afile.close();
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
        }
    }
    grid_data = ret;
    return ret;
}

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

/**
 * put Method.<br>//from w  w  w  .  j a v  a  2  s  . com
 * 
 * @param key
 * @param value
 * @param hashCode This is a key value hash code
 */
public void put(String key, String value, int hashCode) {
    /*
    long start1 = 0L;
    long start2 = 0L;
    long start3 = 0L;
    long start4 = 0L;
            
    long end1 = 0L;
    long end2 = 0L;
    long end3 = 0L;
    long end4 = 0L;
    */
    try {

        //start1 = System.nanoTime();
        //start2 = System.nanoTime();
        File file = dataFileList[hashCode % numberOfDataFiles];

        StringBuilder buf = new StringBuilder(this.keyDataLength);

        boolean callMapSizeCalc = true;
        if (key != null && key.equals(FileBaseDataMap.sizeSaveKey))
            callMapSizeCalc = false;

        //TODO:???
        buf.append(this.fillCharacter(key, keyDataLength));
        //buf.append(this.fillCharacter(value, oneDataLength));

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

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

            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;
            wr = accessor.wr;
        }
        //end2 = System.nanoTime();
        //start3 = System.nanoTime();
        // KeyData Write File
        for (int tryIdx = 0; tryIdx < 2; tryIdx++) {
            try {

                // Key??

                long[] dataLineNoRet = this.getLinePoint(key, raf);
                //end3 = System.nanoTime();
                //start4 = System.nanoTime();
                if (dataLineNoRet[0] == -1) {

                    wr.write(buf.toString());
                    SystemUtil.diskAccessSync(wr);
                    wr.write(value);
                    SystemUtil.diskAccessSync(wr);

                    // ????????????
                    int valueSize = value.length();
                    byte[] fillByte = new byte[1];
                    fillByte[0] = new Integer(FileBaseDataMap.paddingSymbol).byteValue();
                    int paddingSize = (oneDataLength - valueSize);

                    int writeSetCount = paddingSize / 512;
                    int singleWriteCount = paddingSize % 512;

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

                        wr.write(FileBaseDataMap.paddingSymbolSetString);
                        if ((i % 14) == 0)
                            SystemUtil.diskAccessSync(wr);
                    }
                    SystemUtil.diskAccessSync(wr);

                    byte[] fillBytes = new byte[singleWriteCount];
                    for (int i = 0; i < singleWriteCount; i++) {
                        fillBytes[i] = fillByte[0];
                    }

                    wr.write(new String(fillBytes));
                    SystemUtil.diskAccessSync(wr);

                    // The size of an increment
                    if (callMapSizeCalc)
                        this.getAndIncrement();
                } else {

                    // ?????1
                    boolean increMentFlg = false;
                    if (dataLineNoRet[1] == -1)
                        increMentFlg = true;
                    //if (this.get(key, hashCode) == null) increMentFlg = true;

                    raf.seek(dataLineNoRet[0] * (lineDataSize));
                    raf.write(buf.toString().getBytes(), 0, keyDataLength);
                    raf.write(value.getBytes());

                    // ????????????
                    int valueSize = value.length();
                    byte[] fillByte = new byte[1];
                    fillByte[0] = new Integer(FileBaseDataMap.paddingSymbol).byteValue();

                    int paddingSize = (oneDataLength - valueSize);

                    int writeSetCount = paddingSize / (4096);
                    int singleWriteCount = paddingSize % (4096);

                    for (int i = 0; i < writeSetCount; i++) {
                        raf.write(FileBaseDataMap.fillStream.toByteArray());
                    }

                    byte[] remainderPaddingBytes = new byte[singleWriteCount];

                    for (int i = 0; i < singleWriteCount; i++) {
                        remainderPaddingBytes[i] = fillByte[0];
                    }
                    if (remainderPaddingBytes.length > 0)
                        raf.write(remainderPaddingBytes);

                    if (callMapSizeCalc) {
                        if (increMentFlg)
                            this.getAndIncrement();
                    }
                }
                //end4 = System.nanoTime();
                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;
                }
            }
        }
    } catch (Exception e2) {
        e2.printStackTrace();
    }
    //end1 = System.nanoTime();
    //if (ImdstDefine.fileBaseMapTimeDebug) {
    //  System.out.println("1="+(end1 - start1) + " 2="+(end2 - start2) + " 3="+(end3 - start3) + " 4="+(end4 - start4));
    //}

}

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

/**
 * Start FTP upload.// w w  w  .ja v a  2s  . c o 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:au.org.ala.layers.intersect.Grid.java

/**
 * @return calculated min and max values of a grid file as float [] where [0] is min and [1] is max.
 *///from   ww  w .  ja v  a 2s  .com
public float[] calculatetMinMax() {

    float[] ret = new float[2];
    ret[0] = Float.MAX_VALUE;
    ret[1] = Float.MAX_VALUE * -1;

    long i;
    int size;
    byte[] b;
    RandomAccessFile afile = null;

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

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

        if (datatype.equalsIgnoreCase("BYTE")) {
            size = 1;
            b = new byte[size];
            for (i = 0; i < length; i++) {
                f = afile.readByte();
                if (f != (float) nodatavalue) {
                    ret[0] = Math.min(f * rescale, ret[0]);
                    ret[1] = Math.max(f * rescale, ret[1]);
                }
            }
        } else if (datatype.equalsIgnoreCase("UBYTE")) {
            size = 1;
            b = new byte[size];
            for (i = 0; i < length; i++) {
                f = afile.readByte();
                if (f < 0) {
                    f += 256;
                }
                if (f != (float) nodatavalue) {
                    ret[0] = Math.min(f * rescale, ret[0]);
                    ret[1] = Math.max(f * rescale, ret[1]);
                }
            }
        } else if (datatype.equalsIgnoreCase("SHORT")) {
            size = 2;
            b = new byte[size];
            for (i = 0; i < length; i++) {
                afile.read(b);
                if (byteorderLSB) {
                    f = (short) (((0xFF & b[1]) << 8) | (b[0] & 0xFF));
                } else {
                    f = (short) (((0xFF & b[0]) << 8) | (b[1] & 0xFF));
                }
                if (f != (float) nodatavalue) {
                    ret[0] = Math.min(f * rescale, ret[0]);
                    ret[1] = Math.max(f * rescale, ret[1]);
                }
            }
        } else if (datatype.equalsIgnoreCase("INT")) {
            size = 4;
            b = new byte[size];
            for (i = 0; i < length; i++) {
                afile.read(b);
                if (byteorderLSB) {
                    f = ((0xFF & b[3]) << 24) | ((0xFF & b[2]) << 16) + ((0xFF & b[1]) << 8) + (b[0] & 0xFF);
                } else {
                    f = ((0xFF & b[0]) << 24)
                            | ((0xFF & b[1]) << 16) + ((0xFF & b[2]) << 8) + ((0xFF & b[3]) & 0xFF);
                }
                if (f != (float) nodatavalue) {
                    ret[0] = Math.min(f * rescale, ret[0]);
                    ret[1] = Math.max(f * rescale, ret[1]);
                }
            }
        } else if (datatype.equalsIgnoreCase("LONG")) {
            size = 8;
            b = new byte[size];
            for (i = 0; i < length; i++) {
                afile.read(b);
                if (byteorderLSB) {
                    f = ((long) (0xFF & b[7]) << 56) + ((long) (0xFF & b[6]) << 48)
                            + ((long) (0xFF & b[5]) << 40) + ((long) (0xFF & b[4]) << 32)
                            + ((long) (0xFF & b[3]) << 24) + ((long) (0xFF & b[2]) << 16)
                            + ((long) (0xFF & b[1]) << 8) + (0xFF & b[0]);
                } else {
                    f = ((long) (0xFF & b[0]) << 56) + ((long) (0xFF & b[1]) << 48)
                            + ((long) (0xFF & b[2]) << 40) + ((long) (0xFF & b[3]) << 32)
                            + ((long) (0xFF & b[4]) << 24) + ((long) (0xFF & b[5]) << 16)
                            + ((long) (0xFF & b[6]) << 8) + (0xFF & b[7]);
                }
                if (f != (float) nodatavalue) {
                    ret[0] = Math.min(f * rescale, ret[0]);
                    ret[1] = Math.max(f * rescale, ret[1]);
                }
            }
        } else if (datatype.equalsIgnoreCase("FLOAT")) {
            size = 4;
            b = new byte[size];
            for (i = 0; i < length; i++) {
                afile.read(b);
                ByteBuffer bb = ByteBuffer.wrap(b);
                if (byteorderLSB) {
                    bb.order(ByteOrder.LITTLE_ENDIAN);
                }
                f = bb.getFloat();
                if (f != (float) nodatavalue) {
                    ret[0] = Math.min(f * rescale, ret[0]);
                    ret[1] = Math.max(f * rescale, ret[1]);
                }
            }
        } else if (datatype.equalsIgnoreCase("DOUBLE")) {
            size = 8;
            b = new byte[8];
            for (i = 0; i < length; i++) {
                afile.read(b);
                ByteBuffer bb = ByteBuffer.wrap(b);
                if (byteorderLSB) {
                    bb.order(ByteOrder.LITTLE_ENDIAN);
                }
                f = (float) bb.getDouble();
                if (f != (float) nodatavalue) {
                    ret[0] = Math.min(f * rescale, ret[0]);
                    ret[1] = Math.max(f * rescale, ret[1]);
                }
            }
        } else {
            logger.error("datatype not supported in Grid.getValues: " + datatype);
        }
    } catch (Exception e) {
        logger.error("error calculating min/max of a grid file", e);
    } finally {
        if (afile != null) {
            try {
                afile.close();
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
        }
    }
    return ret;
}

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

/**
 * @param points input array for longitude and latitude
 *               double[number_of_points][2]
 * @return array of .gri file values corresponding to the
 * points provided/*from w w w .  ja  v a 2  s  .co  m*/
 */
public float[] getValues(double[][] points) {

    //confirm inputs since they come from somewhere else
    if (points == null || points.length == 0) {
        return null;
    }

    //use preloaded grid data if available
    Grid g = Grid.getLoadedGrid(filename);
    if (g != null) {
        return g.getValues2(points);
    }

    if (subgrids != null) {
        return getValues3(points, Math.min(1024 * 1024, 64 * points.length));
    }

    float[] ret = new float[points.length];

    int length = points.length;
    long size;
    int i, pos;
    byte[] b;
    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");
        }

        if (datatype.equalsIgnoreCase("BYTE")) {
            size = 1;
            b = new byte[(int) size];
            for (i = 0; i < length; i++) {
                pos = (int) getcellnumber(points[i][0], points[i][1]);
                if (pos >= 0) {
                    afile.seek(pos * size);
                    ret[i] = afile.readByte();
                } else {
                    ret[i] = Float.NaN;
                }
            }
        } else if (datatype.equalsIgnoreCase("UBYTE")) {
            size = 1;
            b = new byte[(int) size];
            for (i = 0; i < length; i++) {
                pos = (int) getcellnumber(points[i][0], points[i][1]);
                if (pos >= 0) {
                    afile.seek(pos * size);
                    ret[i] = afile.readByte();
                    if (ret[i] < 0) {
                        ret[i] += 256;
                    }
                } else {
                    ret[i] = Float.NaN;
                }
            }
        } else if (datatype.equalsIgnoreCase("SHORT")) {
            size = 2;
            b = new byte[(int) size];
            for (i = 0; i < length; i++) {
                pos = (int) getcellnumber(points[i][0], points[i][1]);
                if (pos >= 0) {
                    afile.seek(pos * size);
                    afile.read(b);
                    if (byteorderLSB) {
                        ret[i] = (short) (((0xFF & b[1]) << 8) | (b[0] & 0xFF));
                    } else {
                        ret[i] = (short) (((0xFF & b[0]) << 8) | (b[1] & 0xFF));
                    }
                    //ret[i] = afile.readShort();
                } else {
                    ret[i] = Float.NaN;
                }
            }
        } else if (datatype.equalsIgnoreCase("INT")) {
            size = 4;
            b = new byte[(int) size];
            for (i = 0; i < length; i++) {
                pos = (int) getcellnumber(points[i][0], points[i][1]);
                if (pos >= 0) {
                    afile.seek(pos * size);
                    afile.read(b);
                    if (byteorderLSB) {
                        ret[i] = ((0xFF & b[3]) << 24)
                                | ((0xFF & b[2]) << 16) + ((0xFF & b[1]) << 8) + (b[0] & 0xFF);
                    } else {
                        ret[i] = ((0xFF & b[0]) << 24)
                                | ((0xFF & b[1]) << 16) + ((0xFF & b[2]) << 8) + ((0xFF & b[3]) & 0xFF);
                    }
                    //ret[i] = afile.readInt();
                } else {
                    ret[i] = Float.NaN;
                }
            }
        } else if (datatype.equalsIgnoreCase("LONG")) {
            size = 8;
            b = new byte[(int) size];
            for (i = 0; i < length; i++) {
                pos = (int) getcellnumber(points[i][0], points[i][1]);
                if (pos >= 0) {
                    afile.seek(pos * size);
                    afile.read(b);
                    if (byteorderLSB) {
                        ret[i] = ((long) (0xFF & b[7]) << 56) + ((long) (0xFF & b[6]) << 48)
                                + ((long) (0xFF & b[5]) << 40) + ((long) (0xFF & b[4]) << 32)
                                + ((long) (0xFF & b[3]) << 24) + ((long) (0xFF & b[2]) << 16)
                                + ((long) (0xFF & b[1]) << 8) + (0xFF & b[0]);
                    } else {
                        ret[i] = ((long) (0xFF & b[0]) << 56) + ((long) (0xFF & b[1]) << 48)
                                + ((long) (0xFF & b[2]) << 40) + ((long) (0xFF & b[3]) << 32)
                                + ((long) (0xFF & b[4]) << 24) + ((long) (0xFF & b[5]) << 16)
                                + ((long) (0xFF & b[6]) << 8) + (0xFF & b[7]);
                    }
                    //ret[i] = afile.readLong();
                } else {
                    ret[i] = Float.NaN;
                }
            }
        } else if (datatype.equalsIgnoreCase("FLOAT")) {
            size = 4;
            b = new byte[(int) size];
            for (i = 0; i < length; i++) {
                pos = (int) getcellnumber(points[i][0], points[i][1]);
                if (pos >= 0) {
                    afile.seek(pos * size);
                    afile.read(b);
                    ByteBuffer bb = ByteBuffer.wrap(b);
                    if (byteorderLSB) {
                        bb.order(ByteOrder.LITTLE_ENDIAN);
                    }
                    ret[i] = bb.getFloat();
                } else {
                    ret[i] = Float.NaN;
                }

            }
        } else if (datatype.equalsIgnoreCase("DOUBLE")) {
            size = 8;
            b = new byte[8];
            for (i = 0; i < length; i++) {
                pos = (int) getcellnumber(points[i][0], points[i][1]);
                if (pos >= 0) {
                    afile.seek(pos * size);
                    afile.read(b);
                    ByteBuffer bb = ByteBuffer.wrap(b);
                    if (byteorderLSB) {
                        bb.order(ByteOrder.LITTLE_ENDIAN);
                    }
                    ret[i] = (float) bb.getDouble();

                    //ret[i] = afile.readFloat();
                } else {
                    ret[i] = Float.NaN;
                }
            }
        } else {
            logger.error("datatype not supported in Grid.getValues: " + datatype);
            // / should not happen; catch anyway...
            for (i = 0; i < length; 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("error getting grid file values", e);
    } finally {
        if (afile != null) {
            try {
                afile.close();
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
        }
    }
    return ret;
}