Example usage for java.io RandomAccessFile readInt

List of usage examples for java.io RandomAccessFile readInt

Introduction

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

Prototype

public final int readInt() throws IOException 

Source Link

Document

Reads a signed 32-bit integer from this file.

Usage

From source file:org.ms123.common.git.GitServiceImpl.java

protected static String getFileType(File file) {
    if (file.isDirectory()) {
        return "sw.directory";
    } else if (file.toString().endsWith(".txt")) {
        return "text/plain";
    } else if (file.toString().endsWith(".jpeg") || file.toString().endsWith(".jpg")) {
        return "image/jpeg";
    } else if (file.toString().endsWith(".png")) {
        return "image/png";
    } else if (file.toString().endsWith(".svg")) {
        return "image/svg+xml";
    } else if (file.toString().endsWith(".xml")) {
        return "text/xml";
    } else if (file.toString().endsWith(".woff") || file.toString().endsWith(".woff.gz")) {
        return "application/x-font-woff";
    } else if (file.toString().endsWith(".js") || file.toString().endsWith(".js.gz")) {
        return "text/javascript";
    } else if (file.toString().endsWith(".adoc") || file.toString().endsWith(".adoc.gz")) {
        return "text/x-asciidoc";
    } else if (file.toString().endsWith(".html") || file.toString().endsWith(".html.gz")) {
        return "text/html";
    } else if (file.toString().endsWith(".css") || file.toString().endsWith(".css.gz")) {
        return "text/css";
    } else if (file.toString().endsWith(".yaml") || file.toString().endsWith(".yml")) {
        return "text/x-yaml";
    } else if (file.toString().endsWith(".json") || file.toString().endsWith(".json.gz")) {
        return "application/json";
    } else if (file.toString().endsWith(".odt")) {
        return "application/vnd.oasis.opendocument.text";
    } else if (file.toString().endsWith(".groovy")) {
        return "sw.groovy";
    } else if (file.toString().endsWith(".java")) {
        return "sw.java";
    }//from  w  ww .  j ava2  s. c  om
    RandomAccessFile r = null;
    try {
        int lnr = 0;
        r = new RandomAccessFile(file, "r");
        int i = r.readInt();
        if (i == 1534293853) {
            r.seek(0);
            while (true) {
                String line = r.readLine();
                if (line == null) {
                    break;
                }
                if (lnr == 0 && !line.startsWith("[sw]"))
                    break;
                if (line.trim().startsWith("type")) {
                    int eq = line.indexOf("=");
                    if (eq != -1) {
                        return line.substring(eq + 1).trim();
                    }
                }
                lnr++;
                if (lnr > 20)
                    break;
            }
        } else if (i == -2555936) {
            return "image/jpeg";
        } else if (i == -1991225785) {
            return "image/png";
        }
    } catch (Exception e) {
        return "sw.unknown";
    } finally {
        closeFile(r);
    }
    return detectFileType(file);
}

From source file:org.apache.hadoop.hdfs.server.datanode.DataStorage.java

public boolean isConversionNeeded(StorageDirectory sd) throws IOException {
    File oldF = new File(sd.getRoot(), "storage");
    if (!oldF.exists())
        return false;
    // check the layout version inside the storage file
    // Lock and Read old storage file
    RandomAccessFile oldFile = new RandomAccessFile(oldF, "rws");
    FileLock oldLock = oldFile.getChannel().tryLock();
    try {//  w w  w  .j a  v a  2s.  com
        oldFile.seek(0);
        int oldVersion = oldFile.readInt();
        if (oldVersion < LAST_PRE_UPGRADE_LAYOUT_VERSION)
            return false;
    } finally {
        oldLock.release();
        oldFile.close();
    }
    return true;
}

From source file:edu.msu.cme.rdp.readseq.readers.core.SFFCore.java

