List of usage examples for java.io RandomAccessFile close
public void close() throws IOException
From source file:com.grarak.kerneladiutor.utils.kernel.cpu.CPUFreq.java
private static Usage[] getUsages() { try {// w w w .j a va 2 s . co m RandomAccessFile reader = new RandomAccessFile("/proc/stat", "r"); Usage[] usage = new Usage[getCpuCount() + 1]; for (int i = 0; i < usage.length; i++) usage[i] = new Usage(reader.readLine()); reader.close(); return usage; } catch (FileNotFoundException e) { Log.i(TAG, "/proc/stat does not exist"); } catch (IOException e) { e.printStackTrace(); } return null; }
From source file:com.bristle.javalib.io.FileUtil.java
/************************************************************************** * Copy the contents of the specified binary file to the specified Writer. *@param strFilename Name of the file to copy. *@param writer Writer to write to. *@throws FileNotFoundException//w w w.j av a2 s. c o m * When binary file not found. *@throws IOException When an I/O error occurs reading the file or * writing it to the Writer. **************************************************************************/ public static void copyBinaryFileToWriter(String strFilename, Writer writer) throws FileNotFoundException, IOException { RandomAccessFile file = new RandomAccessFile(strFilename, "r"); try { int i; while (-1 != (i = file.read())) { writer.write((char) i); } writer.flush(); } finally { file.close(); } }
From source file:org.apache.jackrabbit.oak.run.SegmentUtils.java
static void compact(File directory, boolean force) throws IOException { FileStore store = openFileStore(directory.getAbsolutePath(), force); try {/*from w w w . ja v a2 s . c o m*/ boolean persistCM = Boolean.getBoolean("tar.PersistCompactionMap"); CompactionStrategy compactionStrategy = new CompactionStrategy(false, CompactionStrategy.CLONE_BINARIES_DEFAULT, CompactionStrategy.CleanupType.CLEAN_ALL, 0, CompactionStrategy.MEMORY_THRESHOLD_DEFAULT) { @Override public boolean compacted(Callable<Boolean> setHead) throws Exception { // oak-run is doing compaction single-threaded // hence no guarding needed - go straight ahead // and call setHead return setHead.call(); } }; compactionStrategy.setOfflineCompaction(true); compactionStrategy.setPersistCompactionMap(persistCM); store.setCompactionStrategy(compactionStrategy); store.compact(); } finally { store.close(); } System.out.println(" -> cleaning up"); store = openFileStore(directory.getAbsolutePath(), false); try { for (File file : store.cleanup()) { if (!file.exists() || file.delete()) { System.out.println(" -> removed old file " + file.getName()); } else { System.out.println(" -> failed to remove old file " + file.getName()); } } String head; File journal = new File(directory, "journal.log"); JournalReader journalReader = new JournalReader(journal); try { head = journalReader.iterator().next() + " root " + System.currentTimeMillis() + "\n"; } finally { journalReader.close(); } RandomAccessFile journalFile = new RandomAccessFile(journal, "rw"); try { System.out.println(" -> writing new " + journal.getName() + ": " + head); journalFile.setLength(0); journalFile.writeBytes(head); journalFile.getChannel().force(false); } finally { journalFile.close(); } } finally { store.close(); } }
From source file:com.egt.core.util.JS.java
public static String getOpenFileWindowJavaScript(String spec) { Bitacora.trace(JS.class, "getOpenFileWindowJavaScript", spec); VelocityContext context = new VelocityContext(); context.put("url", StringUtils.trimToNull(spec)); context.put("msg", "Recurso no disponible"); /* TODO: obtener de una tabla de mensajes */ if (StringUtils.isNotBlank(spec)) { RandomAccessFile input; try {/*from www. j av a 2 s. c o m*/ URL url = new URL(spec); // throws MalformedURLException File file = new File(Utils.getAttachedFileName(url)); if (file.exists()) { input = new RandomAccessFile(file, "r"); // throws FileNotFoundException ImageInfo imageInfo = new ImageInfo(); imageInfo.setInput(input); // input can be InputStream or RandomAccessFile imageInfo.setDetermineImageNumber(false); // default is false imageInfo.setCollectComments(false); // default is false if (imageInfo.check()) { context.put("imageInfo", imageInfo); } input.close(); // throws IOException } } catch (MalformedURLException ex) { Bitacora.logFatal(ex); } catch (FileNotFoundException ex) { Bitacora.logFatal(ex); } catch (IOException ex) { Bitacora.logFatal(ex); } } return merge("js-open-window", context); }
From source file:org.apache.jackrabbit.oak.run.SegmentTarUtils.java
static void compact(File directory, boolean force) throws IOException { FileStore store = newFileStoreBuilder(directory.getAbsolutePath(), force) .withGCOptions(defaultGCOptions().setOffline()).build(); try {/*from w w w. ja v a 2 s . c o m*/ store.compact(); } finally { store.close(); } System.out.println(" -> cleaning up"); store = newFileStoreBuilder(directory.getAbsolutePath(), force) .withGCOptions(defaultGCOptions().setOffline()).build(); try { for (File file : store.cleanup()) { if (!file.exists() || file.delete()) { System.out.println(" -> removed old file " + file.getName()); } else { System.out.println(" -> failed to remove old file " + file.getName()); } } String head; File journal = new File(directory, "journal.log"); JournalReader journalReader = new JournalReader(journal); try { head = journalReader.next() + " root " + System.currentTimeMillis() + "\n"; } finally { journalReader.close(); } RandomAccessFile journalFile = new RandomAccessFile(journal, "rw"); try { System.out.println(" -> writing new " + journal.getName() + ": " + head); journalFile.setLength(0); journalFile.writeBytes(head); journalFile.getChannel().force(false); } finally { journalFile.close(); } } finally { store.close(); } }
From source file:org.apache.storm.utils.ServerUtils.java
/** * Given a zip File input it will return its size * Only works for zip files whose uncompressed size is less than 4 GB, * otherwise returns the size module 2^32, per gzip specifications * @param myFile The zip file as input//w ww .jav a2 s . co m * @throws IOException * @return zip file size as a long */ public static long zipFileSize(File myFile) throws IOException { RandomAccessFile raf = new RandomAccessFile(myFile, "r"); raf.seek(raf.length() - 4); long b4 = raf.read(); long b3 = raf.read(); long b2 = raf.read(); long b1 = raf.read(); long val = (b1 << 24) | (b2 << 16) + (b3 << 8) + b4; raf.close(); return val; }
From source file:de.ailis.wlandsuite.pics.Pics.java
/** * Returns the offsets of the base frame MSQ blocks in the specified file. * The offsets are determined by reading the raw data of each block and * looking at the current position in the file. * * @param file/*ww w. j a v a2 s . c o m*/ * The file * @return The offsets * @throws IOException * When file operation fails. */ public static List<Integer> getMsqOffsets(final File file) throws IOException { List<Integer> offsets; RandomAccessFile access; FileInputStream stream; MsqHeader header; long offset; byte[] dummy; boolean baseFrame = true; HuffmanInputStream huffmanStream; offsets = new ArrayList<Integer>(); access = new RandomAccessFile(file, "r"); try { stream = new FileInputStream(access.getFD()); offset = 0; while ((header = MsqHeader.read(stream)) != null) { if (baseFrame) { offsets.add(Integer.valueOf((int) offset)); } baseFrame = !baseFrame; huffmanStream = new HuffmanInputStream(stream); dummy = new byte[header.getSize()]; huffmanStream.read(dummy); offset = access.getFilePointer(); } } finally { access.close(); } return offsets; }
From source file:de.tuebingen.uni.sfs.germanet.api.GermaNet.java
/** * Checks whether the <code>File</code> is a <code>ZipFile</code>. * @param file the <code>File</code> to check * @return true if this <code>File</code> is a <code>ZipFile</code> * @throws javax.xml.stream.IOException/*from w w w . j a v a2 s . c o m*/ */ protected static boolean isZipFile(File file) throws IOException { RandomAccessFile raf = new RandomAccessFile(file, "r"); long n = raf.readInt(); raf.close(); if (n == 0x504B0304) { return true; } else { return false; } }
From source file:org.apache.jackrabbit.oak.plugins.segment.file.TarReader.java
/** * Collects all entries from the given file and optionally backs-up the * file, by renaming it to a ".bak" extension * /*from w w w.ja v a 2 s . c o m*/ * @param file * @param entries * @param backup * @throws IOException */ private static void collectFileEntries(File file, LinkedHashMap<UUID, byte[]> entries, boolean backup) throws IOException { log.info("Recovering segments from tar file {}", file); try { RandomAccessFile access = new RandomAccessFile(file, "r"); try { recoverEntries(file, access, entries); } finally { access.close(); } } catch (IOException e) { log.warn("Could not read tar file " + file + ", skipping...", e); } if (backup) { backupSafely(file); } }
From source file:cerrla.Performance.java
/** * Reads a raw numerical performance file and stores the values as * accessible private values.// ww w .j a v a2 s .co m * * @param perfFile * The performance file to read. * @return True if the file was read successfully, false otherwise. */ public static boolean readRawPerformanceFile(File perfFile, boolean byEpisode) throws Exception { if (Config.getInstance().getGeneratorFile() == null) { // First, read the last line of the normal file for the time RandomAccessFile raf = new RandomAccessFile(perfFile, "r"); long pos = perfFile.length() - 1; StringBuffer line = new StringBuffer(); char c; boolean foundIt = false; do { raf.seek(pos); c = (char) raf.read(); foundIt |= Character.isDigit(c); line.append(c); pos--; } while (!foundIt || Character.isDigit(c) || c == ':'); raf.close(); String time = line.reverse().toString().trim(); String[] timeSplit = time.split(":"); runTime_ = (Long.parseLong(timeSplit[2]) + 60 * Long.parseLong(timeSplit[1]) + 3600 * Long.parseLong(timeSplit[0])) * 1000; } if (Config.getInstance().getGeneratorFile() == null) perfFile = new File(perfFile.getPath() + "raw"); else perfFile = new File(perfFile.getPath() + "greedy"); performanceMap_ = new TreeMap<Integer, Float[]>(); FileReader reader = new FileReader(perfFile); BufferedReader buf = new BufferedReader(reader); // For every value within the performance file String input = null; Float[] prevPerfs = null; while ((input = buf.readLine()) != null) { String[] vals = input.split("\t"); if (vals[PerformanceDetails.EPISODE.ordinal()].equals("Episode")) continue; Float[] perfs = new Float[PerformanceDetails.values().length]; int episode = 0; for (PerformanceDetails detail : PerformanceDetails.values()) { if (vals.length > detail.ordinal()) { if (!vals[detail.ordinal()].equals("null")) perfs[detail.ordinal()] = Float.parseFloat(vals[detail.ordinal()]); else if (detail.equals(PerformanceDetails.ELITEMEAN) && !vals[PerformanceDetails.ELITEMAX.ordinal()].equals("null")) perfs[detail.ordinal()] = Float.parseFloat(vals[PerformanceDetails.ELITEMAX.ordinal()]); else if (detail.equals(PerformanceDetails.ELITEMEAN) || detail.equals(PerformanceDetails.ELITEMAX)) perfs[detail.ordinal()] = Float.parseFloat(vals[PerformanceDetails.MEAN.ordinal()]); else if (prevPerfs != null) perfs[detail.ordinal()] = prevPerfs[detail.ordinal()]; } if (detail.equals(PerformanceDetails.EPISODE)) episode = perfs[detail.ordinal()].intValue(); } performanceMap_.put(episode, perfs); prevPerfs = perfs; } buf.close(); reader.close(); return true; }