List of usage examples for java.io LineNumberReader LineNumberReader
public LineNumberReader(Reader in)
From source file:com.puppycrawl.tools.checkstyle.api.Utils.java
/** * Loads the contents of a file in a String array using * the named charset./* w ww . jav a2 s . co m*/ * @return the lines in the file * @param fileName the name of the file to load * @param charsetName the name of a supported charset * @throws IOException error occurred * @deprecated consider using {@link FileText} instead **/ @Deprecated public static String[] getLines(String fileName, String charsetName) throws IOException { final List<String> lines = Lists.newArrayList(); final FileInputStream fr = new FileInputStream(fileName); LineNumberReader lnr = null; try { lnr = new LineNumberReader(new InputStreamReader(fr, charsetName)); } catch (final UnsupportedEncodingException ex) { fr.close(); final String message = "unsupported charset: " + ex.getMessage(); throw new UnsupportedEncodingException(message); } try { while (true) { final String l = lnr.readLine(); if (l == null) { break; } lines.add(l); } } finally { Utils.closeQuietly(lnr); } return lines.toArray(new String[lines.size()]); }
From source file:org.kalypso.dwd.DWDRasterHelper.java
public static DWDRasterGeoLayer loadGeoRaster(final URL url, final String targetEpsg) throws Exception { LineNumberReader reader = null; try {/*from w w w . ja v a 2 s. co m*/ reader = new LineNumberReader(new InputStreamReader(url.openStream())); String line = null; DWDRaster raster = null; DWDRaster xRaster = null; DWDRaster yRaster = null; final double factor = DWDRasterHelper.getFactorForDwdKey(DWDRaster.KEY_100000_LAT); final double offset = DWDRasterHelper.getOffsetForDwdKey(DWDRaster.KEY_100000_LAT); while ((line = reader.readLine()) != null) { final Matcher staticHeaderMatcher = HEADER_STATIC.matcher(line); if (staticHeaderMatcher.matches()) { if (raster != null && raster.getKey() == DWDRaster.KEY_100000_LAT) yRaster = raster; if (raster != null && raster.getKey() == DWDRaster.KEY_100000_LON) xRaster = raster; final Date date = DATEFORMAT_RASTER.parse(staticHeaderMatcher.group(1)); final int key = Integer.parseInt(staticHeaderMatcher.group(2)); raster = new DWDRaster(date, key); continue; } final String[] values = (line.trim()).split(" +", 13); if (raster != null) { for (final String value : values) raster.addValue((Double.parseDouble(value) + offset) * factor); } } if (raster != null && raster.getKey() == DWDRaster.KEY_100000_LAT) yRaster = raster; if (raster != null && raster.getKey() == DWDRaster.KEY_100000_LON) xRaster = raster; return new DWDRasterGeoLayer(targetEpsg, xRaster, yRaster); } finally { IOUtils.closeQuietly(reader); } }
From source file:org.kalypso.model.wspm.tuhh.schema.simulation.QRelationFileReader.java
public void read(final File inputFile) throws IOException { final String filename = inputFile.getName(); LineNumberReader reader = null; try {/*from w w w.j a v a2s . c o m*/ reader = new LineNumberReader(new FileReader(inputFile)); while (reader.ready()) { final String line = reader.readLine(); if (line == null) break; final int lineNumber = reader.getLineNumber(); readLine(line, lineNumber, filename); } reader.close(); m_qresult.setPointsObservation(m_observation); } finally { IOUtils.closeQuietly(reader); } }
From source file:org.kalypso.kalypsomodel1d2d.sim.IterationInfoSWAN.java
@Override public void readIterFile() throws IOException { m_itrFile.refresh();/*from ww w . ja v a 2s.co m*/ if (!m_itrFile.exists()) return; /* Read file and write outputs */ LineNumberReader lnr = null; try { final byte[] content = FileUtil.getContent(m_itrFile); lnr = new LineNumberReader(new StringReader(new String(content, Charset.defaultCharset()))); while (lnr.ready()) { final String line = lnr.readLine(); if (line == null) break; processLine(line, lnr.getLineNumber()); } } catch (final FileNotFoundException e) { // FIXME: these stati are never used; what happened here? // if( lnr == null ) // StatusUtilities.createStatus( IStatus.WARNING, ISimulation1D2DConstants.CODE_RMA10S, Messages.getString( "org.kalypso.kalypsomodel1d2d.sim.IterationInfo.1" ), e ); //$NON-NLS-1$ // // final String msg = Messages.getString( "org.kalypso.kalypsomodel1d2d.sim.IterationInfo.2", lnr.getLineNumber() ); //$NON-NLS-1$ // StatusUtilities.createStatus( IStatus.WARNING, ISimulation1D2DConstants.CODE_RMA10S, msg, e ); } finally { IOUtils.closeQuietly(lnr); } }
From source file:org.kalypso.kalypsomodel1d2d.sim.IterationInfo.java
@Override public void readIterFile() throws IOException { m_itrFile.refresh();/*from w ww . ja va2s. c o m*/ if (!m_itrFile.exists()) return; /* Read file and write outputs */ LineNumberReader lnr = null; try { // final InputStream inputStream = m_itrFile.getContent().getInputStream(); final byte[] content = FileUtil.getContent(m_itrFile); // lnr = new LineNumberReader( new BufferedReader( new InputStreamReader( inputStream ) ) ); lnr = new LineNumberReader(new StringReader(new String(content, Charset.defaultCharset()))); while (lnr.ready()) { final String line = lnr.readLine(); if (line == null) break; processLine(line, lnr.getLineNumber()); } } catch (final FileNotFoundException e) { // FIXME: stati are never used; what happened here?! // if( lnr == null ) // StatusUtilities.createStatus( IStatus.WARNING, ISimulation1D2DConstants.CODE_RMA10S, Messages.getString( "org.kalypso.kalypsomodel1d2d.sim.IterationInfo.1" ), e ); //$NON-NLS-1$ // // final String msg = Messages.getString( "org.kalypso.kalypsomodel1d2d.sim.IterationInfo.2", lnr.getLineNumber() ); //$NON-NLS-1$ // StatusUtilities.createStatus( IStatus.WARNING, ISimulation1D2DConstants.CODE_RMA10S, msg, e ); } finally { IOUtils.closeQuietly(lnr); } }
From source file:com.jkoolcloud.tnt4j.streams.inputs.ZipLineStream.java
@Override protected void initialize() throws Exception { super.initialize(); if (StringUtils.isEmpty(zipFileName)) { throw new IllegalStateException( StreamsResources.getStringFormatted(StreamsResources.RESOURCE_BUNDLE_NAME, "TNTInputStream.property.undefined", StreamProperties.PROP_FILENAME)); }/*from w w w.j a va 2s . com*/ logger().log(OpLevel.DEBUG, StreamsResources.getString(StreamsResources.RESOURCE_BUNDLE_NAME, "ZipLineStream.initializing.stream"), zipFileName); InputStream fis = loadFile(zipPath); try { if (ArchiveTypes.JAR.name().equalsIgnoreCase(archType)) { zipStream = new JarInputStream(fis); } else if (ArchiveTypes.GZIP.name().equalsIgnoreCase(archType)) { zipStream = new GZIPInputStream(fis); } else { zipStream = new ZipInputStream(fis); } } catch (IOException exc) { Utils.close(fis); throw exc; } if (zipStream instanceof GZIPInputStream) { lineReader = new LineNumberReader(new BufferedReader(new InputStreamReader(zipStream))); } else { hasNextEntry(); } }
From source file:com.l2jfree.gameserver.scripting.L2ScriptEngineManager.java
public void executeScriptList(File list) throws IOException { if (list.isFile()) { LineNumberReader lnr = new LineNumberReader(new FileReader(list)); String line;//w ww. j a v a 2 s . c om File file; while ((line = lnr.readLine()) != null) { String[] parts = line.trim().split("#"); if (parts.length > 0 && !parts[0].startsWith("#") && parts[0].length() > 0) { line = parts[0]; if (line.endsWith("/**")) { line = line.substring(0, line.length() - 3); } else if (line.endsWith("/*")) { line = line.substring(0, line.length() - 2); } file = new File(SCRIPT_FOLDER, line); if (file.isDirectory() && parts[0].endsWith("/**")) { this.executeAllScriptsInDirectory(file, true, 32); } else if (file.isDirectory() && parts[0].endsWith("/*")) { this.executeAllScriptsInDirectory(file); } else if (file.isFile()) { try { this.executeScript(file); } catch (ScriptException e) { reportScriptFileError(file, e); } } else { _log.warn("Failed loading: (" + file.getCanonicalPath() + ") @ " + list.getName() + ":" + lnr.getLineNumber() + " - Reason: doesnt exists or is not a file."); } } } lnr.close(); } else { throw new IllegalArgumentException( "Argument must be an file containing a list of scripts to be loaded"); } }
From source file:pl.otros.logview.api.io.UtilsTest.java
@Test public void testLoadHttpNotGzippedBufferedReader() throws Exception { String url = HTTP_NOT_GZIPPED; LoadingInfo loadingInfo = Utils.openFileObject(fsManager.resolveFile(url)); InputStream contentInputStream = loadingInfo.getContentInputStream(); // byte[] expectedBytes = // IOUtils.toByteArray(fsManager.resolveFile(url).getContent().getInputStream()); LineNumberReader bin = new LineNumberReader(new InputStreamReader(contentInputStream)); int lines = 0; while (bin.readLine() != null) { lines++;//from w w w.j av a 2 s . c o m } AssertJUnit.assertEquals(2600, lines); // assertEquals(expected.length, actual.length); // assertArrayEquals(expected, actual); }
From source file:org.tolweb.content.preparers.EolContentPreparer.java
private List<String> createReferencesList(MappedPage mpage) { List<String> references = new ArrayList<String>(); String input = mpage.getReferences(); if (input != null) { LineNumberReader reader = null; String currentLine;//from w w w.j a va 2s .c om try { reader = new LineNumberReader(new StringReader(input)); while ((currentLine = reader.readLine()) != null) { if (StringUtils.notEmpty(currentLine)) { references.add(currentLine); } } } catch (Exception e) { e.printStackTrace(); } finally { try { reader.close(); } catch (Exception e) { } } } return references; }
From source file:org.codehaus.mojo.taglist.FileAnalyser.java
/** * Scans a file to look for task tags.// ww w. j a va 2 s . co m * * @param file the file to scan. */ public void scanFile(File file) { LineNumberReader reader = null; try { reader = new LineNumberReader(getReader(file)); String currentLine = reader.readLine(); while (currentLine != null) { int index = -1; Iterator iter = tagClasses.iterator(); // look for a tag on this line while (iter.hasNext()) { TagClass tagClass = (TagClass) iter.next(); index = tagClass.tagMatchContains(currentLine, locale); if (index != TagClass.NO_MATCH) { // there's a tag on this line String commentType = null; commentType = extractCommentType(currentLine, index); if (commentType == null) { // this is not a valid comment tag: skip other tag classes and // go to the next line break; } int tagLength = tagClass.getLastTagMatchStringLength(); int commentStartIndex = reader.getLineNumber(); StringBuffer comment = new StringBuffer(); String firstLine = StringUtils.strip(currentLine.substring(index + tagLength)); firstLine = StringUtils.removeEnd(firstLine, "*/"); //MTAGLIST-35 if (firstLine.length() == 0 || ":".equals(firstLine)) { // this is not a valid comment tag: nothing is written there if (emptyCommentsOn) { comment.append("--"); comment.append(noCommentString); comment.append("--"); } else { continue; } } else { // this tag has a comment if (firstLine.charAt(0) == ':') { comment.append(firstLine.substring(1).trim()); } else { comment.append(firstLine); } if (multipleLineCommentsOn) { // Mark the current position, set the read forward limit to // a large number that should not be met. reader.mark(MAX_COMMENT_CHARACTERS); // next line String futureLine = reader.readLine(); // we're looking for multiple line comments while (futureLine != null && futureLine.trim().startsWith(commentType) && futureLine.indexOf(tagClass.getLastTagMatchString()) < 0) { String currentComment = futureLine .substring(futureLine.indexOf(commentType) + commentType.length()) .trim(); if (currentComment.startsWith("@") || "".equals(currentComment) || "/".equals(currentComment)) { // the comment is finished break; } // try to look if the next line is not a new tag boolean newTagFound = false; Iterator moreTCiter = tagClasses.iterator(); while (moreTCiter.hasNext()) { TagClass tc = (TagClass) moreTCiter.next(); if (tc.tagMatchStartsWith(currentComment, locale)) { newTagFound = true; break; } } if (newTagFound) { // this is a new comment: stop here the current comment break; } // nothing was found: this means the comment is going on this line comment.append(" "); comment.append(currentComment); futureLine = reader.readLine(); } // Reset the reader to the marked position before the multi // line check was performed. reader.reset(); } } TagReport tagReport = tagClass.getTagReport(); FileReport fileReport = tagReport.getFileReport(file, encoding); fileReport.addComment(comment.toString(), commentStartIndex); } } currentLine = reader.readLine(); } } catch (IOException e) { log.error("Error while scanning the file " + file.getPath(), e); } finally { IOUtil.close(reader); } }