private LinkedHashMap<String, Long> readIndex() throws IOException {
    if (commonHeader.indexOffset <= commonHeader.headerLength) {
        throw new IOException("Index offset is not set correctly");
    }/*from   w  w  w.j a  va2 s  . co  m*/

    RandomAccessFile seqFile = super.getRawFile();

    long seekBackTo = seqFile.getFilePointer();
    seqFile.seek(commonHeader.indexOffset);
    long dataEnd = seqFile.getFilePointer();
    LinkedHashMap<String, Long> seqIndex = new LinkedHashMap(commonHeader.numReads);

    int magicNumber = seqFile.readInt();

    if (magicNumber == mftMagicNumber) {
        int version = seqFile.readInt();

        if (version != v1MagicNumber) {
            throw new IOException("Can only parse .mft v1.0 indices");
        }

        int xmlSize = seqFile.readInt();
        int dataSize = seqFile.readInt();
        dataEnd += dataSize;

        byte[] xml = new byte[xmlSize];
        seqFile.read(xml);
        manifest = new String(xml);

    } else if (magicNumber == srtMagicNumber) {
        int version = seqFile.readInt();

        if (version != v1MagicNumber) {
            throw new IOException("Can only parse .srt v1.0 indices");
        }

        if (seqFile.read() != 0) {
            throw new IOException("Failed to find expected null byte in .srt header");
        }
        dataEnd += commonHeader.indexLength;
    } else {
        throw new IOException("No supported index found");
    }

    List<Integer> currIndex = new ArrayList();
    while (seqFile.getFilePointer() < dataEnd) {
        int b = seqFile.readUnsignedByte();
        if (b == 0xff) {
            byte[] nameArray = new byte[currIndex.size() - 5];
            long indexLoc = 0;
            int[] multipliers = new int[] { 0, 16581375, 65025, 255, 1 };
            for (int i = 0; i < currIndex.size(); i++) {
                if (i < nameArray.length) {
                    nameArray[i] = (byte) (currIndex.get(i) & 0xff);
                } else {
                    int index = i - nameArray.length;
                    indexLoc += currIndex.get(i) * multipliers[index];
                }
            }
            String name = new String(nameArray);

            seqIndex.put(name, indexLoc);

            currIndex.clear();
        } else {
            currIndex.add(b);
        }
    }
    seqFile.seek(seekBackTo);

    return seqIndex;
}

From source file:org.mhisoft.wallet.service.AttachmentService.java

private ReadContentVO readCipherParameter(RandomAccessFile fileIn, long pos)
        throws IOException, NoSuchAlgorithmException {
    ReadContentVO ret = new ReadContentVO();
    ret.pos = pos;/*from   w ww.  j a  va2  s  . c o  m*/

    /*#3: ciperParameters size 4 bytes*/
    int cipherParametersLength = fileIn.readInt();
    ret.pos += 4;
    //logger.fine("read cipherParametersLength:" + cipherParametersLength);

    /*#4: cipherParameters body*/
    byte[] _byteCiper = new byte[cipherParametersLength];
    int readBytes = fileIn.read(_byteCiper);
    if (readBytes != cipherParametersLength)
        throw new RuntimeException("read " + readBytes + " bytes only, expected to read:" + _byteCiper);
    ret.pos += _byteCiper.length;

    ret.algorithmParameters = AlgorithmParameters.getInstance(PBEEncryptor.ALGORITHM);
    ret.algorithmParameters.init(_byteCiper);

    return ret;

}

From source file:org.mhisoft.wallet.service.AttachmentService.java

/**
 * Append to the existing store for Merged and Created/Updated attachments.
 * @param filename// w  ww  .  j a  v  a 2  s .c o  m
 * @param model
 * @param encryptor
 */
