List of usage examples for java.io RandomAccessFile seek
public void seek(long pos) throws IOException
From source file:com.netflix.imfutility.itunes.image.ImageValidator.java
private static boolean checkBytes(File file, byte[] start, byte[] end) throws IOException { RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r"); byte[] destStart = new byte[start.length]; if (start.length != 0) { int res = randomAccessFile.read(destStart, 0, start.length); if (res == -1) { return false; }/* ww w.j av a2s . co m*/ } byte[] destEnd = new byte[end.length]; if (end.length != 0) { randomAccessFile.seek(file.length() - end.length); int res = randomAccessFile.read(destEnd, 0, end.length); if (res == -1) { return false; } } return Arrays.equals(start, destStart) && Arrays.equals(end, destEnd); }
From source file:fm.last.commons.io.LastFileUtils.java
/** * Reads the last bytes from the end of the passed file as a String. * //from w w w . j av a 2s .c o m * @param file File to read from. * @param bytes Number of bytes from end of file to read. * @return The end content of the file as a String. * @throws IOException If the file could not be opened or read. */ public static String tail(File file, long bytes) throws IOException { RandomAccessFile raFile = null; StringBuffer tail = new StringBuffer(); try { raFile = new RandomAccessFile(file, "r"); long length = raFile.length(); if (bytes >= length) { return FileUtils.readFileToString(file); } else { raFile.seek(length - bytes); tail = new StringBuffer((int) bytes); String line = raFile.readLine(); while (line != null) { tail.append(line); line = raFile.readLine(); if (line != null) { // there is another line coming, so add line break tail.append("\n"); } } } } finally { LastIoUtils.closeQuietly(raFile); } return tail.toString(); }
From source file:org.fusesource.meshkeeper.util.internal.FileSupport.java
static public byte[] read(File file, long pos, int length) throws IOException { RandomAccessFile is = new RandomAccessFile(file, "r"); try {//from www . ja v a2 s . c o m long remaining = is.length() - pos; if (remaining < 0) { remaining = 0; } byte rc[] = new byte[(int) Math.min(remaining, length)]; if (rc.length == 0) { return rc; } is.seek(pos); is.readFully(rc); return rc; } finally { is.close(); } }
From source file:net.sourceforge.doddle_owl.data.JpnWordNetDic.java
private static Concept getConcept(long dfp) { RandomAccessFile dataFile = null; try {/*from ww w. ja v a 2 s . c o m*/ dataFile = jpnwnConceptDataFile; dataFile.seek(dfp); String data = new String(dataFile.readLine().getBytes("ISO8859_1"), "UTF-8"); // System.out.println(data); String[] dataArray = data.split("\\^"); String[] conceptData = new String[4]; String id = dataArray[0].replaceAll("\t", ""); System.arraycopy(dataArray, 1, conceptData, 0, conceptData.length); String uri = ""; Concept c = null; uri = DODDLEConstants.JPN_WN_URI + id; c = new Concept(uri, conceptData); jpnwnURIConceptMap.put(uri, c); return c; } catch (IOException ioe) { ioe.printStackTrace(); } return null; }
From source file:net.sourceforge.doddle_owl.data.JpnWordNetDic.java
private static long getIndexFp(long fp) { RandomAccessFile indexFpListFile = null; indexFpListFile = jpnwnWordIndexFile; try {/*from w ww . j a v a 2s .c o m*/ indexFpListFile.seek(fp); String fpStr = indexFpListFile.readLine(); if (fpStr == null) { return -1; } return Long.valueOf(fpStr); } catch (IOException ioe) { ioe.printStackTrace(); } return -1; }
From source file:net.sourceforge.doddle_owl.data.JpnWordNetDic.java
private static String getData(long dfp, RandomAccessFile dataFile, String encoding) { try {/* w w w . ja va 2 s . c o m*/ // System.out.println("dfp: " + dfp); dataFile.seek(dfp); return new String(dataFile.readLine().getBytes("ISO8859_1"), encoding); } catch (IOException ioe) { ioe.printStackTrace(); } return null; }
From source file:net.sourceforge.doddle_owl.data.JpnWordNetDic.java
private static String getTermAndIndexFpSet(long ifp) { RandomAccessFile indexFile = null; indexFile = jpnwnWordDataFile;/* ww w . j a v a 2s .c o m*/ try { // System.out.println("ifp: " + ifp); indexFile.seek(ifp); return new String(indexFile.readLine().getBytes("ISO8859_1"), "UTF-8"); } catch (IOException ioe) { ioe.printStackTrace(); } return null; }
From source file:com.aol.advertising.qiao.util.CommonUtils.java
public static long checksum(RandomAccessFile raFile, int numBytes) throws IOException, InsufficientFileLengthException { CRC32 _crc = new CRC32(); long pos = raFile.getFilePointer(); try {//from w w w . j a v a 2 s.c om byte[] buffer = new byte[numBytes]; raFile.seek(0); int n = raFile.read(buffer); if (n < numBytes) { String s; logger.warn(s = ("not enough data for checksum: current file size=" + n)); throw new InsufficientFileLengthException(s); } synchronized (_crc) { _crc.reset(); _crc.update(buffer); return _crc.getValue(); } } finally { raFile.seek(pos); } }
From source file:org.apache.hadoop.hdfs.server.namenode.FSImageTestUtil.java
/** * Calculate the md5sum of an image after zeroing out the transaction ID * field in the header. This is useful for tests that want to verify * that two checkpoints have identical namespaces. *//* ww w .j a v a 2s.c om*/ public static String getImageFileMD5IgnoringTxId(File imageFile) throws IOException { File tmpFile = File.createTempFile("hadoop_imagefile_tmp", "fsimage"); tmpFile.deleteOnExit(); try { Files.copy(imageFile, tmpFile); RandomAccessFile raf = new RandomAccessFile(tmpFile, "rw"); try { raf.seek(IMAGE_TXID_POS); raf.writeLong(0); } finally { IOUtils.closeStream(raf); } return getFileMD5(tmpFile); } finally { tmpFile.delete(); } }
From source file:Main.java
public static double getCpuUsage1() { try {/*from w ww.ja v a 2 s.c om*/ RandomAccessFile reader = new RandomAccessFile("/proc/stat", "r"); String load = reader.readLine(); String[] toks = load.split(" "); double user1 = Double.parseDouble(toks[2]); double system1 = Double.parseDouble(toks[4]); double irq1 = Double.parseDouble(toks[7]); double idle1 = Double.parseDouble(toks[5]); try { Thread.sleep(360); } catch (Exception e) { e.printStackTrace(); } reader.seek(0); load = reader.readLine(); reader.close(); toks = load.split(" "); double user2 = Double.parseDouble(toks[2]); double system2 = Double.parseDouble(toks[4]); double irq2 = Double.parseDouble(toks[7]); double idle2 = Double.parseDouble(toks[5]); double user_pass = user2 - user1; double system_pass = system2 - system1; double irq_pass = irq2 - irq1; double idle_pass = idle2 - idle1; double usage = (user_pass + system_pass + irq_pass) * 100.00 / (user_pass + irq_pass + system_pass + idle_pass); BigDecimal b = new BigDecimal(usage); double res = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); return res; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } return 0; }