List of usage examples for java.io RandomAccessFile read
public int read() throws IOException
From source file:com.haulmont.cuba.core.sys.LogControlImpl.java
protected void skipFirstLine(RandomAccessFile logFile) throws IOException { boolean eol = false; while (!eol) { switch (logFile.read()) { case -1://from w ww . jav a2 s . c o m case '\n': eol = true; break; case '\r': eol = true; long cur = logFile.getFilePointer(); if ((logFile.read()) != '\n') { logFile.seek(cur); } break; default: break; } } }
From source file:com.haulmont.cuba.core.sys.LogControlImpl.java
protected String readUtf8Line(RandomAccessFile logFile) throws IOException { int c = -1;//from w w w . j a v a 2 s. c o m boolean eol = false; ByteArrayOutputStream input = new ByteArrayOutputStream(); while (!eol) { switch (c = logFile.read()) { case -1: case '\n': eol = true; break; case '\r': eol = true; long cur = logFile.getFilePointer(); if ((logFile.read()) != '\n') { logFile.seek(cur); } break; default: input.write((byte) c); break; } } if ((c == -1) && (input.size() == 0)) { return null; } return new String(input.toByteArray(), StandardCharsets.UTF_8); }
From source file:org.apache.hadoop.hdfs.server.namenode.TestFSEditLogLoader.java
/** * Corrupt the byte at the given offset in the given file, * by subtracting 1 from it.//from w ww . ja v a 2 s. co m */ private void corruptByteInFile(File file, long offset) throws IOException { RandomAccessFile raf = new RandomAccessFile(file, "rw"); try { raf.seek(offset); int origByte = raf.read(); raf.seek(offset); raf.writeByte(origByte - 1); } finally { IOUtils.closeStream(raf); } }
From source file:com.dotmarketing.servlets.taillog.Tailer.java
/** * Version of readline() that returns null on EOF rather than a partial line. * @param reader the input file//w w w .j ava2s . co m * @return the line, or null if EOF reached before '\n' is seen. * @throws IOException if an error occurs. */ private String readLine(RandomAccessFile reader) throws IOException { StringBuffer sb = new StringBuffer(); int ch; boolean seenCR = false; while ((ch = reader.read()) != -1) { switch (ch) { case '\n': return sb.toString(); case '\r': seenCR = true; break; default: if (seenCR) { sb.append('\r'); seenCR = false; } sb.append((char) ch); // add character, not its ascii value } } return null; }
From source file:info.ajaxplorer.client.http.AjxpFileBody.java
public void writeTo(OutputStream out) { InputStream in;//from w w w . j a v a 2s . c o m try { if (this.chunkSize > 0) { //System.out.println("Uploading file part " + this.chunkIndex); RandomAccessFile raf = new RandomAccessFile(getFile(), "r"); int start = chunkIndex * this.chunkSize; int count = 0; int limit = chunkSize; if (chunkIndex == (totalChunks - 1)) { limit = lastChunkSize; } raf.seek(start); while (count < limit) { int byt = raf.read(); out.write(byt); count++; } raf.close(); //System.out.println("Sent " + count); } else { in = new FileInputStream(getFile()); byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } in.close(); } this.chunkIndex++; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:hoot.services.info.ErrorLog.java
public String getErrorlog(long maxLength) throws Exception { File file = new File(_errLogPath); RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r"); int lines = 0; StringBuilder builder = new StringBuilder(); long length = file.length(); //length--;//w w w. jav a 2 s . co m long startOffset = 0; if (length > maxLength) { startOffset = length - maxLength; } for (long seek = startOffset; seek < length; seek++) { randomAccessFile.seek(seek); char c = (char) randomAccessFile.read(); builder.append(c); } randomAccessFile.close(); return builder.toString(); }
From source file:org.mitre.mpf.mvc.util.tailer.MpfLogTailer.java
/** * Read new lines.//from ww w .ja v a 2s . c om * * @param reader The file to read * @param maxLines The maximum number of lines to read * @return The number of lines read * @throws IOException if an I/O error occurs. */ private int readLines(RandomAccessFile reader, int maxLines) throws IOException { int numLines = 0; StringBuilder sb = new StringBuilder(); long pos = reader.getFilePointer(); long rePos = pos; // position to re-read byte ch; boolean seenCR = false; while (((ch = (byte) (reader.read())) != -1) && (numLines < maxLines)) { switch (ch) { case '\n': seenCR = false; // swallow CR before LF if (listener.handle(sb.toString())) { numLines++; } sb.setLength(0); rePos = pos + 1; break; case '\r': if (seenCR) { sb.append('\r'); } seenCR = true; break; default: if (seenCR) { seenCR = false; // swallow final CR if (listener.handle(sb.toString())) { numLines++; } sb.setLength(0); rePos = pos + 1; } sb.append((char) ch); // add character, not its ascii value } pos = reader.getFilePointer(); } reader.seek(rePos); // Ensure we can re-read if necessary position = rePos; return numLines; }
From source file:dk.netarkivet.common.utils.FileUtils.java
/** * Read the last line in a file. Note this method is not UTF-8 safe. * * @param file input file to read last line from. * @return The last line in the file (ending newline is irrelevant), * returns an empty string if file is empty. * @throws ArgumentNotValid on null argument, or file is not a readable * file./* w w w . j a v a 2s. c o m*/ * @throws IOFailure on IO trouble reading file. */ public static String readLastLine(File file) { ArgumentNotValid.checkNotNull(file, "File file"); if (!file.isFile() || !file.canRead()) { final String errMsg = "File '" + file.getAbsolutePath() + "' is not a readable file."; log.warn(errMsg); throw new ArgumentNotValid(errMsg); } if (file.length() == 0) { return ""; } RandomAccessFile rafile = null; try { rafile = new RandomAccessFile(file, "r"); //seek to byte one before end of file (remember we know the file is // not empty) - this ensures that an ending newline is not read rafile.seek(rafile.length() - 2); //now search to the last linebreak, or beginning of file while (rafile.getFilePointer() != 0 && rafile.read() != '\n') { //search back two, because we just searched forward one to find //newline rafile.seek(rafile.getFilePointer() - 2); } return rafile.readLine(); } catch (IOException e) { final String errMsg = "Unable to access file '" + file.getAbsolutePath() + "'"; log.warn(errMsg, e); throw new IOFailure(errMsg, e); } finally { try { if (rafile != null) { rafile.close(); } } catch (IOException e) { log.debug("Unable to close file '" + file.getAbsolutePath() + "' after reading", e); } } }
From source file:com.haulmont.cuba.core.sys.LogControlImpl.java
@Override public String getTail(String fileName) throws LogControlException { // security check, supported only valid file names fileName = FilenameUtils.getName(fileName); StringBuilder sb = new StringBuilder(); RandomAccessFile randomAccessFile = null; try {/*from w w w.ja va2s. c om*/ File logFile = new File(logDir, fileName); if (!logFile.exists()) throw new LogFileNotFoundException(fileName); randomAccessFile = new RandomAccessFile(logFile, "r"); long lengthFile = randomAccessFile.length(); if (lengthFile >= LOG_TAIL_AMOUNT_BYTES) { randomAccessFile.seek(lengthFile - LOG_TAIL_AMOUNT_BYTES); skipFirstLine(randomAccessFile); } while (randomAccessFile.read() != -1) { randomAccessFile.seek(randomAccessFile.getFilePointer() - 1); String line = readUtf8Line(randomAccessFile); if (line != null) { sb.append(line).append("\n"); } } } catch (IOException e) { log.error("Error reading log file", e); throw new LogControlException("Error reading log file: " + fileName); } finally { if (randomAccessFile != null) { try { randomAccessFile.close(); } catch (IOException ignored) { } } } return sb.toString(); }
From source file:org.ala.spatial.util.AnalysisJobMaxent.java
private String getMaxentError(File file, int count) { try {//from w ww .j ava2s. c om RandomAccessFile rf = new RandomAccessFile(file, "r"); // first check if maxent threw a 'No species selected' error String nosp = rf.readLine(); // first line: date/time nosp = rf.readLine(); // second line: maxent version nosp = rf.readLine(); // third line: "No species selected" if (nosp.equals("No species selected")) { return "No species selected"; } long flen = file.length() - 1; int nlcnt = -1; StringBuilder lines = new StringBuilder(); while (nlcnt != count) { rf.seek(flen--); char c = (char) rf.read(); lines.append(c); if (c == '\n') { nlcnt++; } } String line = lines.reverse().toString(); if (line.contains("Warning: Skipping species because it has 0 test samples")) { return "Warning: Skipping species because it has 0 test samples"; } rf.close(); } catch (Exception e) { System.out.println("Unable to read lines"); e.printStackTrace(System.out); } // return false anyways return null; }