protected void appendAttachmentStore(final String filename, final WalletModel model,
        final PBEEncryptor encryptor) {

    FileAccessTable t = new FileAccessTable();
    for (WalletItem item : model.getItemsFlatList()) {
        if (item.getAttachmentEntry() == null)
            continue;
        if (item.getAttachmentEntry().getAccessFlag() == FileAccessFlag.Merge) {
            t.addEntry(item.getAttachmentEntry());
        } else if (FileAccessFlag.Create == item.getAttachmentEntry().getAccessFlag()
                || FileAccessFlag.Update == item.getAttachmentEntry().getAccessFlag()) {

            if (item.getNewAttachmentEntry() != null && item.getNewAttachmentEntry().getFile() != null) {
                t.addEntry(item.getNewAttachmentEntry());
            } else if (item.getAttachmentEntry().getFile() != null) {
                t.addEntry(item.getAttachmentEntry());
            }
        }
    }

    RandomAccessFile attachmentFileStore = null;
    try {
        attachmentFileStore = new RandomAccessFile(filename, "rw");

        if (t.getSize() > 0) {

            //write the total number of entries first
            int entriesCount = attachmentFileStore.readInt();

            //add to be appended ones
            entriesCount += t.getSize();
            attachmentFileStore.seek(0);
            attachmentFileStore.writeInt(entriesCount);

            //seek to the end
            long itemStartPos = attachmentFileStore.length();
            attachmentFileStore.seek(itemStartPos);

            //append new entries to the end of the store.
            writeFileEntries(model, false, filename, itemStartPos, attachmentFileStore, t,
                    model.getEncryptorForRead(), encryptor);

        }

        /*marked the deleted entries **/
        for (WalletItem item : model.getItemsFlatList()) {
            if (item.getAttachmentEntry() == null)
                continue;
            if (FileAccessFlag.Delete == item.getAttachmentEntry().getAccessFlag() //the attachment is deleted
                    //the entry had the content saved in the store, now it is replaced. the new content is appended to the end of the file.
                    // the file entry at the old position needs to be marked as DELETE.
                    || (FileAccessFlag.Update == item.getAttachmentEntry().getAccessFlag()
                            && item.getAttachmentEntry().getEncSize() > 0)
                            && item.getAttachmentEntry().getPosition() > 0) {

                attachmentFileStore.seek(item.getAttachmentEntry().getPosition() + 40);
                attachmentFileStore.writeInt(FileAccessFlag.Delete.ordinal());
            }
        }

        attachmentFileStore.close();
        attachmentFileStore = null;

    } catch (IOException e) {
        e.printStackTrace();
        DialogUtils.getInstance().error("Error writing attachment entries.", e.getMessage());
    } finally {
        if (attachmentFileStore != null)
            try {
                attachmentFileStore.close();
            } catch (IOException e) {
                //e.printStackTrace();
            }
    }

}

From source file:org.mhisoft.wallet.service.AttachmentService.java

