List of usage examples for java.io LineNumberReader LineNumberReader
public LineNumberReader(Reader in)
From source file:kilim.tools.Asm.java
public Asm(String afileName) throws IOException { fileName = afileName;/*from ww w. j av a 2s.co m*/ reader = new LineNumberReader(new FileReader(fileName)); cv = new ClassWriter(computeFrames ? ClassWriter.COMPUTE_FRAMES : 0); try { parseClass(); } catch (EOF eof) { if (!eofOK) { log.error("Premature end of file: " + fileName); System.exit(1); } } catch (AsmException e) { log.error(e); System.exit(1); } catch (RuntimeException e) { log.warn("File: " + fileName); if (methodName != null) { log.warn("Method: " + methodName); } log.warn(""); log.warn("Line " + line); log.warn("Last pattern match: " + lastPattern); throw e; } }
From source file:com.ggvaidya.scinames.ui.DatasetImporterController.java
private void displayPreview() { filePreviewTextArea.setText(""); if (currentFile == null) return;//from w w w. j a va2s. c o m if (currentFile.getName().endsWith("xls") || currentFile.getName().endsWith("xlsx")) { // Excel files are special! We need to load it special and then preview it. ExcelImporter imp; String excelPreviewText; try { imp = new ExcelImporter(currentFile); List<Sheet> sheets = imp.getWorksheets(); StringBuffer preview = new StringBuffer(); preview.append("Excel file version " + imp.getWorkbook().getSpreadsheetVersion() + " containing " + sheets.size() + " sheets.\n"); for (Sheet sh : sheets) { preview.append( " - " + sh.getSheetName() + " contains " + sh.getPhysicalNumberOfRows() + " rows.\n"); // No rows? if (sh.getPhysicalNumberOfRows() == 0) continue; // Header row? Row headerRow = sh.getRow(0); boolean headerEmitted = false; for (int rowIndex = 1; rowIndex < sh.getPhysicalNumberOfRows(); rowIndex++) { if (rowIndex >= 10) break; Row row = sh.getRow(rowIndex); if (!headerEmitted) { preview.append( " - " + String.join("\t", ExcelImporter.getCellsAsValues(headerRow)) + "\n"); headerEmitted = true; } preview.append(" - " + String.join("\t", ExcelImporter.getCellsAsValues(row)) + "\n"); } preview.append("\n"); } excelPreviewText = preview.toString(); } catch (IOException ex) { excelPreviewText = "Could not open '" + currentFile + "': " + ex; } filePreviewTextArea.setText(excelPreviewText); return; } // If we're here, then this is some sort of text file, so let's preview the text content directly. try { LineNumberReader reader = new LineNumberReader(new BufferedReader(new FileReader(currentFile))); // Load the first ten lines. StringBuffer head = new StringBuffer(); for (int x = 0; x < 10; x++) { head.append(reader.readLine()); head.append('\n'); } reader.close(); filePreviewTextArea.setText(head.toString()); } catch (IOException ex) { filePreviewTextArea.setBackground(BACKGROUND_RED); filePreviewTextArea.setText("ERROR: Could not load file '" + currentFile + "': " + ex); } }
From source file:net.sourceforge.pmd.lang.java.rule.errorprone.AvoidDuplicateLiteralsRule.java
private LineNumberReader getLineReader() throws IOException { return new LineNumberReader( Files.newBufferedReader(getProperty(EXCEPTION_FILE_DESCRIPTOR).toPath(), StandardCharsets.UTF_8)); }
From source file:org.wso2.carbon.ml.rest.api.neuralNetworks.FeedForwardNetwork.java
/** * method to createFeedForwardNetwork.//w ww .j av a 2 s. c om * @param seed * @param learningRate * @param analysisID * @param bachSize * @param backprop * @param hiddenList * @param inputLayerNodes * @param iterations * @param versionID * @param momentum * @param nepoches * @param datasetId * @param noHiddenLayers * @param optimizationAlgorithms * @param outputList * @param pretrain * @param updater * @return an String object with evaluation result. */ public String createFeedForwardNetwork(long seed, double learningRate, int bachSize, double nepoches, int iterations, String optimizationAlgorithms, String updater, double momentum, boolean pretrain, boolean backprop, int noHiddenLayers, int inputLayerNodes, int datasetId, int versionID, int analysisID, List<HiddenLayerDetails> hiddenList, List<OutputLayerDetails> outputList) throws IOException, InterruptedException { String evaluationDetails = null; int numLinesToSkip = 0; String delimiter = ","; mlDataSet = getDatasetPath(datasetId, versionID); analysisFraction = getAnalysisFraction(analysisID); analysisResponceVariable = getAnalysisResponseVariable(analysisID); responseIndex = getAnalysisResponseVariableIndex(analysisID); SplitTestAndTrain splitTestAndTrain; DataSet currentDataset; DataSet trainingSet = null; DataSet testingSet = null; INDArray features = null; INDArray labels = null; INDArray predicted = null; Random rnd = new Random(); int labelIndex = 0; int numClasses = 0; int fraction = 0; //Initialize RecordReader RecordReader rr = new CSVRecordReader(numLinesToSkip, delimiter); //read the dataset rr.initialize(new FileSplit(new File(mlDataSet))); labelIndex = responseIndex; numClasses = outputList.get(0).outputNodes; //Get the fraction to do the spliting data to training and testing FileReader fr = new FileReader(mlDataSet); LineNumberReader lineNumberReader = new LineNumberReader(fr); //Get the total number of lines lineNumberReader.skip(Long.MAX_VALUE); int lines = lineNumberReader.getLineNumber(); //handling multiplication of 0 error if (analysisFraction == 0) { return null; } //Take floor value to set the numHold of training data fraction = ((int) Math.floor(lines * analysisFraction)); org.nd4j.linalg.dataset.api.iterator.DataSetIterator trainIter = new RecordReaderDataSetIterator(rr, lines, labelIndex, numClasses); //Create NeuralNetConfiguration object having basic settings. NeuralNetConfiguration.ListBuilder neuralNetConfiguration = new NeuralNetConfiguration.Builder().seed(seed) .iterations(iterations).optimizationAlgo(mapOptimizationAlgorithm(optimizationAlgorithms)) .learningRate(learningRate).updater(mapUpdater(updater)).momentum(momentum) .list(noHiddenLayers + 1); //Add Hidden Layers to the network with unique settings for (int i = 0; i < noHiddenLayers; i++) { int nInput = 0; if (i == 0) nInput = inputLayerNodes; else nInput = hiddenList.get(i - 1).hiddenNodes; neuralNetConfiguration.layer(i, new DenseLayer.Builder().nIn(nInput).nOut(hiddenList.get(i).hiddenNodes) .weightInit(mapWeightInit(hiddenList.get(i).weightInit)) .activation(hiddenList.get(i).activationAlgo).build()); } //Add Output Layers to the network with unique settings neuralNetConfiguration.layer(noHiddenLayers, new OutputLayer.Builder(mapLossFunction(outputList.get(0).lossFunction)) .nIn(hiddenList.get(noHiddenLayers - 1).hiddenNodes).nOut(outputList.get(0).outputNodes) .weightInit(mapWeightInit(outputList.get(0).weightInit)) .activation(outputList.get(0).activationAlgo).build()); //Create MultiLayerConfiguration network MultiLayerConfiguration conf = neuralNetConfiguration.pretrain(pretrain).backprop(backprop).build(); MultiLayerNetwork model = new MultiLayerNetwork(conf); model.init(); model.setListeners(Collections.singletonList((IterationListener) new ScoreIterationListener(1))); while (trainIter.hasNext()) { currentDataset = trainIter.next(); splitTestAndTrain = currentDataset.splitTestAndTrain(fraction, rnd); trainingSet = splitTestAndTrain.getTrain(); testingSet = splitTestAndTrain.getTest(); features = testingSet.getFeatureMatrix(); labels = testingSet.getLabels(); } //Train the model with the training data for (int n = 0; n < nepoches; n++) { model.fit(trainingSet); } //Do the evaluations of the model including the Accuracy, F1 score etc. log.info("Evaluate model...."); Evaluation eval = new Evaluation(outputList.get(0).outputNodes); predicted = model.output(features, false); eval.eval(labels, predicted); evaluationDetails = "{\"Accuracy\":\"" + eval.accuracy() + "\", \"Pecision\":\"" + eval.precision() + "\",\"Recall\":\"" + eval.recall() + "\",\"F1Score\":\"" + eval.f1() + "\"}"; return evaluationDetails; }
From source file:mitm.common.security.ca.handlers.comodo.CollectCustomClientCert.java
private void handleResponse(int statusCode, HttpMethod httpMethod) throws IOException { if (statusCode != HttpStatus.SC_OK) { throw new IOException("Error Collecting certificate. Message: " + httpMethod.getStatusLine()); }// w w w .ja v a2 s . c o m InputStream input = httpMethod.getResponseBodyAsStream(); if (input == null) { throw new IOException("Response body is null."); } /* * we want to set a max on the number of bytes to download. We do not want a rogue server to return 1GB. */ InputStream limitInput = new SizeLimitedInputStream(input, MAX_HTTP_RESPONSE_SIZE); String response = IOUtils.toString(limitInput, CharEncoding.US_ASCII); if (logger.isDebugEnabled()) { logger.debug("Response:\r\n" + response); } LineNumberReader lineReader = new LineNumberReader(new StringReader(response)); String statusParameter = lineReader.readLine(); errorCode = CustomClientStatusCode.fromCode(statusParameter); if (errorCode.getID() < CustomClientStatusCode.SUCCESSFUL.getID()) { error = true; errorMessage = lineReader.readLine(); } else { error = false; if (errorCode == CustomClientStatusCode.CERTIFICATES_ATTACHED) { /* * The certificate is base64 encoded between -----BEGIN CERTIFICATE----- and -----END CERTIFICATE----- */ StrBuilder base64 = new StrBuilder(4096); /* * Skip -----BEGIN CERTIFICATE----- */ String line = lineReader.readLine(); if (!"-----BEGIN CERTIFICATE-----".equalsIgnoreCase(line)) { throw new IOException("-----BEGIN CERTIFICATE----- expected but got: " + line); } do { line = lineReader.readLine(); if ("-----END CERTIFICATE-----".equalsIgnoreCase(line)) { break; } if (line != null) { base64.append(line); } } while (line != null); try { byte[] decoded = Base64.decodeBase64(MiscStringUtils.toAsciiBytes(base64.toString())); Collection<X509Certificate> certificates = CertificateUtils .readX509Certificates(new ByteArrayInputStream(decoded)); if (certificates != null && certificates.size() > 0) { certificate = certificates.iterator().next(); } } catch (CertificateException e) { throw new IOException(e); } catch (NoSuchProviderException e) { throw new IOException(e); } } } }
From source file:net.sqs2.util.FileUtil.java
/** * keyword replace in stream (replacing string cannot contain newline * chars)./*from w w w . ja v a 2 s . c o m*/ * * @param inputStream * @param outputStream * @param from * keyword(from) * @param to * keyword(to) * @param encoding * file encoding * @return true: some keywords replaced, false: no keyword replaced */ public static boolean keywordSubstitution(InputStream inputStream, OutputStream outputStream, String from, String to, String encoding) { boolean modified = false; try { PrintWriter writer = new PrintWriter( new OutputStreamWriter(new BufferedOutputStream(outputStream), encoding)); LineNumberReader reader = new LineNumberReader( new InputStreamReader(new BufferedInputStream(inputStream), encoding)); String line = null; while ((line = reader.readLine()) != null) { String result = StringUtil.replaceAll(line, from, to); writer.println(result); if (!result.equals(line)) { modified |= true; } } reader.close(); writer.close(); } catch (IOException e) { e.printStackTrace(); new RuntimeException(e); } return modified; }
From source file:org.jumpmind.metl.core.runtime.component.XmlReader.java
void processFiles(List<String> files, Message inputMessage, ISendMessageCallback callback, boolean unitOfWorkLastMessage) throws XmlPullParserException, IOException { IDirectory directory = getResourceReference(); XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser(); for (String file : files) { ArrayList<String> outboundPayload = new ArrayList<String>(); log(LogLevel.INFO, "Reading %s", file); Map<String, Serializable> headers = new HashMap<>(); headers.put("source.file.path", file); LineNumberReader lineNumberReader = null; InputStream parserIs = null; try {// w w w . j av a2 s . c om String filePath = resolveParamsAndHeaders(file, inputMessage); parserIs = directory.getInputStream(filePath, mustExist); if (parserIs != null) { InputStreamReader reader = new InputStreamReader(directory.getInputStream(filePath, mustExist), encoding); parser.setInput(parserIs, encoding); lineNumberReader = new LineNumberReader(reader); lineNumberReader.setLineNumber(1); int startCol = 0; int startLine = 1; int prevEndLine = 1; int prevEndCol = 0; int eventType = parser.getEventType(); String line = null; while (eventType != XmlPullParser.END_DOCUMENT) { switch (eventType) { case XmlPullParser.START_TAG: if (StringUtils.isBlank(readTag)) { readTag = parser.getName(); info("Read tag was not set, defaulting to root tag: " + readTag); } if (parser.getName().equals(readTag)) { startCol = prevEndCol; startLine = prevEndLine; } prevEndCol = parser.getColumnNumber(); prevEndLine = parser.getLineNumber(); break; case XmlPullParser.END_TAG: prevEndCol = parser.getColumnNumber(); prevEndLine = parser.getLineNumber(); if (parser.getName().equals(readTag)) { StringBuilder xml = new StringBuilder(); forward(startLine, lineNumberReader); int linesToRead = parser.getLineNumber() - lineNumberReader.getLineNumber(); if (lineNumberReader.getLineNumber() > startLine) { startCol = 0; } line = lineNumberReader.readLine(); while (linesToRead >= 0 && line != null) { if (startCol > 0) { if (line.length() > startCol) { xml.append(line.substring(startCol)).append("\n"); } startCol = 0; } else if (linesToRead == 0) { if (line.length() > parser.getColumnNumber()) { xml.append(line.substring(0, parser.getColumnNumber())); } else { xml.append(line).append("\n"); } } else { xml.append(line).append("\n"); } linesToRead--; if (linesToRead >= 0) { line = lineNumberReader.readLine(); } } getComponentStatistics().incrementNumberEntitiesProcessed(threadNumber); outboundPayload.add(xml.toString()); if (outboundPayload.size() == readTagsPerMessage) { callback.sendTextMessage(headers, outboundPayload); outboundPayload = new ArrayList<String>(); } startCol = 0; } break; } eventType = parser.next(); } } else { info("File %s didn't exist, but Must Exist setting was false. Continuing", file); } } finally { closeQuietly(lineNumberReader); closeQuietly(parserIs); } if (outboundPayload.size() > 0) { callback.sendTextMessage(headers, outboundPayload); } } }
From source file:org.kawanfw.sql.jdbc.util.FileBackedList.java
@Override public int size() { if (lineNumberReader == null) { throw new IllegalStateException("FileBaskList has been cleared. Can not be used anymore."); }//from w ww . j a v a 2s. c om // Return the size only if it has not been computed if (size != -1) { return size; } IOUtils.closeQuietly(lineNumberReader); LineNumberReader localLineNumberReader = null; size = 0; try { localLineNumberReader = new LineNumberReader(new FileReader(file)); @SuppressWarnings("unused") String line = ""; while ((line = localLineNumberReader.readLine()) != null) { size++; } size = size - 1; } catch (IOException e) { throw new IllegalArgumentException(e); } finally { IOUtils.closeQuietly(localLineNumberReader); } try { lineNumberReader = new LineNumberReader(new FileReader(file)); } catch (FileNotFoundException e) { throw new IllegalArgumentException(e); } return size; }
From source file:org.kalypso.kalypsomodel1d2d.sim.IterationInfoTelemac.java
@Override public void readIterFile() throws IOException { m_itrFile.refresh();//from w ww.j ava2s . 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) { 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", //$NON-NLS-1$ lnr.getLineNumber()); StatusUtilities.createStatus(IStatus.WARNING, ISimulation1D2DConstants.CODE_RMA10S, msg, e); } finally { IOUtils.closeQuietly(lnr); } }
From source file:com.google.ratel.service.error.ErrorReport.java
/** * Create a ErrorReport instance from the given error and page. * * @param error the cause of the error//w w w . j av a2s. c o m * @param pageClass the Page class which caused the error * @param isProductionMode the application is in "production" mode * @param request the page request * @param servletContext the servlet context */ public ErrorReport(Throwable throwable, Class<?> serviceClass, Method method, Mode mode, HttpServletRequest request, RatelConfig ratelConfig) { this.serviceClass = serviceClass; this.method = method; this.mode = mode; this.request = request; this.ratelConfig = ratelConfig; Throwable temp = getTemplateException(throwable); if (temp == null) { error = throwable; } else { error = temp; } if (isParseError(error)) { TemplateException te = (TemplateException) error; if (te.getTemplateName().charAt(0) == '/') { sourceName = te.getTemplateName(); } else { sourceName = '/' + te.getTemplateName(); } lineNumber = te.getLineNumber(); columnNumber = te.getColumnNumber(); InputStream is = ratelConfig.getServletContext().getResourceAsStream(sourceName); sourceReader = new LineNumberReader(new InputStreamReader(is)); isParseError = true; } else { isParseError = false; sourceName = null; columnNumber = -1; StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); Throwable cause = getCause(); cause.printStackTrace(pw); StringTokenizer tokenizer = new StringTokenizer(sw.toString(), "\n"); try { tokenizer.nextToken(); String line = tokenizer.nextToken(); //if (cause instanceof IllegalArgumentException) { // line = tokenizer.nextToken(); //if (line.indexOf("org.apache.commons.lang.Validate") != -1) { // line = tokenizer.nextToken(); //} //} int nameStart = line.indexOf("at "); int nameEnd = line.indexOf("("); nameEnd = line.lastIndexOf(".", nameEnd); if (line.indexOf("$") != -1) { nameEnd = line.indexOf("$"); } if (nameStart != -1 && nameEnd != -1) { String classname = line.substring(nameStart + 3, nameEnd); int lineStart = line.indexOf(":"); if (lineStart != -1) { int lineEnd = line.indexOf(")"); if (lineEnd != -1) { String localLineNumber = line.substring(lineStart + 1, lineEnd); this.lineNumber = Integer.parseInt(localLineNumber); } String filename = "/" + classname.replace('.', '/') + ".java"; sourceReader = getJavaSourceReader(filename); } } } catch (Exception e) { e.printStackTrace(); } } }