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:org.commoncrawl.service.listcrawler.CrawlList.java

public CrawlListMetadata getSubDomainMetadataByRootDomain(String rootDomainName) throws IOException {
    long domainHash = URLFingerprint.generate64BitURLFPrint(rootDomainName);

    CrawlListMetadata metadata = new CrawlListMetadata();
    synchronized (_subDomainMetadataFile) {
        RandomAccessFile file = new RandomAccessFile(_subDomainMetadataFile, "rw");
        try {//w w  w .ja va 2  s.c om
            int dataOffset = getOffsetForSubDomainData(domainHash);
            if (dataOffset == 0) {
                throw new IOException("Data Offset Zero for host:" + rootDomainName);
            }
            file.seek(dataOffset);
            metadata.readFields(file);
            // set the data offset on the way out so that updates write to the proper location 
            metadata.setSubDomainDataOffset(dataOffset);
        } finally {
            file.close();
        }
    }
    return metadata;
}

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

void updateSubDomainQueueStatus(String rootDomainName, int deltaQueuedCount) throws IOException {
    long domainHash = URLFingerprint.generate64BitURLFPrint(rootDomainName);
    synchronized (_subDomainMetadataFile) {
        CrawlListMetadata metadata = new CrawlListMetadata();

        RandomAccessFile file = new RandomAccessFile(_subDomainMetadataFile, "rw");
        try {//  w  ww. j  a v a2s  .c o  m
            int dataOffset = getOffsetForSubDomainData(domainHash);
            if (dataOffset == 0) {
                throw new IOException("Data Offset Zero for host:" + rootDomainName);
            }
            file.seek(dataOffset);
            metadata.readFields(file);
            // set the data offset on the way out so that updates write to the proper location 
            metadata.setQueuedItemCount(metadata.getQueuedItemCount() + deltaQueuedCount);
            // ok reseek to data offset 
            file.seek(dataOffset);
            // rewrite the data structure
            metadata.write(file);
        } finally {
            file.close();
        }
    }
}

From source file:org.opencb.cellbase.mongodb.db.VariantAnnotationMongoDBAdaptorTest.java

private int getVepAnnotationBatch(RandomAccessFile raf, int nVariantsToRead,
        Set<AnnotationComparisonObject> vepAnnotationSet) throws IOException {
    /**//from   w ww. jav a 2 s. c o m
     * Loads VEP annotation
     */
    String newLine;
    int nNonRegulatoryAnnotations = 0;
    int nReadVariants = 0;
    String previousChr = "";
    String previousPosition = "";
    String previousAlt = "";
    String alt;
    long filePointer = 0;

    if (nVariantsToRead > 0) {
        while (((newLine = raf.readLine()) != null) && nReadVariants <= nVariantsToRead) {
            String[] lineFields = newLine.split("\t");
            String[] coordinatesParts = lineFields[1].split(":");
            if (lineFields[2].equals("deletion")) {
                alt = "-";
            } else {
                alt = lineFields[2];
            }
            if (!previousChr.equals(coordinatesParts[0]) || !previousPosition.equals(coordinatesParts[1])
                    || !previousAlt.equals(alt)) {
                nReadVariants++;
            }
            if (nReadVariants <= nVariantsToRead) {
                for (String SOname : lineFields[6].split(",")) {
                    if (SOname.equals("nc_transcript_variant")) {
                        SOname = "non_coding_transcript_variant";
                    }
                    if (!SOname.equals("regulatory_region_variant")) {
                        nNonRegulatoryAnnotations++;
                    }
                    vepAnnotationSet.add(new AnnotationComparisonObject(coordinatesParts[0],
                            coordinatesParts[1], alt, lineFields[3], lineFields[4], SOname));
                }
                previousChr = coordinatesParts[0];
                previousPosition = coordinatesParts[1];
                previousAlt = alt;
                filePointer = raf.getFilePointer();
            }
        }

        raf.seek(filePointer);
    }

    return nNonRegulatoryAnnotations;
}

From source file:com.aliyun.android.oss.task.GetObjectTask.java

