List of usage examples for java.io RandomAccessFile readLine
public final String readLine() throws IOException
From source file:com.redhat.rhn.frontend.action.common.DownloadFile.java
@Override protected StreamInfo getStreamInfo(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { String path = ""; Map params = (Map) request.getAttribute(PARAMS); String type = (String) params.get(TYPE); if (type.equals(DownloadManager.DOWNLOAD_TYPE_KICKSTART)) { return getStreamInfoKickstart(mapping, form, request, response, path); } else if (type.equals(DownloadManager.DOWNLOAD_TYPE_COBBLER)) { String url = ConfigDefaults.get().getCobblerServerUrl() + (String) params.get(URL_STRING); KickstartHelper helper = new KickstartHelper(request); String data = ""; if (helper.isProxyRequest()) { data = KickstartManager.getInstance().renderKickstart(helper.getKickstartHost(), url); } else {//from www . j a va 2 s . co m data = KickstartManager.getInstance().renderKickstart(url); } setTextContentInfo(response, data.length()); return getStreamForText(data.getBytes()); } else if (type.equals(DownloadManager.DOWNLOAD_TYPE_COBBLER_API)) { // read data from POST body String postData = new String(); String line = null; BufferedReader reader = request.getReader(); while ((line = reader.readLine()) != null) { postData += line; } // Send data URL url = new URL(ConfigDefaults.get().getCobblerServerUrl() + "/cobbler_api"); URLConnection conn = url.openConnection(); conn.setDoOutput(true); OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); // this will write POST /download//cobbler_api instead of // POST /cobbler_api, but cobbler do not mind wr.write(postData, 0, postData.length()); wr.flush(); conn.connect(); // Get the response String output = new String(); BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); while ((line = rd.readLine()) != null) { output += line; } wr.close(); KickstartHelper helper = new KickstartHelper(request); if (helper.isProxyRequest()) { // Search/replacing all instances of cobbler host with host // we pass in, for use with Spacewalk Proxy. output = output.replaceAll(ConfigDefaults.get().getCobblerHost(), helper.getForwardedHost()); } setXmlContentInfo(response, output.length()); return getStreamForXml(output.getBytes()); } else { Long fileId = (Long) params.get(FILEID); Long userid = (Long) params.get(USERID); User user = UserFactory.lookupById(userid); if (type.equals(DownloadManager.DOWNLOAD_TYPE_PACKAGE)) { Package pack = PackageFactory.lookupByIdAndOrg(fileId, user.getOrg()); setBinaryContentInfo(response, pack.getPackageSize().intValue()); path = Config.get().getString(ConfigDefaults.MOUNT_POINT) + "/" + pack.getPath(); return getStreamForBinary(path); } else if (type.equals(DownloadManager.DOWNLOAD_TYPE_SOURCE)) { Package pack = PackageFactory.lookupByIdAndOrg(fileId, user.getOrg()); List<PackageSource> src = PackageFactory.lookupPackageSources(pack); if (!src.isEmpty()) { setBinaryContentInfo(response, src.get(0).getPackageSize().intValue()); path = Config.get().getString(ConfigDefaults.MOUNT_POINT) + "/" + src.get(0).getPath(); return getStreamForBinary(path); } } else if (type.equals(DownloadManager.DOWNLOAD_TYPE_REPO_LOG)) { Channel c = ChannelFactory.lookupById(fileId); ChannelManager.verifyChannelAdmin(user, fileId); StringBuilder output = new StringBuilder(); for (String fileName : ChannelManager.getLatestSyncLogFiles(c)) { RandomAccessFile file = new RandomAccessFile(fileName, "r"); long fileLength = file.length(); if (fileLength > DOWNLOAD_REPO_LOG_LENGTH) { file.seek(fileLength - DOWNLOAD_REPO_LOG_LENGTH); // throw away text till end of the actual line file.readLine(); } else { file.seek(0); } String line; while ((line = file.readLine()) != null) { output.append(line); output.append("\n"); } file.close(); if (output.length() > DOWNLOAD_REPO_LOG_MIN_LENGTH) { break; } } setTextContentInfo(response, output.length()); return getStreamForText(output.toString().getBytes()); } else if (type.equals(DownloadManager.DOWNLOAD_TYPE_CRASHFILE)) { CrashFile crashFile = CrashManager.lookupCrashFileByUserAndId(user, fileId); String crashPath = crashFile.getCrash().getStoragePath(); setBinaryContentInfo(response, (int) crashFile.getFilesize()); path = Config.get().getString(ConfigDefaults.MOUNT_POINT) + "/" + crashPath + "/" + crashFile.getFilename(); return getStreamForBinary(path); } } throw new UnknownDownloadTypeException( "The specified download type " + type + " is not currently supported"); }
From source file:org.opencb.cellbase.mongodb.db.VariantAnnotationMongoDBAdaptorTest.java
private int getVepAnnotationBatch(RandomAccessFile raf, int nVariantsToRead, Set<AnnotationComparisonObject> vepAnnotationSet) throws IOException { /**// ww w .j ava 2 s . c o m * Loads VEP annotation */ String newLine; int nNonRegulatoryAnnotations = 0; int nReadVariants = 0; String previousChr = ""; String previousPosition = ""; String previousAlt = ""; String alt; long filePointer = 0; if (nVariantsToRead > 0) { while (((newLine = raf.readLine()) != null) && nReadVariants <= nVariantsToRead) { String[] lineFields = newLine.split("\t"); String[] coordinatesParts = lineFields[1].split(":"); if (lineFields[2].equals("deletion")) { alt = "-"; } else { alt = lineFields[2]; } if (!previousChr.equals(coordinatesParts[0]) || !previousPosition.equals(coordinatesParts[1]) || !previousAlt.equals(alt)) { nReadVariants++; } if (nReadVariants <= nVariantsToRead) { for (String SOname : lineFields[6].split(",")) { if (SOname.equals("nc_transcript_variant")) { SOname = "non_coding_transcript_variant"; } if (!SOname.equals("regulatory_region_variant")) { nNonRegulatoryAnnotations++; } vepAnnotationSet.add(new AnnotationComparisonObject(coordinatesParts[0], coordinatesParts[1], alt, lineFields[3], lineFields[4], SOname)); } previousChr = coordinatesParts[0]; previousPosition = coordinatesParts[1]; previousAlt = alt; filePointer = raf.getFilePointer(); } } raf.seek(filePointer); } return nNonRegulatoryAnnotations; }
From source file:pandroid.agent.PandroidAgentListener.java
private void getMemoryStatus() { String memoryStatus = getSharedData("PANDROID_DATA", "memoryStatus", Core.defaultMemoryStatus, "string"); long availableRamKb = 0; long totalRamKb = 0; if (memoryStatus.equals("enabled")) { MemoryInfo mi = new MemoryInfo(); ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE); activityManager.getMemoryInfo(mi); availableRamKb = mi.availMem / 1024; totalRamKb = 0;/*from w ww . j av a2 s . com*/ try { RandomAccessFile reader = new RandomAccessFile("/proc/meminfo", "r"); String line = reader.readLine(); reader.close(); line = line.replaceAll("[ ]+", " "); String[] tokens = line.split(" "); totalRamKb = Long.valueOf(tokens[1]); } catch (IOException ex) { ex.printStackTrace(); } } putSharedData("PANDROID_DATA", "availableRamKb", "" + availableRamKb, "long"); putSharedData("PANDROID_DATA", "totalRamKb", "" + totalRamKb, "long"); }
From source file:dk.statsbiblioteket.util.LineReaderTest.java
public void testConstruction() throws Exception { assertTrue("The logfile " + logfile.getAbsoluteFile() + " should exist", logfile.exists()); RandomAccessFile rReader = new RandomAccessFile(logfile, "r"); LineReader lReader = new LineReader(logfile, "r"); assertEquals("The first line should match", "INFO [TP-Processor128] [2007-04-01 00:00:00,109] [website.performance.search_classic] FEBBCAC5ABBA604784A4990025CF0197|hitcount[9988]|searchwsc[1033]|clusterwsc[119]|didyoumeanwsc[342]|didyoumean_check[0]|page_render[1137]|66.249.66.193|Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)einstein lma_long:\"tekst\"", lReader.readLine());//from w ww . j a v a 2 s . com rReader.readLine(); String secondLine = "INFO [TP-Processor185] [2007-04-01 00:00:47,074] [website.performance.search_classic] 9BC1F1B16AAE36840B6A13C63F67B806|hitcount[214]|searchwsc[182]|clusterwsc[121]|didyoumeanwsc[420]|didyoumean_check[0]|page_render[879]|66.249.66.193|Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)einstein cluster:\"systems\""; assertEquals("The second line should match", secondLine, lReader.readLine()); assertEquals("The second RAF line should match", secondLine, fixISO(rReader.readLine())); for (int i = 0; i < LINES - 4; i++) { String l = lReader.readLine(); assertNotNull("Line " + i + " should be extractable from the logfile", l); assertEquals("Line #" + i + " should be the same for RAF and LR", fixISO(rReader.readLine()), l); } String secondLast = "INFO [TP-Processor225] [2007-04-01 23:59:32,128] [website.performance.search_cl" + "assic] 617D24C65E2F53E56E95FBADCF390189|hitcount[6]|searchwsc[42]|clusterwsc[95]" + "|didyoumeanwsc[382]|didyoumean_check[15]|page_render[475]|66.249.66.193|Mozilla/" + "5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)au:\"branner h c" + "\" author_normalised:\"vosmar j\" lkl:\"bl skr 33\""; String rLast = fixISO(rReader.readLine()); assertEquals("The second last line using RAF should be known", secondLast, fixISO(rLast)); assertEquals("The second last line should be known", secondLast, lReader.readLine()); assertNotNull("The last line should be something", lReader.readLine()); }
From source file:org.opencb.cellbase.lib.db.VariantAnnotationCalculatorTest.java
private int getVepAnnotationBatch(RandomAccessFile raf, int nVariantsToRead, Set<AnnotationComparisonObject> vepAnnotationSet) throws IOException { /**/*from w ww.ja va2 s . c om*/ * Loads VEP annotation */ String newLine; int nNonRegulatoryAnnotations = 0; int nReadVariants = 0; String previousChr = ""; String previousPosition = ""; String previousAlt = ""; String alt; long filePointer = 0; if (nVariantsToRead > 0) { while (((newLine = raf.readLine()) != null) && nReadVariants <= nVariantsToRead) { String[] lineFields = newLine.split("\t"); String[] coordinatesParts = lineFields[1].split(":"); if (lineFields[2].equals("deletion")) { alt = "-"; } else { alt = lineFields[2]; } // TODO: Remove this if as refactoring implements consequence types for other variant types // if(!alt.equals("-") && coordinatesParts[1].split("-").length==1) { if (!previousChr.equals(coordinatesParts[0]) || !previousPosition.equals(coordinatesParts[1]) || !previousAlt.equals(alt)) { nReadVariants++; } if (nReadVariants <= nVariantsToRead) { for (String SOname : lineFields[6].split(",")) { if (SOname.equals("nc_transcript_variant")) { SOname = "non_coding_transcript_variant"; } if (!SOname.equals("regulatory_region_variant")) { nNonRegulatoryAnnotations++; } vepAnnotationSet.add(new AnnotationComparisonObject(coordinatesParts[0], coordinatesParts[1], alt, lineFields[3], lineFields[4], SOname)); } previousChr = coordinatesParts[0]; previousPosition = coordinatesParts[1]; previousAlt = alt; filePointer = raf.getFilePointer(); } // } } raf.seek(filePointer); } return nNonRegulatoryAnnotations; }
From source file:org.carewebframework.api.logging.LogFileTailer.java
/** * Typically executed via a <code>new Thread(FileTailer).start()</code> *//*from w ww . j a va2s .c o m*/ @Override public void run() { // The file pointer keeps track of where we are in the file long filePointer = 0; final long startTime = new Date().getTime(); // Determine start point if (this.startAtBeginning) { filePointer = 0; } else { filePointer = this.file.length(); } try { // Start tailing this.tailing = true; RandomAccessFile file = new RandomAccessFile(this.file, "r"); while (isTailing()) { //check to see if maxActiveInterval has been exceeded if (new Date().getTime() - startTime > this.maxActiveInterval) { if (log.isWarnEnabled()) { log.warn("FileTailer exceeded maxActiveInterval: " + this.maxActiveInterval); } stopTailing(); fireMaxActiveIntervalExceeded(); } try { // Compare the length of the file to the file pointer final long fileLength = this.file.length(); if (fileLength < filePointer) { // file must have been rotated or deleted; // reopen the file and reset the file pointer file = new RandomAccessFile(this.file, "r"); filePointer = 0; } if (fileLength > filePointer) { // There is data to read file.seek(filePointer); String line = file.readLine(); while (line != null) { fireNewFileLine(line); line = file.readLine(); } filePointer = file.getFilePointer(); } // Sleep for the specified interval Thread.sleep(this.interval); } catch (final Exception e) { log.error(e.getMessage(), e); } } // Close the file that we are tailing file.close(); } catch (final Exception e) { log.error(e.getMessage(), e); } }
From source file:org.apache.james.mailrepository.file.MBoxMailRepository.java
/** * Parse the mbox file./*w ww . ja v a 2s . co m*/ * * @param ins * The random access file to load. Note that the file may or may * not start at offset 0 in the file * @param messAct * The action to take when a message is found */ private MimeMessage parseMboxFile(RandomAccessFile ins, MessageAction messAct) { if ((getLogger().isDebugEnabled())) { String logBuffer = this.getClass().getName() + " Start parsing " + mboxFile; getLogger().debug(logBuffer); } try { Pattern sepMatchPattern = Pattern.compile("^From (.*) (.*):(.*):(.*)$"); int c; boolean inMessage = false; StringBuffer messageBuffer = new StringBuffer(); String previousMessageSeparator = null; boolean foundSep; long prevMessageStart = ins.getFilePointer(); if (BUFFERING) { String line; while ((line = ins.readLine()) != null) { foundSep = sepMatchPattern.matcher(line).matches(); if (foundSep && inMessage) { // if ((DEEP_DEBUG) && (getLogger().isDebugEnabled())) { // getLogger().debug(this.getClass().getName() + // " Invoking " + messAct.getClass() + " at " + // prevMessageStart); // } MimeMessage endResult = messAct.messageAction(previousMessageSeparator, messageBuffer.toString(), prevMessageStart); if (messAct.isComplete()) { // I've got what I want so just exit return endResult; } previousMessageSeparator = line; prevMessageStart = ins.getFilePointer() - line.length(); messageBuffer = new StringBuffer(); inMessage = true; } // Only done at the start (first header) if (foundSep && !inMessage) { previousMessageSeparator = line; inMessage = true; } if (!foundSep && inMessage) { messageBuffer.append(line).append("\n"); } } } else { StringBuffer line = new StringBuffer(); while ((c = ins.read()) != -1) { if (c == 10) { foundSep = sepMatchPattern.matcher(line).matches(); if (foundSep && inMessage) { // if ((DEEP_DEBUG) && // (getLogger().isDebugEnabled())) { // getLogger().debug(this.getClass().getName() + // " Invoking " + messAct.getClass() + " at " + // prevMessageStart); // } MimeMessage endResult = messAct.messageAction(previousMessageSeparator, messageBuffer.toString(), prevMessageStart); if (messAct.isComplete()) { // I've got what I want so just exit return endResult; } previousMessageSeparator = line.toString(); prevMessageStart = ins.getFilePointer() - line.length(); messageBuffer = new StringBuffer(); inMessage = true; } // Only done at the start (first header) if (foundSep && !inMessage) { previousMessageSeparator = line.toString(); inMessage = true; } if (!foundSep) { messageBuffer.append(line).append((char) c); } line = new StringBuffer(); // Reset buffer } else { line.append((char) c); } } } if (messageBuffer.length() != 0) { // process last message return messAct.messageAction(previousMessageSeparator, messageBuffer.toString(), prevMessageStart); } } catch (IOException ioEx) { getLogger().error("Unable to write file (General I/O problem) " + mboxFile, ioEx); } catch (PatternSyntaxException e) { getLogger().error("Bad regex passed " + mboxFile, e); } finally { if ((getLogger().isDebugEnabled())) { String logBuffer = this.getClass().getName() + " Finished parsing " + mboxFile; getLogger().debug(logBuffer); } } 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/*from w ww .j a v a 2 s . co m*/ * 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.cyberway.issue.crawler.fetcher.FetchHTTP.java
/** * Load cookies from a file before the first fetch. * <p>//from w ww .j a v a 2s.co m * The file is a text file in the Netscape's 'cookies.txt' file format.<br> * Example entry of cookies.txt file:<br> * <br> * www.archive.org FALSE / FALSE 1074567117 details-visit texts-cralond<br> * <br> * Each line has 7 tab-separated fields:<br> * <li>1. DOMAIN: The domain that created and have access to the cookie * value. * <li>2. FLAG: A TRUE or FALSE value indicating if hosts within the given * domain can access the cookie value. * <li>3. PATH: The path within the domain that the cookie value is valid * for. * <li>4. SECURE: A TRUE or FALSE value indicating if to use a secure * connection to access the cookie value. * <li>5. EXPIRATION: The expiration time of the cookie value (unix style.) * <li>6. NAME: The name of the cookie value * <li>7. VALUE: The cookie value * * @param cookiesFile file in the Netscape's 'cookies.txt' format. */ public void loadCookies(String cookiesFile) { // Do nothing if cookiesFile is not specified. if (cookiesFile == null || cookiesFile.length() <= 0) { return; } RandomAccessFile raf = null; try { raf = new RandomAccessFile(cookiesFile, "r"); String[] cookieParts; String line; Cookie cookie = null; while ((line = raf.readLine()) != null) { // Line that starts with # is commented line, therefore skip it. if (!line.startsWith("#")) { cookieParts = line.split("\\t"); if (cookieParts.length == 7) { // Create cookie with not expiration date (-1 value). // TODO: add this as an option. cookie = new Cookie(cookieParts[0], cookieParts[5], cookieParts[6], cookieParts[2], -1, Boolean.valueOf(cookieParts[3]).booleanValue()); if (cookieParts[1].toLowerCase().equals("true")) { cookie.setDomainAttributeSpecified(true); } else { cookie.setDomainAttributeSpecified(false); } this.http.getState().addCookie(cookie); logger.fine("Adding cookie: " + cookie.toExternalForm()); } } } } catch (FileNotFoundException e) { // We should probably throw FatalConfigurationException. System.out.println("Could not find file: " + cookiesFile + " (Element: " + ATTR_LOAD_COOKIES + ")"); } catch (IOException e) { // We should probably throw FatalConfigurationException. e.printStackTrace(); } finally { try { if (raf != null) { raf.close(); } } catch (IOException e) { e.printStackTrace(); } } }
From source file:org.archive.crawler.fetcher.OptimizeFetchHTTP.java
/** * Load cookies from a file before the first fetch. * <p>/*from w w w. jav a 2 s . c o m*/ * The file is a text file in the Netscape's 'cookies.txt' file format.<br> * Example entry of cookies.txt file:<br> * <br> * www.archive.org FALSE / FALSE 1074567117 details-visit texts-cralond<br> * <br> * Each line has 7 tab-separated fields:<br> * <li>1. DOMAIN: The domain that created and have access to the cookie * value. * <li>2. FLAG: A TRUE or FALSE value indicating if hosts within the given * domain can access the cookie value. * <li>3. PATH: The path within the domain that the cookie value is valid * for. * <li>4. SECURE: A TRUE or FALSE value indicating if to use a secure * connection to access the cookie value. * <li>5. EXPIRATION: The expiration time of the cookie value (unix style.) * <li>6. NAME: The name of the cookie value * <li>7. VALUE: The cookie value * * @param cookiesFile file in the Netscape's 'cookies.txt' format. */ public void loadCookies(String cookiesFile) { // Do nothing if cookiesFile is not specified. if (cookiesFile == null || cookiesFile.length() <= 0) { return; } RandomAccessFile raf = null; try { raf = new RandomAccessFile(cookiesFile, "r"); String[] cookieParts; String line; Cookie cookie = null; while ((line = raf.readLine()) != null) { // Line that starts with # is commented line, therefore skip it. if (!line.startsWith("#")) { cookieParts = line.split("\\t"); if (cookieParts.length == 7) { // Create cookie with not expiration date (-1 value). // TODO: add this as an option. cookie = new Cookie(cookieParts[0], cookieParts[5], cookieParts[6], cookieParts[2], -1, Boolean.valueOf(cookieParts[3]).booleanValue()); if (cookieParts[1].toLowerCase().equals("true")) { cookie.setDomainAttributeSpecified(true); } else { cookie.setDomainAttributeSpecified(false); } HttpClient http = this.getClient(); http.getState().addCookie(cookie); logger.debug("Adding cookie: " + cookie.toExternalForm()); } } } } catch (FileNotFoundException e) { // We should probably throw FatalConfigurationException. System.out.println("Could not find file: " + cookiesFile + " (Element: " + ATTR_LOAD_COOKIES + ")"); } catch (IOException e) { // We should probably throw FatalConfigurationException. e.printStackTrace(); } finally { try { if (raf != null) { raf.close(); } } catch (IOException e) { e.printStackTrace(); } } }