List of usage examples for java.io RandomAccessFile seek
public void seek(long pos) throws IOException
From source file:raptor.service.DictionaryService.java
/** * This method started out as code from/*from w w w. java 2 s . c o m*/ * http://stackoverflow.com/questions/736556/binary * -search-in-a-sorted-memory-mapped-file-in-java * * It has been altered to fix some bugs. */ protected boolean binarySearch(String filename, String string) { string = string.toLowerCase(); long startTime = System.currentTimeMillis(); RandomAccessFile raf = null; boolean result = false; try { File file = new File(filename); raf = new RandomAccessFile(file, "r"); long low = 0; long high = file.length(); long p = -1; while (low < high) { long mid = (low + high) / 2; p = mid; while (p >= 0) { raf.seek(p); char c = (char) raf.readByte(); if (c == '\n') break; p--; } if (p < 0) raf.seek(0); String line = raf.readLine(); // Useful for debugging // System.out.println("-- " + mid + " " + line); if (line == null) { low = high; } else { int compare = line.compareTo(string); if (compare < 0) { low = mid + 1; } else if (compare == 0) { return true; } else { high = mid; } } } p = low; while (p >= 0 && p < high) { raf.seek(p); if (((char) raf.readByte()) == '\n') break; p--; } if (p < 0) raf.seek(0); while (true) { String line = raf.readLine(); // Useful for debugging. // System.out.println("searching forwards " + line); if (line == null) { result = false; break; } else if (line.equals(string)) { result = true; break; } else if (!line.startsWith(string)) { result = false; break; } } } catch (Throwable t) { Raptor.getInstance().onError("Error reading dictionary file: " + DICTIONARY_PATH, t); } finally { try { raf.close(); } catch (Throwable t) { } if (LOG.isDebugEnabled()) { LOG.debug("Searched " + string + " (" + (System.currentTimeMillis() - startTime) + ") " + result); } } return result; }
From source file:org.caboclo.clients.GoogleDriveClient.java
@Override public void sendNextPart(MultiPartUpload mpu) { byte[] chunk = new byte[Constants.CHUNK_UPLOAD_SIZE]; try {//w ww .j a va 2s .c o m RandomAccessFile raf = new RandomAccessFile(mpu.getFile(), "r"); raf.seek(mpu.getOffset()); int chunkLen = raf.read(chunk); if (chunkLen < 0) { mpu.setFinished(); raf.close(); return; } if (chunkLen < Constants.CHUNK_UPLOAD_SIZE) { chunk = Arrays.copyOfRange(chunk, 0, chunkLen); } String sessionURL = (String) mpu.getObject("location"); String mimeType = (String) mpu.getObject("mimeType"); long start = mpu.getOffset(); long end = start + chunkLen - 1; long total = mpu.getFile().length(); String contentRange = "bytes " + start + "-" + end + "/" + total; Client client = Client.create(); WebResource webResource = client.resource(sessionURL); ClientResponse response = webResource.header("Host", "www.googleapis.com") .header("Authorization", "Bearer " + token).header("Content-Length", "" + chunkLen) .header("Content-Type", mimeType).header("Content-Range", contentRange) .put(ClientResponse.class, chunk); mpu.incrOffset(chunkLen); } catch (IOException ex) { ex.printStackTrace(); } }
From source file:ome.services.blitz.impl.ExporterI.java
/** * Read size bytes, and transition to "waiting" If any exception is thrown, * the offset for the current file will not be updated. */// w ww . j a v a2 s . c o m private byte[] read(long pos, int size) throws ServerError { if (size > MAX_SIZE) { throw new ApiUsageException("Max read size is: " + MAX_SIZE); } byte[] buf = new byte[size]; RandomAccessFile ra = null; try { ra = new RandomAccessFile(file, "r"); long l = ra.length(); if (pos + size > l) { size = (int) (l - pos); } ra.seek(pos); int read = ra.read(buf); // Handle end of file if (read < 0) { buf = new byte[0]; } else if (read < size) { byte[] newBuf = new byte[read]; System.arraycopy(buf, 0, newBuf, 0, read); buf = newBuf; } } catch (IOException io) { throw new RuntimeException(io); } finally { if (ra != null) { try { ra.close(); } catch (IOException e) { log.warn("IOException on file close"); } } } return buf; }
From source file:org.apache.cassandra.db.ScrubTest.java
private void overrideWithGarbage(SSTableReader sstable, long startPosition, long endPosition) throws IOException { RandomAccessFile file = new RandomAccessFile(sstable.getFilename(), "rw"); file.seek(startPosition); file.writeBytes(StringUtils.repeat('z', (int) (endPosition - startPosition))); file.close();// w w w . j a va2s . c o m }
From source file:org.apache.hadoop.hdfs.server.namenode.TestFsck.java
public void testCorruptBlock() throws Exception { Configuration conf = new Configuration(); conf.setLong("dfs.blockreport.intervalMsec", 1000); FileSystem fs = null;/* w ww. j a va 2s .c o m*/ DFSClient dfsClient = null; LocatedBlocks blocks = null; int replicaCount = 0; Random random = new Random(); String outStr = null; MiniDFSCluster cluster = null; try { cluster = new MiniDFSCluster(conf, 3, true, null); cluster.waitActive(); fs = cluster.getFileSystem(); Path file1 = new Path("/testCorruptBlock"); DFSTestUtil.createFile(fs, file1, 1024, (short) 3, 0); // Wait until file replication has completed DFSTestUtil.waitReplication(fs, file1, (short) 3); String block = DFSTestUtil.getFirstBlock(fs, file1).getBlockName(); // Make sure filesystem is in healthy state outStr = runFsck(conf, 0, true, "/"); System.out.println(outStr); assertTrue(outStr.contains(NamenodeFsck.HEALTHY_STATUS)); // corrupt replicas File baseDir = new File(System.getProperty("test.build.data", "build/test/data"), "dfs/data"); for (int i = 0; i < 6; i++) { File blockFile = new File(baseDir, "data" + (i + 1) + "/current/" + block); if (blockFile.exists()) { RandomAccessFile raFile = new RandomAccessFile(blockFile, "rw"); FileChannel channel = raFile.getChannel(); String badString = "BADBAD"; int rand = random.nextInt((int) channel.size() / 2); raFile.seek(rand); raFile.write(badString.getBytes()); raFile.close(); } } // Read the file to trigger reportBadBlocks try { IOUtils.copyBytes(fs.open(file1), new IOUtils.NullOutputStream(), conf, true); } catch (IOException ie) { // Ignore exception } dfsClient = new DFSClient(new InetSocketAddress("localhost", cluster.getNameNodePort()), conf); blocks = dfsClient.namenode.getBlockLocations(file1.toString(), 0, Long.MAX_VALUE); replicaCount = blocks.get(0).getLocations().length; while (replicaCount != 3) { try { Thread.sleep(100); } catch (InterruptedException ignore) { } blocks = dfsClient.namenode.getBlockLocations(file1.toString(), 0, Long.MAX_VALUE); replicaCount = blocks.get(0).getLocations().length; } assertTrue(blocks.get(0).isCorrupt()); // Check if fsck reports the same outStr = runFsck(conf, 1, true, "/"); System.out.println(outStr); assertTrue(outStr.contains(NamenodeFsck.CORRUPT_STATUS)); assertTrue(outStr.contains("testCorruptBlock")); } finally { if (cluster != null) { cluster.shutdown(); } } }
From source file:com.kyne.webby.rtk.modules.WebbyRTKModule.java
public List<String> readConsoleLog(final LogMode logMode) { final List<String> logLines = new ArrayList<String>(); String line = ""; File logFile = null;/* w w w . ja va2 s . c om*/ if (logMode == LogMode.NEW) { logFile = new File("logs/latest.log"); } else if (logMode == LogMode.OLD) { logFile = new File("server.log"); } else { throw new UnsupportedOperationException("Unsupported log mode " + logMode); } if (!logFile.exists()) { LogHelper.error("Unable to find the log file at " + logFile.getAbsolutePath()); return Arrays.asList("Unable to find the log file"); } RandomAccessFile randomFile = null; try { randomFile = new RandomAccessFile(logFile, "r"); final long linesToRead = 100; final long fileLength = randomFile.length(); long startPosition = fileLength - (linesToRead * 100); if (startPosition < 0) { startPosition = 0; } randomFile.seek(startPosition); while ((line = randomFile.readLine()) != null) { logLines.add(line.replace("[0;30;22m", "").replace("[0;34;22m", "").replace("[0;32;22m", "") .replace("[0;36;22m", "").replace("[0;31;22m", "").replace("[0;35;22m", "") .replace("[0;33;22m", "").replace("[0;37;22m", "").replace("[0;30;1m", "") .replace("[0;34;1m", "").replace("[0;32;1m", "").replace("[0;36;1m", "") .replace("[0;31;1m", "").replace("[0;35;1m", "").replace("[0;33;1m", "") .replace("[0;37;1m", "").replace("[m", "").replace("[5m", "").replace("[21m", "") .replace("[9m", "").replace("[4m", "").replace("[3m", "").replace("[0;39m", "") .replace("[0m", "")); } } catch (final IOException e) { LogHelper.error("Unable to read server.log", e); } finally { IOUtils.closeQuietly(randomFile); } return logLines; }
From source file:GridFDock.DataDistribute.java
public Status uploadFile(String remoteFile, File localFile, FTPClient ftpClient, long remoteSize) throws IOException { long step = localFile.length() / 100; long process = 0; long localreadbytes = 0L; boolean tmp2 = true; Status result = null;/*from w ww. j av a2 s . c om*/ ftpClient.setFileType(FTP.BINARY_FILE_TYPE); while (tmp2) { RandomAccessFile raf = new RandomAccessFile(localFile, "r"); OutputStream out = ftpClient.appendFileStream(new String(remoteFile.getBytes(CODING_1), CODING_2)); if (remoteSize > 0) { ftpClient.setRestartOffset(remoteSize); process = remoteSize / step; raf.seek(remoteSize); localreadbytes = remoteSize; } byte[] bytes = new byte[1024]; int c; while ((c = raf.read(bytes)) != -1) { out.write(bytes, 0, c); localreadbytes += c; if (localreadbytes / step != process) { process = localreadbytes / step; // System.out.println("Upload Progress" + process); } } out.flush(); raf.close(); out.close(); boolean judge = ftpClient.completePendingCommand(); if (judge) { result = Status.Upload_From_Break_Success; tmp2 = false; } else { result = Status.Upload_New_File_Failed; } } return result; }
From source file:org.apache.flume.tools.TestFileChannelIntegrityTool.java
public void doTestFixCorruptEvents(boolean withCheckpoint) throws Exception { Set<String> corruptFiles = new HashSet<String>(); File[] files = dataDir.listFiles(new FilenameFilter() { @Override// w w w.ja va 2 s . com public boolean accept(File dir, String name) { if (name.contains("lock") || name.contains("meta")) { return false; } return true; } }); Random random = new Random(); int corrupted = 0; for (File dataFile : files) { LogFile.SequentialReader reader = new LogFileV3.SequentialReader(dataFile, null); RandomAccessFile handle = new RandomAccessFile(dataFile, "rw"); long eventPosition1 = reader.getPosition(); LogRecord rec = reader.next(); //No point corrupting commits, so ignore them if (rec == null || rec.getEvent().getClass().getName().equals("org.apache.flume.channel.file.Commit")) { handle.close(); reader.close(); continue; } long eventPosition2 = reader.getPosition(); rec = reader.next(); handle.seek(eventPosition1 + 100); handle.writeInt(random.nextInt()); corrupted++; corruptFiles.add(dataFile.getName()); if (rec == null || rec.getEvent().getClass().getName().equals("org.apache.flume.channel.file.Commit")) { handle.close(); reader.close(); continue; } handle.seek(eventPosition2 + 100); handle.writeInt(random.nextInt()); corrupted++; handle.close(); reader.close(); } FileChannelIntegrityTool tool = new FileChannelIntegrityTool(); tool.run(new String[] { "-l", dataDir.toString() }); FileChannel channel = new FileChannel(); channel.setName("channel"); String cp; if (withCheckpoint) { cp = origCheckpointDir.toString(); } else { FileUtils.deleteDirectory(checkpointDir); Assert.assertTrue(checkpointDir.mkdirs()); cp = checkpointDir.toString(); } ctx.put(FileChannelConfiguration.CHECKPOINT_DIR, cp); ctx.put(FileChannelConfiguration.DATA_DIRS, dataDir.toString()); channel.configure(ctx); channel.start(); Transaction tx = channel.getTransaction(); tx.begin(); int i = 0; while (channel.take() != null) { i++; } tx.commit(); tx.close(); channel.stop(); Assert.assertEquals(25 - corrupted, i); files = dataDir.listFiles(new FilenameFilter() { @Override public boolean accept(File dir, String name) { if (name.contains(".bak")) { return true; } return false; } }); Assert.assertEquals(corruptFiles.size(), files.length); for (File file : files) { String name = file.getName(); name = name.replaceAll(".bak", ""); Assert.assertTrue(corruptFiles.remove(name)); } Assert.assertTrue(corruptFiles.isEmpty()); }
From source file:com.skin.generator.action.UploadTestAction.java
/** * @throws IOException/*from w ww . ja v a2 s .c o m*/ * @throws ServletException */ @UrlPattern("/upload/test2.html") public void test2() throws IOException, ServletException { logger.info("method: " + this.request.getMethod() + " " + this.request.getRequestURI() + " " + this.request.getQueryString()); /** * cross domain support */ this.doOptions(this.request, this.response); /** * preflight */ if (this.request.getMethod().equalsIgnoreCase("OPTIONS")) { logger.info("options: " + this.request.getHeader("Origin")); return; } String home = this.servletContext.getRealPath("/WEB-INF/tmp"); Map<String, Object> result = new HashMap<String, Object>(); RandomAccessFile raf = null; InputStream inputStream = null; try { Map<String, Object> map = this.parse(this.request); int start = Integer.parseInt((String) map.get("start")); int end = Integer.parseInt((String) map.get("end")); int length = Integer.parseInt((String) map.get("length")); FileItem fileItem = (FileItem) map.get("fileData"); inputStream = fileItem.getInputStream(); raf = new RandomAccessFile(new File(home, fileItem.getName()), "rw"); raf.seek(start); String partMd5 = copy(inputStream, raf, 4096); if (end >= length) { String fileMd5 = Hex.encode(Digest.md5(new File(home, fileItem.getName()))); result.put("fileMd5", fileMd5); } result.put("status", 200); result.put("message", "??"); result.put("start", end); result.put("partMD5", partMd5); JsonUtil.callback(this.request, this.response, result); return; } catch (Exception e) { logger.error(e.getMessage(), e); result.put("status", 500); result.put("message", "?"); JsonUtil.callback(this.request, this.response, result); return; } finally { close(raf); close(inputStream); } }
From source file:com.funambol.foundation.util.FileSystemDAOHelper.java
/** * Writes the InputStream in append to the file starting from the given * position. If the file size does not match the expected value an * EOFException is thrown./*from w ww . j a v a 2 s. c om*/ * * @param f the file to open in writing * @param in the InputStream to copy in the file * @param pos the file-pointer offset * @param expectedSize the expected file size * @throws IOException if an error occurs */ public void copyInRandomAccessFile(File f, InputStream in, long pos, long expectedSize) throws IOException { // // open file for reading and writing: also its metadata will be updated // RandomAccessFile raf = new RandomAccessFile(f, "rws"); raf.seek(pos); byte[] buffer = new byte[1024]; long count = 0; while (true) { int read = in.read(buffer); if (read < 0) { break; } count += read; if (expectedSize >= 0 && count > expectedSize) { throw new EOFException("More bytes (" + count + ") than expected (" + expectedSize + ")"); } raf.write(buffer, 0, read); } raf.close(); if (expectedSize >= 0 && count < expectedSize) { throw new EOFException("Less bytes (" + count + ") than expected (" + expectedSize + ")"); } }