/**
 * ???/*w  w  w .  j a v  a 2s.c o m*/
 * @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;/* w w  w .j a v a  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:org.commoncrawl.service.listcrawler.CacheManager.java

private final void flushLocalLog(final long bytesToRemove, final int itemsToRemove,
        final List<FingerprintAndOffsetTuple> flushedTupleList,
        final ArrayList<IndexDataFileTriple> tempFileTriples) {

    LOG.info("Acquiring Log Access Semaphores");
    // first boost this thread's priority ... 
    int originalThreadPriority = Thread.currentThread().getPriority();
    Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
    // next acquire all permits to the local access log ... block until we get there ... 
    getLocalLogAccessSemaphore().acquireUninterruptibly(LOG_ACCESS_SEMAPHORE_COUNT);
    // now that we have all the semaphores we need, reduce the thread's priority to normal
    Thread.currentThread().setPriority(originalThreadPriority);
    LOG.info("Acquired ALL Log Access Semaphores");

    long timeStart = System.currentTimeMillis();

    // now we have exclusive access to the local transaction log ... 
    File activeLogFilePath = getActiveLogFilePath();
    File checkpointLogFilePath = getCheckpointLogFilePath();
    try {/*from  ww  w.  j a  va2 s  .co m*/
        // delete checkpoint file if it existed ... 
        checkpointLogFilePath.delete();
        // now rename activelog to checkpoint path 
        activeLogFilePath.renameTo(checkpointLogFilePath);

        long logFileConsolidationStartTime = System.currentTimeMillis();
        // now trap for exceptions in case something fails 
        try {
            // fix up the header ... 
            _header._fileSize -= bytesToRemove;
            _header._itemCount -= itemsToRemove;

            // open a old file and new file 
            RandomAccessFile newFile = new RandomAccessFile(activeLogFilePath, "rw");
            RandomAccessFile oldFile = new RandomAccessFile(checkpointLogFilePath, "r");

            LOG.info("Opened new and old files. New Header FileSize is:" + _header._fileSize + " ItemCount:"
                    + _header._itemCount);
            try {
                // write out header ...
                long bytesRemainingInLogFile = _header._fileSize;

                LOG.info("Writing Header to New File. Bytes Remaining for Data are:" + bytesRemainingInLogFile);
                // write header to new file ... 
                _header.writeHeader(newFile);
                // decrement bytes available ... 
                bytesRemainingInLogFile -= LocalLogFileHeader.SIZE;

                if (bytesRemainingInLogFile != 0) {
                    byte transferBuffer[] = new byte[(1 << 20) * 16];
                    LOG.info("Seeking old file past flushed data (pos:" + LocalLogFileHeader.SIZE
                            + bytesToRemove + ")");
                    // seek past old data ... 
                    oldFile.seek(LocalLogFileHeader.SIZE + bytesToRemove);
                    // and copy across remaining data 
                    while (bytesRemainingInLogFile != 0) {
                        int bytesToReadWriteThisIteration = Math.min((int) bytesRemainingInLogFile,
                                transferBuffer.length);
                        oldFile.read(transferBuffer, 0, bytesToReadWriteThisIteration);
                        newFile.write(transferBuffer, 0, bytesToReadWriteThisIteration);
                        LOG.info("Copied " + bytesToReadWriteThisIteration + " from Old to New");
                        bytesRemainingInLogFile -= bytesToReadWriteThisIteration;
                    }
                }
            } finally {
                if (newFile != null) {
                    newFile.close();
                }
                if (oldFile != null) {
                    oldFile.close();
                }
            }
            // if we reached here then checkpoint was successfull ... 
            LOG.info("Checkpoint - Log Consolidation Successfull! TOOK:"
                    + (System.currentTimeMillis() - logFileConsolidationStartTime));

            LOG.info("Loading Index Files");
            for (IndexDataFileTriple triple : tempFileTriples) {
                LOG.info("Loading Index File:" + triple._localIndexFilePath);
                final HDFSFileIndex fileIndex = new HDFSFileIndex(_remoteFileSystem, triple._localIndexFilePath,
                        triple._dataFilePath);
                LOG.info("Loaded Index File");
                // update hdfs index list ... 
                synchronized (CacheManager.this) {
                    LOG.info("Adding HDFS Index to list");
                    _hdfsIndexList.addElement(fileIndex);
                }
            }

            // create a semaphore to wait on 
            final Semaphore semaphore = new Semaphore(0);

            LOG.info("Scheduling Async Event");
            // now we need to schedule an async call to main thread to update data structures safely ... 
            _eventLoop.setTimer(new Timer(0, false, new Timer.Callback() {

                @Override
                public void timerFired(Timer timer) {
                    LOG.info("Cleaning Map");

                    synchronized (CacheManager.this) {
                        // walk tuples 
                        for (FingerprintAndOffsetTuple tuple : flushedTupleList) {
                            //TODO: HACK!
                            // remove from collection ... 
                            _fingerprintToLocalLogPos.removeAll(tuple._fingerprint);
                        }
                    }
                    LOG.info("Increment Offset Info");
                    // finally increment locallog offset by bytes removed ... 
                    _localLogStartOffset += bytesToRemove;

                    LOG.info("Releasing Wait Semaphore");
                    //release wait sempahore 
                    semaphore.release();
                }
            }));

            LOG.info("Waiting for Async Event to Complete");
            //wait for async operation to complete ...
            semaphore.acquireUninterruptibly();

            LOG.info("Async Event to Completed");
        } catch (IOException e) {
            LOG.error("Checkpoint Failed with Exception:" + CCStringUtils.stringifyException(e));
            // delete new file ... 
            activeLogFilePath.delete();
            // and rename checkpoint file to active file ... 
            checkpointLogFilePath.renameTo(activeLogFilePath);
        }
    } finally {
        LOG.info("Releasing ALL Log Access Semaphores. HELD FOR:" + (System.currentTimeMillis() - timeStart));
        getLocalLogAccessSemaphore().release(LOG_ACCESS_SEMAPHORE_COUNT);
    }
}

