List of usage examples for java.io RandomAccessFile RandomAccessFile
public RandomAccessFile(File file, String mode) throws FileNotFoundException
From source file:com.aol.advertising.qiao.injector.file.AbstractFileReader.java
protected RandomAccessFile openFile(File file) throws InterruptedException { RandomAccessFile reader = null; while (running && reader == null) { try {/*from ww w . j a v a2 s . c o m*/ reader = new RandomAccessFile(file, RAF_MODE); } catch (FileNotFoundException e) { if (dataHandler != null) dataHandler.fileNotFound(); } if (reader != null) return reader; boolean timed_out = CommonUtils.sleepQuietly(fileCheckDelayMillis); if (!timed_out) // interrupted break; } throw new InterruptedException("interrupted"); }
From source file:de.micromata.genome.logging.spi.ifiles.IndexedReader.java
public LogEntry select(int startOffset, boolean masterOnly) throws IOException { if (logRandomAccessFile == null) { logRandomAccessFile = new RandomAccessFile(logFile, "r"); // logChannel = logRandomAccessFile.getChannel(); // logByteBuffer = logChannel.map(FileChannel.MapMode.READ_ONLY, 0, logChannel.size()); }/* ww w . jav a 2s .c o m*/ LogEntry le = new LogEntry(); le.setLogEntryIndex(buildLogPk(startOffset)); logRandomAccessFile.seek(startOffset); for (Pair<String, Integer> pair : indexHeader.headerOrder) { String name = pair.getFirst(); Integer length = pair.getSecond(); String value = NIOUtils.readString(logRandomAccessFile, length, logCharset); logRandomAccessFile.read(); // | character StdSearchFields sf = StdSearchFields.findFromString(name); if (sf != null) { sf.getValueSetter().accept(le, value); } else { le.getAttributes().add(createLogAttribute(name, value)); } } if (masterOnly == true) { return le; } if (seekNextLine(startOffset) == false) { return le; } int ch; do { ch = logRandomAccessFile.read(); if (ch != '@') { break; } LogAttribute attr = parseLogAttributeFromPos(); if (attr == null) { break; } if (attr.getType().name().equals("message") == true) { le.setMessage(attr.getValue()); } else { le.getAttributes().add(attr); } } while (ch != -1); return le; }
From source file:com.facebook.infrastructure.net.TcpConnection.java
public void stream(File file, long startPosition, long endPosition) throws IOException { if (!bStream_) throw new IllegalStateException("Cannot stream since we are not set up to stream data."); lock_.lock();/*from w ww . j av a 2 s.c om*/ try { /* transfer 64MB in each attempt */ int limit = 64 * 1024 * 1024; long total = endPosition - startPosition; /* keeps track of total number of bytes transferred */ long bytesWritten = 0L; RandomAccessFile raf = new RandomAccessFile(file, "r"); FileChannel fc = raf.getChannel(); /* * If the connection is not yet established then wait for * the timeout period of 2 seconds. Attempt to reconnect 3 times and then * bail with an IOException. */ long waitTime = 2; int retry = 0; while (!connected_.get()) { if (retry == 3) throw new IOException("Unable to connect to " + remoteEp_ + " after " + retry + " attempts."); waitToContinueStreaming(waitTime, TimeUnit.SECONDS); ++retry; } while (bytesWritten < total) { if (startPosition == 0) { ByteBuffer buffer = MessagingService.constructStreamHeader(false, true); socketChannel_.write(buffer); handleIncompleteWrite(buffer); } /* returns the number of bytes transferred from file to the socket */ long bytesTransferred = fc.transferTo(startPosition, limit, socketChannel_); logger_.trace("Bytes transferred " + bytesTransferred); bytesWritten += bytesTransferred; startPosition += bytesTransferred; /* * If the number of bytes transferred is less than intended * then we need to wait till socket becomes writeable again. */ if (bytesTransferred < limit && bytesWritten != total) { if ((key_.interestOps() & SelectionKey.OP_WRITE) == 0) { SelectorManager.getSelectorManager().modifyKeyForWrite(key_); } waitToContinueStreaming(); } } } finally { lock_.unlock(); } }
From source file:com.example.android.vault.EncryptedDocumentTest.java
public void testBitTwiddle() throws Exception { final EncryptedDocument doc = new EncryptedDocument(4, mFile, mDataKey, mMacKey); // write some metadata final JSONObject before = new JSONObject(); before.put("twiddle", "twiddle"); doc.writeMetadataAndContent(before, null); final RandomAccessFile f = new RandomAccessFile(mFile, "rw"); f.seek(f.length() - 4);//from www .j ava 2 s . c o m f.write(0x00); f.close(); try { doc.readMetadata(); fail("somehow passed hmac"); } catch (DigestException expected) { } }
From source file:com.vincestyling.netroid.request.FileDownloadRequest.java
/** * In this method, we got the Content-Length, with the TemporaryFile length, * we can calculate the actually size of the whole file, if TemporaryFile not exists, * we'll take the store file length then compare to actually size, and if equals, * we consider this download was already done. * We used {@link RandomAccessFile} to continue download, when download success, * the TemporaryFile will be rename to StoreFile. *//*from w w w . j a v a 2 s .c om*/ @Override public byte[] handleResponse(HttpResponse response, Delivery delivery) throws IOException, ServerError { // Content-Length might be negative when use HttpURLConnection because it default header Accept-Encoding is gzip, // we can force set the Accept-Encoding as identity in prepare() method to slove this problem but also disable gzip response. HttpEntity entity = response.getEntity(); long fileSize = entity.getContentLength(); if (fileSize <= 0) { NetroidLog.d("Response doesn't present Content-Length!"); } long downloadedSize = mTemporaryFile.length(); boolean isSupportRange = HttpUtils.isSupportRange(response); if (isSupportRange) { fileSize += downloadedSize; // Verify the Content-Range Header, to ensure temporary file is part of the whole file. // Sometime, temporary file length add response content-length might greater than actual file length, // in this situation, we consider the temporary file is invalid, then throw an exception. String realRangeValue = HttpUtils.getHeader(response, "Content-Range"); // response Content-Range may be null when "Range=bytes=0-" if (!TextUtils.isEmpty(realRangeValue)) { String assumeRangeValue = "bytes " + downloadedSize + "-" + (fileSize - 1); if (TextUtils.indexOf(realRangeValue, assumeRangeValue) == -1) { throw new IllegalStateException("The Content-Range Header is invalid Assume[" + assumeRangeValue + "] vs Real[" + realRangeValue + "], " + "please remove the temporary file [" + mTemporaryFile + "]."); } } } // Compare the store file size(after download successes have) to server-side Content-Length. // temporary file will rename to store file after download success, so we compare the // Content-Length to ensure this request already download or not. if (fileSize > 0 && mStoreFile.length() == fileSize) { // Rename the store file to temporary file, mock the download success. ^_^ mStoreFile.renameTo(mTemporaryFile); // Deliver download progress. delivery.postDownloadProgress(this, fileSize, fileSize); return null; } RandomAccessFile tmpFileRaf = new RandomAccessFile(mTemporaryFile, "rw"); // If server-side support range download, we seek to last point of the temporary file. if (isSupportRange) { tmpFileRaf.seek(downloadedSize); } else { // If not, truncate the temporary file then start download from beginning. tmpFileRaf.setLength(0); downloadedSize = 0; } InputStream in = null; try { in = entity.getContent(); // Determine the response gzip encoding, support for HttpClientStack download. if (HttpUtils.isGzipContent(response) && !(in instanceof GZIPInputStream)) { in = new GZIPInputStream(in); } byte[] buffer = new byte[6 * 1024]; // 6K buffer int offset; while ((offset = in.read(buffer)) != -1) { tmpFileRaf.write(buffer, 0, offset); downloadedSize += offset; delivery.postDownloadProgress(this, fileSize, downloadedSize); if (isCanceled()) { delivery.postCancel(this); break; } } } finally { try { // Close the InputStream if (in != null) in.close(); } catch (Exception e) { NetroidLog.v("Error occured when calling InputStream.close"); } try { // release the resources by "consuming the content". entity.consumeContent(); } catch (Exception e) { // This can happen if there was an exception above that left the entity in // an invalid state. NetroidLog.v("Error occured when calling consumingContent"); } tmpFileRaf.close(); } return null; }
From source file:gate.util.reporting.PRTimeReporter.java
/** * Stores GATE processing elements and the time taken by them in an in-memory * data structure for report generation. * * @param inputFile// ww w . j av a 2 s .c om * A File handle of the input log file. * * @return An Object of type LinkedHashMap<String, Object> containing the * processing elements (with time in milliseconds) in hierarchical * structure. Null if there was an error. */ @Override public Object store(File inputFile) throws BenchmarkReportInputFileFormatException { LinkedHashMap<String, Object> globalStore = new LinkedHashMap<String, Object>(); long fromPos = 0; RandomAccessFile in = null; try { if (getLogicalStart() != null) { fromPos = tail(inputFile, FILE_CHUNK_SIZE); } in = new RandomAccessFile(inputFile, "r"); if (getLogicalStart() != null) { in.seek(fromPos); } ArrayList<String> startTokens = new ArrayList<String>(); String logEntry; String docName = null; Pattern pattern = Pattern.compile("(\\d+) (\\d+) (.*) (.*) \\{(.*)\\}"); while ((logEntry = in.readLine()) != null) { Matcher matcher = pattern.matcher(logEntry); // Skip the statistics for the event documentLoaded if (logEntry.matches(".*documentLoaded.*")) continue; if (logEntry.matches(".*START.*")) { String[] splittedStartEntry = logEntry.split("\\s"); String startToken = (splittedStartEntry.length > 2) ? splittedStartEntry[2] : null; if (startToken == null) { throw new BenchmarkReportInputFileFormatException( getBenchmarkFile().getAbsolutePath() + " is invalid."); } startTokens.add(startToken); if (startToken.endsWith("Start")) continue; organizeEntries(globalStore, startToken.split("\\."), "0"); } if (matcher != null) { if (matcher.matches()) { if (validateLogEntry(matcher.group(3), startTokens)) { String[] splittedBIDs = matcher.group(3).split("\\."); if (splittedBIDs.length > 1) { docName = splittedBIDs[1]; pipelineNames.add(splittedBIDs[0]); } organizeEntries(globalStore, (matcher.group(3).replaceFirst(Pattern.quote(docName) + ".", "")).split("\\."), matcher.group(2)); } } } } } catch (IOException e) { e.printStackTrace(); globalStore = null; } finally { try { if (in != null) { in.close(); } } catch (IOException e) { e.printStackTrace(); globalStore = null; } } if (validEntries == 0) { if (logicalStart != null) { throw new BenchmarkReportInputFileFormatException( "No valid log entries present in " + getBenchmarkFile().getAbsolutePath() + " does not contain a marker named " + logicalStart + "."); } else { throw new BenchmarkReportInputFileFormatException( "No valid log entries present in " + getBenchmarkFile().getAbsolutePath()); } } return globalStore; }
From source file:com.yifanlu.PSXperiaTool.PSXperiaTool.java
private void patchGame() throws IOException { /*// ww w . j a va 2 s.c om * Custom patch format (config/game-patch.bin) is as follows: * 0x8 byte little endian: Address in game image to start patching * 0x8 byte little endian: Length of patch * If there are more patches, repeat after reading the length of patch * Note that all games will be patched the same way, so if a game is broken before patching, it will still be broken! */ nextStep("Patching game."); File gamePatch = new File(mTempDir, "/config/game-patch.bin"); if (!gamePatch.exists()) return; Logger.info("Making a copy of game."); File tempGame = new File(mTempDir, "game.iso"); FileUtils.copyFile(mInputFile, tempGame); RandomAccessFile game = new RandomAccessFile(tempGame, "rw"); InputStream patch = new FileInputStream(gamePatch); while (true) { byte[] rawPatchAddr = new byte[8]; byte[] rawPatchLen = new byte[8]; if (patch.read(rawPatchAddr) + patch.read(rawPatchLen) < rawPatchAddr.length + rawPatchLen.length) break; ByteBuffer bb = ByteBuffer.wrap(rawPatchAddr); bb.order(ByteOrder.LITTLE_ENDIAN); long patchAddr = bb.getLong(); bb = ByteBuffer.wrap(rawPatchLen); bb.order(ByteOrder.LITTLE_ENDIAN); long patchLen = bb.getLong(); game.seek(patchAddr); while (patchLen-- > 0) { game.write(patch.read()); } } mInputFile = tempGame; game.close(); patch.close(); Logger.debug("Done patching game."); }
From source file:esg.node.components.monitoring.InfoResources.java
InfoResources(String filename, int buffSize_) { this.filename = filename; this.buffSize = buffSize_; System.out.println("InfoResource initializing for " + filename); try {/*from w ww.j a va 2s .c om*/ File procFile = new File(filename); if (buffSize < 0) { buffSize = (int) procFile.length(); if (buffSize == 0) { buffSize = guestimateSize; } } raf = new RandomAccessFile(procFile, "r"); fc = raf.getChannel(); bb = ByteBuffer.allocateDirect(buffSize); Charset cs = Charset.forName("8859_1"); decoder = cs.newDecoder(); cb = CharBuffer.allocate(buffSize); System.out.println("Buffer Size is " + buffSize + " number of bytes"); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.btoddb.fastpersitentqueue.JournalFileTest.java
@Test public void testNumberOfEntriesIsIncorrect() throws Exception { JournalFile jf1 = new JournalFile(theFile); jf1.initForWriting(new UUID()); jf1.append(new FpqEntry(1, new byte[10])); jf1.close();// w ww. j a v a 2 s. c om // mess up the UUID RandomAccessFile raFile = new RandomAccessFile(theFile, "rw"); Utils.writeInt(raFile, 1); Utils.writeUuidToFile(raFile, jf1.getId()); Utils.writeLong(raFile, 123L); raFile.close(); JournalFile jf2 = new JournalFile(theFile); jf2.initForReading(); Iterator<FpqEntry> iter = jf2.iterator(); long count = 0; while (iter.hasNext()) { count++; iter.next(); } assertThat(jf2.getNumberOfEntries(), is(not(count))); assertThat(count, is(jf1.getNumberOfEntries())); }
From source file:de.erdesignerng.dialect.msaccess.MSAccessFileFormat.java
private static int findInFile(String aFileName, String aSearchFor) { int theBufferSize = 5242880; // 5MB boolean theSearchOn = true; String theStringBuffer;//from w w w . ja va 2 s . c o m int theOffset = 0; int theRead = theBufferSize; int thePosition; int theOverhead = aSearchFor.length() - 1; int theResult = -1; if (theBufferSize >= aSearchFor.length()) { try { File file = new File(aFileName); RandomAccessFile ra = new RandomAccessFile(aFileName, "r"); byte[] theByteBuffer = new byte[theBufferSize]; while ((theOffset < file.length()) && (theSearchOn) && (theRead == theBufferSize)) { theRead = ra.read(theByteBuffer); if (theRead >= 0) { theStringBuffer = new String(theByteBuffer, 0, theRead); thePosition = theStringBuffer.indexOf(aSearchFor); if (thePosition >= 0) { theResult = theOffset + thePosition; theSearchOn = false; LOGGER.debug( "Found '" + aSearchFor + "' in '" + aFileName + "' at position " + theResult); } else { if (theRead == theBufferSize) { theOffset += (theRead - theOverhead); ra.seek(theOffset); } } } } ra.close(); } catch (FileNotFoundException ex) { LOGGER.error("Cannot find database file " + aFileName, ex); } catch (IOException ex) { LOGGER.error("Cannot read database file " + aFileName, ex); } } else { throw new RuntimeException("The string to find is too long. Only strings of lenght up to " + theBufferSize + " can be found!"); } return theResult; }