public FileAccessTable read(String dataFile, final PBEEncryptor encryptor) {
    logger.fine("\n\nread attachment file:" + dataFile);
    FileAccessTable t = null;//from   w w  w  . j  a v a 2 s.c  o  m
    RandomAccessFile fileIn = null;
    try {
        File fIn = new File(dataFile);
        if (!fIn.exists()) {
            return t;
        }

        fileIn = new RandomAccessFile(dataFile, "rw");
        int numberOfEntries = fileIn.readInt();
        t = new FileAccessTable();
        long pos = 4;
        int readBytes = 0;
        for (int i = 0; i < numberOfEntries; i++) {

            fileIn.seek(pos);

            /* UUIID and position */
            String UUID = FileUtils.readString(fileIn);
            FileAccessEntry fileAccessEntry = new FileAccessEntry(UUID);
            pos += 40;
            logger.fine("Read entry, UUID:" + UUID);

            /*  accessflag */
            fileAccessEntry.setAccessFlag(FileAccessFlag.values[fileIn.readInt()]);
            pos += 4;
            logger.fine("\t access flag:" + fileAccessEntry.getAccessFlag());

            /*  pos */
            fileAccessEntry.setPosition(fileIn.readLong());
            pos += 8;
            logger.fine("\t position:" + fileAccessEntry.getPosition());

            /* read filename */
            ReadContentVO vo = readCipherParameter(fileIn, pos);
            pos = vo.pos;
            int encSize_FileName = fileIn.readInt();
            pos += 4;
            byte[] _encedBytes = new byte[encSize_FileName];
            fileIn.readFully(_encedBytes);
            pos += encSize_FileName;
            byte[] byte_filename = encryptor.decrypt(_encedBytes, vo.algorithmParameters);
            fileAccessEntry.setFileName(StringUtils.bytesToString(byte_filename));
            logger.fine("\t file name:" + fileAccessEntry.getFileName());

            /* attachment */
            vo = readCipherParameter(fileIn, pos);
            pos = vo.pos;
            fileAccessEntry.setAlgorithmParameters(vo.algorithmParameters);

            /*#5  size of the content (int): 4 bytes */
            int encSize = fileIn.readInt();
            pos += 4;

            fileAccessEntry.setPosOfContent(pos);
            fileAccessEntry.setEncSize(encSize);
            logger.fine("\t encSize:" + encSize);

            if (fileAccessEntry.getAccessFlag() != FileAccessFlag.Delete)
                t.addEntry(fileAccessEntry);
            else {
                logger.fine("\tentries is marked as deleted.");
                t.deletedEntries++;
            }

            /* #6 file content */
            pos += encSize;

            /* delay read it on demand
            byte[] _encBytes =readFileContent(dataFile, pos, encSize ) ;
            byte[] fileContent = encryptor.decrypt(_encBytes, algorithmParameters);
            pos +=  fileContent.length;
                    
            fileAccessEntry.setFileContent(fileContent);
            fileAccessEntry.setSize(fileContent.length); //decrypted size.
            */

        }

        fileIn.close();
        fileIn = null;

    } catch (Exception e) {
        e.printStackTrace();
        DialogUtils.getInstance().error("Error occurred in reading attachments.", e.getMessage());
    } finally {
        if (fileIn != null)
            try {
                fileIn.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }

    DebugUtil.append(
            "Attachment Store total entries:" + t.getSize() + "\n" + "Orphan records :" + t.deletedEntries);

    return t;
}

From source file:org.apache.hadoop.hdfs.server.namenode.NNStorage.java

@Override // Storage
public boolean isConversionNeeded(StorageDirectory sd) throws IOException {
    if (disablePreUpgradableLayoutCheck) {
        return false;
    }//from w  w w  .  j  av  a  2  s. c o  m

    File oldImageDir = new File(sd.getRoot(), "image");
    if (!oldImageDir.exists()) {
        return false;
    }
    // check the layout version inside the image file
    File oldF = new File(oldImageDir, "fsimage");
    RandomAccessFile oldFile = new RandomAccessFile(oldF, "rws");
    try {
        oldFile.seek(0);
        int oldVersion = oldFile.readInt();
        oldFile.close();
        oldFile = null;
        if (oldVersion < LAST_PRE_UPGRADE_LAYOUT_VERSION)
            return false;
    } finally {
        IOUtils.cleanup(LOG, oldFile);
    }
    return true;
}

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

private HashMap<String, Object> getGridIndexEntry(String path, String objectId) {
    HashMap<String, Object> map = new HashMap<String, Object>();
    RandomAccessFile raf = null;
    try {//from   w w  w. j ava 2  s  . c  om
        raf = new RandomAccessFile(path + ".wkt.index.dat", "r");

        int s2 = Integer.parseInt(objectId);

        // it is all in order, seek to the record
        int recordSize = 4 * 7; // 2 int + 5 float
        int start = raf.readInt();
        raf.seek(recordSize * (s2 - start));

        map.put("gn", raf.readInt());
        map.put("charoffset", raf.readInt());
        map.put("minx", raf.readFloat());
        map.put("miny", raf.readFloat());
        map.put("maxx", raf.readFloat());
        map.put("maxy", raf.readFloat());
        map.put("area", raf.readFloat());
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    } finally {
        try {
            if (raf != null) {
                raf.close();
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    return map;
}

From source file:org.commoncrawl.service.crawler.CrawlLog.java

private static void transferLocalCheckpointLog(File crawlLogPath, HDFSCrawlURLWriter writer, long checkpointId)
        throws IOException {

    // and open the crawl log file ...
    RandomAccessFile inputStream = null;

    IOException exception = null;

    CRC32 crc = new CRC32();
    CustomByteArrayOutputStream buffer = new CustomByteArrayOutputStream(1 << 17);
    byte[] syncBytesBuffer = new byte[SYNC_BYTES_SIZE];

    // save position for potential debug output.
    long lastReadPosition = 0;

    try {/*w w  w  . jav a2 s. c o  m*/
        inputStream = new RandomAccessFile(crawlLogPath, "rw");
        // and a data input stream ...
        RandomAccessFile reader = inputStream;
        // seek to zero
        reader.seek(0L);

        // read the header ...
        LogFileHeader header = readLogFileHeader(reader);

        // read a crawl url from the stream...

        while (inputStream.getFilePointer() < header._fileSize) {

            if (seekToNextSyncBytesPos(syncBytesBuffer, reader, header._fileSize)) {

                try {
                    lastReadPosition = inputStream.getFilePointer();

                    // skip sync
                    inputStream.skipBytes(SYNC_BYTES_SIZE);

                    // read length ...
                    int urlDataLen = reader.readInt();
                    long urlDataCRC = reader.readLong();

                    if (urlDataLen > buffer.getBuffer().length) {
                        buffer = new CustomByteArrayOutputStream(((urlDataLen / 65536) + 1) * 65536);
                    }
                    reader.read(buffer.getBuffer(), 0, urlDataLen);
                    crc.reset();
                    crc.update(buffer.getBuffer(), 0, urlDataLen);

                    long computedValue = crc.getValue();

                    // validate crc values ...
                    if (computedValue != urlDataCRC) {
                        LOG.error("CRC Mismatch Detected during HDFS transfer in CrawlLog:"
                                + crawlLogPath.getAbsolutePath() + " Checkpoint Id:" + checkpointId
                                + " FilePosition:" + lastReadPosition);
                        inputStream.seek(lastReadPosition + 1);
                    } else {
                        // allocate a crawl url data structure
                        CrawlURL url = new CrawlURL();
                        DataInputStream bufferReader = new DataInputStream(
                                new ByteArrayInputStream(buffer.getBuffer(), 0, urlDataLen));
                        // populate it from the (in memory) data stream
                        url.readFields(bufferReader);
                        try {
                            // and write out appropriate sequence file entries ...
                            writer.writeCrawlURLItem(new Text(url.getUrl()), url);
                        } catch (IOException e) {
                            LOG.error("Failed to write CrawlURL to SequenceFileWriter with Exception:"
                                    + CCStringUtils.stringifyException(e));
                            throw new URLWriterException();
                        }
                    }
                } catch (URLWriterException e) {
                    LOG.error("Caught URLRewriter Exception! - Throwing to outer layer!");
                    throw e;
                } catch (Exception e) {
                    LOG.error("Ignoring Error Processing CrawlLog Entry at Position:" + lastReadPosition
                            + " Exception:" + CCStringUtils.stringifyException(e));
                }
            } else {
                break;
            }
        }
    } catch (EOFException e) {
        LOG.error("Caught EOF Exception during read of local CrawlLog:" + crawlLogPath.getAbsolutePath()
                + " Checkpoint Id:" + checkpointId + " FilePosition:" + lastReadPosition);
    } catch (IOException e) {
        LOG.error(CCStringUtils.stringifyException(e));
        exception = e;
        throw e;
    } finally {
        if (inputStream != null)
            inputStream.close();
    }
}

From source file:org.apache.hadoop.hdfs.server.namenode.TestEditLog.java

/** 
 * Test edit log failover from a corrupt edit log
 *///from   w w  w . j  av  a2  s.c  om
@Test
public void testEditLogFailOverFromCorrupt() throws IOException {
    File f1 = new File(TEST_DIR + "/failover0");
    File f2 = new File(TEST_DIR + "/failover1");
    List<URI> editUris = ImmutableList.of(f1.toURI(), f2.toURI());

    NNStorage storage = setupEdits(editUris, 3);

    final long startErrorTxId = 1 * TXNS_PER_ROLL + 1;
    final long endErrorTxId = 2 * TXNS_PER_ROLL;

    File[] files = new File(f1, "current").listFiles(new FilenameFilter() {
        public boolean accept(File dir, String name) {
            if (name.startsWith(NNStorage.getFinalizedEditsFileName(startErrorTxId, endErrorTxId))) {
                return true;
            }
            return false;
        }
    });
    assertEquals(1, files.length);

    long fileLen = files[0].length();
    LOG.debug("Corrupting Log File: " + files[0] + " len: " + fileLen);
    RandomAccessFile rwf = new RandomAccessFile(files[0], "rw");
    rwf.seek(fileLen - 4); // seek to checksum bytes
    int b = rwf.readInt();
    rwf.seek(fileLen - 4);
    rwf.writeInt(b + 1);
    rwf.close();

    FSEditLog editlog = getFSEditLog(storage);
    editlog.initJournalsForWrite();
    long startTxId = 1;
    Collection<EditLogInputStream> streams = null;
    try {
        streams = editlog.selectInputStreams(startTxId, 4 * TXNS_PER_ROLL);
        readAllEdits(streams, startTxId);
    } catch (IOException e) {
        LOG.error("edit log failover didn't work", e);
        fail("Edit log failover didn't work");
    } finally {
        IOUtils.cleanup(null, streams.toArray(new EditLogInputStream[0]));
    }
}