From source file:org.apache.hadoop.hive.service.HSSessionItem.java

public boolean uploadProto(String user, String fileName, byte[] array) throws HiveServerException {
    boolean res = true;
    String fname;/* w  w  w  .  java2  s . com*/

    String dname = getHome() + "/protobuf/upload/" + user;
    File d = new File(dname);
    if (!d.exists()) {
        if (!d.mkdirs()) {
            l4j.error(getSessionName() + " try to mkdir " + dname + " failed.");
            throw new HiveServerException("Create user proto directory failed.");
        }
    }
    if (!fileName.trim().toLowerCase().endsWith(".proto")) {
        throw new HiveServerException(
                "Upload proto command can only handle .proto file, Check your file suffix");
    }

    fname = dname + "/" + fileName;

    RandomAccessFile raf;
    File f;
    try {
        f = new File(fname);
        if (!f.exists()) {
            if (!f.createNewFile()) {
                l4j.error("Try to create file " + fname + " failed.");
                throw new HiveServerException("Create user upload file " + fname + " failed.");
            }
        }
    } catch (java.io.IOException ex) {
        l4j.error(getSessionName() + " try to create file " + fname + " failed w/ " + ex);
        return false;
    }
    if (array.length == 0) {
        if (!f.delete()) {
            l4j.error("Try to delete file " + fname + " failed.");
            throw new HiveServerException("Delete user proto file " + fname + " failed.");
        } else {
            return true;
        }
    }
    try {
        raf = new RandomAccessFile(f, "rw");
    } catch (java.io.FileNotFoundException ex) {
        l4j.error(getSessionName() + " try to open file " + fname + " failed, not found.");
        return false;
    }
    try {
        raf.setLength(0);
        raf.seek(0);
        raf.write(array);
    } catch (java.io.IOException ex) {
        l4j.error(getSessionName() + " try to truncate/write file " + fname + "failed w/ " + ex);
        return false;
    }
    return res;
}

From source file:tvbrowser.ui.mainframe.MainFrame.java

@Override
public void drop(DropTargetDropEvent dtde) {
    dtde.acceptDrop(dtde.getDropAction());
    File[] files = getDragDropPlugins(dtde.getCurrentDataFlavors(), dtde.getTransferable());

    try {/*from   w w  w  .  j  a  v  a 2 s .co  m*/
        File tmpFile = File.createTempFile("plugins", ".txt");
        StringBuilder alreadyInstalled = new StringBuilder();
        StringBuilder notCompatiblePlugins = new StringBuilder();

        for (File jarFile : files) {
            ClassLoader classLoader = null;

            try {
                URL[] urls = new URL[] { jarFile.toURI().toURL() };
                classLoader = URLClassLoader.newInstance(urls, ClassLoader.getSystemClassLoader());
            } catch (MalformedURLException exc) {

            }

            if (classLoader != null) {
                // Get the plugin name
                String pluginName = jarFile.getName();
                pluginName = pluginName.substring(0, pluginName.length() - 4);

                try {
                    String pluginId = "java." + pluginName.toLowerCase() + "." + pluginName;

                    PluginProxy installedPlugin = PluginProxyManager.getInstance().getPluginForId(pluginId);
                    TvDataServiceProxy service = TvDataServiceProxyManager.getInstance()
                            .findDataServiceById(pluginName.toLowerCase() + '.' + pluginName);

                    Class<?> pluginClass = classLoader.loadClass(pluginName.toLowerCase() + '.' + pluginName);

                    Method getVersion = pluginClass.getMethod("getVersion", new Class[0]);

                    Version version1 = null;
                    try {
                        version1 = (Version) getVersion.invoke(pluginClass, new Object[0]);
                    } catch (Throwable t1) {
                        t1.printStackTrace();
                    }

                    if (installedPlugin != null
                            && (installedPlugin.getInfo().getVersion().compareTo(version1) > 0
                                    || (installedPlugin.getInfo().getVersion().compareTo(version1) == 0
                                            && version1.isStable()))) {
                        alreadyInstalled.append(installedPlugin.getInfo().getName()).append('\n');
                    } else if (service != null && (service.getInfo().getVersion().compareTo(version1) > 0
                            || (service.getInfo().getVersion().compareTo(version1) == 0
                                    && version1.isStable()))) {
                        alreadyInstalled.append(service.getInfo().getName()).append('\n');
                    } else {
                        RandomAccessFile write = new RandomAccessFile(tmpFile, "rw");

                        String versionString = Integer.toString(version1.getMajor()) + '.'
                                + (version1.getMinor() / 10) + (version1.getMinor() % 10) + '.'
                                + version1.getSubMinor();

                        write.seek(write.length());

                        write.writeBytes("[plugin:" + pluginName + "]\n");
                        write.writeBytes("name_en=" + pluginName + "\n");
                        write.writeBytes("filename=" + jarFile.getName() + "\n");
                        write.writeBytes("version=" + versionString + "\n");
                        write.writeBytes("stable=" + version1.isStable() + "\n");
                        write.writeBytes("download=" + jarFile.toURI().toURL() + "\n");
                        write.writeBytes("category=unknown\n");

                        write.close();

                    }
                } catch (Exception e) {
                    notCompatiblePlugins.append(jarFile.getName()).append("\n");
                }
            }
        }

        if (alreadyInstalled.length() > 0) {
            showInfoTextMessage(
                    mLocalizer.msg("update.alreadyInstalled",
                            "The following Plugin in current version are already installed:"),
                    alreadyInstalled.toString(), 400);
        }

        if (notCompatiblePlugins.length() > 0) {
            showInfoTextMessage(
                    mLocalizer.msg("update.noTVBPlugin", "This following files are not TV-Browser Plugins:"),
                    notCompatiblePlugins.toString(), 400);
        }

        if (tmpFile.length() > 0) {
            java.net.URL url = tmpFile.toURI().toURL();
            SoftwareUpdater softwareUpdater = new SoftwareUpdater(url, false, true);
            mSoftwareUpdateItems = softwareUpdater.getAvailableSoftwareUpdateItems();
            dtde.dropComplete(true);

            SoftwareUpdateDlg updateDlg = new SoftwareUpdateDlg(this, false, mSoftwareUpdateItems);
            updateDlg.setVisible(true);
        } else {
            dtde.rejectDrop();
            dtde.dropComplete(false);
        }

        if (!tmpFile.delete()) {
            tmpFile.deleteOnExit();
        }
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

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 . j  av a 2 s  .  c o 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;
}

From source file:org.apache.hadoop.hive.service.HSSessionItem.java

public boolean upload(String rtype, String user, String fileName, String data) throws HiveServerException {
    boolean res = true;
    String fname;//w  w w .ja v a2  s. co  m
    if (rtype.equalsIgnoreCase("jar")) {
        fname = getHome() + "/auxlib/" + fileName;
    } else if (rtype.equalsIgnoreCase("proto")) {
        String dname = getHome() + "/protobuf/upload/" + user;
        File d = new File(dname);
        if (!d.exists()) {
            if (!d.mkdirs()) {
                l4j.error(getSessionName() + " try to mkdir " + dname + " failed.");
                throw new HiveServerException("Create user proto directory failed.");
            }
        }
        if (!fileName.trim().toLowerCase().endsWith(".proto")) {
            throw new HiveServerException(
                    "Upload proto command can only handle .proto file, Check your file suffix");
        }

        fname = dname + "/" + fileName;
    } else {
        String errorMsg = "Can't upload filetype: " + rtype;
        l4j.error(getSessionName() + " upload failed: " + errorMsg);
        throw new HiveServerException("errorMsg");
    }

    RandomAccessFile raf;
    File f;
    try {
        f = new File(fname);
        if (!f.exists()) {
            if (!f.createNewFile()) {
                l4j.error("Try to create file " + fname + " failed.");
                throw new HiveServerException("Create user upload file " + fname + " failed.");
            }
        }
    } catch (java.io.IOException ex) {
        l4j.error(getSessionName() + " try to create file " + fname + " failed w/ " + ex);
        return false;
    }
    if (data.equalsIgnoreCase("")) {
        if (!f.delete()) {
            l4j.error("Try to delete file " + fname + " failed.");
            throw new HiveServerException("Delete user file " + fname + " failed.");
        } else {
            return true;
        }
    }

    try {
        raf = new RandomAccessFile(f, "rw");
    } catch (java.io.FileNotFoundException ex) {
        l4j.error(getSessionName() + " try to open file " + fname + " failed, not found.");
        return false;
    }
    try {
        raf.setLength(0);
        raf.seek(0);
        raf.write(data.getBytes());
    } catch (java.io.IOException ex) {
        l4j.error(getSessionName() + " try to truncate/write file " + fname + "failed w/ " + ex);
        return false;
    }
    return res;
}