List of usage examples for java.io LineNumberReader readLine
public String readLine() throws IOException
From source file:com.haulmont.cuba.uberjar.ServerRunner.java
protected void stop(int port, String key) { try {/* ww w . j a v a2 s . c om*/ try (Socket s = new Socket(InetAddress.getByName("127.0.0.1"), port)) { s.setSoTimeout(STOP_TIMEOUT * 1000); try (OutputStream out = s.getOutputStream()) { out.write((key + "\r\nstop\r\n").getBytes(StandardCharsets.UTF_8)); out.flush(); System.out.println(String.format("Waiting %,d seconds for server to stop", STOP_TIMEOUT)); LineNumberReader lin = new LineNumberReader( new InputStreamReader(s.getInputStream(), StandardCharsets.UTF_8)); String response; while ((response = lin.readLine()) != null) { System.out.println(String.format("Received \"%s\"", response)); if ("Stopped".equals(response)) { System.out.println("Server reports itself as Stopped"); } } } } } catch (SocketTimeoutException e) { System.out.println("Timed out waiting for stop confirmation"); } catch (Exception e) { e.printStackTrace(); } }
From source file:org.apache.cocoon.generation.TextGenerator2.java
/** * Generate XML data./*www .j av a 2 s.com*/ * * @throws IOException * @throws ProcessingException * @throws SAXException */ public void generate() throws IOException, SAXException, ProcessingException { InputStreamReader in = null; try { final InputStream sis = this.inputSource.getInputStream(); if (sis == null) { throw new ProcessingException("Source '" + this.inputSource.getURI() + "' not found"); } if (encoding != null) { in = new InputStreamReader(sis, encoding); } else { in = new InputStreamReader(sis); } } catch (SourceException se) { throw new ProcessingException("Error during resolving of '" + this.source + "'.", se); } LocatorImpl locator = new LocatorImpl(); locator.setSystemId(this.inputSource.getURI()); locator.setLineNumber(1); locator.setColumnNumber(1); /* Do not pass the source URI to the contentHandler, assuming that that is the LexicalTransformer. It does not have to be. contentHandler.setDocumentLocator(locator); */ contentHandler.startDocument(); AttributesImpl atts = new AttributesImpl(); if (localizable) { atts.addAttribute("", "source", "source", "CDATA", locator.getSystemId()); atts.addAttribute("", "line", "line", "CDATA", String.valueOf(locator.getLineNumber())); atts.addAttribute("", "column", "column", "CDATA", String.valueOf(locator.getColumnNumber())); } String nsPrefix = this.element.contains(":") ? this.element.replaceFirst(":.+$", "") : ""; String localName = this.element.replaceFirst("^.+:", ""); if (this.namespace.length() > 1) contentHandler.startPrefixMapping(nsPrefix, this.namespace); contentHandler.startElement(this.namespace, localName, this.element, atts); LineNumberReader reader = new LineNumberReader(in); String line; String newline = null; while (true) { if (newline == null) { line = convertNonXmlChars(reader.readLine()); } else { line = newline; } if (line == null) { break; } newline = convertNonXmlChars(reader.readLine()); if (newline != null) { line += SystemUtils.LINE_SEPARATOR; } locator.setLineNumber(reader.getLineNumber()); locator.setColumnNumber(1); contentHandler.characters(line.toCharArray(), 0, line.length()); if (newline == null) { break; } } reader.close(); contentHandler.endElement(this.namespace, localName, this.element); if (this.namespace.length() > 1) contentHandler.endPrefixMapping(nsPrefix); contentHandler.endDocument(); }
From source file:ca.nrc.cadc.sc2pkg.PackageIntTest.java
private Content getEntry(TarArchiveInputStream tar) throws IOException, NoSuchAlgorithmException { Content ret = new Content(); TarArchiveEntry entry = tar.getNextTarEntry(); ret.name = entry.getName();/*from w w w . j a v a2 s . c o m*/ if (ret.name.endsWith("README")) { byte[] buf = new byte[(int) entry.getSize()]; tar.read(buf); ByteArrayInputStream bis = new ByteArrayInputStream(buf); LineNumberReader r = new LineNumberReader(new InputStreamReader(bis)); String line = r.readLine(); while (line != null) { String[] tokens = line.split(" "); // status [md5 filename url] String status = tokens[0]; if ("OK".equals(status)) { String fname = tokens[1]; String md5 = tokens[2]; ret.md5map.put(fname, md5); } else { throw new RuntimeException("tar content failure: " + line); } line = r.readLine(); } } else { MessageDigest md5 = MessageDigest.getInstance("MD5"); byte[] buf = new byte[8192]; int n = tar.read(buf); while (n > 0) { md5.update(buf, 0, n); n = tar.read(buf); } byte[] md5sum = md5.digest(); ret.contentMD5 = HexUtil.toHex(md5sum); } return ret; }
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 2 s . c o m*/ // 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:com.jkoolcloud.tnt4j.streams.configure.state.FileStreamStateHandlerTest.java
@Test public void findStreamingFile() throws Exception { FileStreamStateHandler rwd = new FileStreamStateHandler(); File testFilesDir = new File(samplesDir, "/multiple-logs/"); File[] testFiles = testFilesDir.listFiles((FilenameFilter) new WildcardFileFilter("orders*")); // NON-NLS FileAccessState newFAS = new FileAccessState(); int count = 0; File fileToSearchFor = null;/*from w w w . j a va2s. c o m*/ int lineLastRead = 0; File fileWritten = null; for (File testFile : testFiles) { count++; FileReader in; LineNumberReader reader; Long fileCRC = rwd.getFileCrc(testFile); if (count == 2) { newFAS.currentFileCrc = fileCRC; fileToSearchFor = testFile; } in = new FileReader(testFile); reader = new LineNumberReader(in); reader.setLineNumber(0); String line = reader.readLine(); int count2 = 0; while (line != null) { count2++; Checksum crcLine = new CRC32(); final byte[] bytes4Line = line.getBytes(); crcLine.update(bytes4Line, 0, bytes4Line.length); final long lineCRC = crcLine.getValue(); final int lineNumber = reader.getLineNumber(); System.out.println("for " + lineNumber + " line CRC is " + lineCRC); // NON-NLS if (count2 == 3) { newFAS.currentLineCrc = lineCRC; newFAS.currentLineNumber = lineNumber; newFAS.lastReadTime = System.currentTimeMillis(); lineLastRead = lineNumber; } line = reader.readLine(); } fileWritten = AbstractFileStreamStateHandler.writeState(newFAS, testFilesDir, "TestStream"); // NON-NLS Utils.close(reader); } final File findLastProcessed = rwd.findStreamingFile(newFAS, testFiles); assertEquals(fileToSearchFor, findLastProcessed); final int lineLastReadRecorded = rwd.checkLine(findLastProcessed, newFAS); assertEquals(lineLastRead, lineLastReadRecorded); fileWritten.delete(); }
From source file:net.ontopia.persistence.rdbms.CSVImport.java
public void importCSV(InputStream csvfile) throws Exception { // Execute statements try {//from ww w . j a v a2s .co m String[] qmarks = new String[columns.length]; for (int i = 0; i < qmarks.length; i++) { qmarks[i] = "?"; } if (cleartable) { String delsql = "delete from " + table; Statement delstm = conn.createStatement(); delstm.executeUpdate(delsql); //! conn.commit(); } String sql = "insert into " + table + " (" + StringUtils.join(columns, ", ") + ") values (" + StringUtils.join(qmarks, ", ") + ")"; log.debug("SQL: " + sql); PreparedStatement stm = conn.prepareStatement(sql); int datatypes[] = new int[columns.length]; for (int i = 0; i < columns.length; i++) { Table tbl = project.getTableByName(table); if (tbl == null) throw new OntopiaRuntimeException("Unknown table: " + table); Column col = tbl.getColumnByName(columns[i]); if (col == null) throw new OntopiaRuntimeException("Unknown table column: " + columns[i]); if (col.getType() == null) throw new OntopiaRuntimeException("Column type is null: " + col.getType()); DataType datatype = project.getDataTypeByName(col.getType(), "generic"); if (datatype == null) throw new OntopiaRuntimeException("Unknown column type: " + col.getType()); String dtype = datatype.getType(); if ("varchar".equals(dtype)) datatypes[i] = Types.VARCHAR; else if ("integer".equals(dtype)) datatypes[i] = Types.INTEGER; else throw new OntopiaRuntimeException("Unknown datatype: " + dtype); } LineNumberReader reader = new LineNumberReader(new InputStreamReader(csvfile)); // Ignore first X lines for (int i = 0; i < ignorelines; i++) { String line = reader.readLine(); if (line == null) break; } // Process input log.debug("[" + StringUtils.join(columns, ", ") + "]"); int lineno = 0; while (true) { lineno++; String line = reader.readLine(); if (line == null) break; try { String[] cols = StringUtils.split(line, separator); if (cols.length > columns.length && !ignorecolumns) log.debug("Ignoring columns: " + (columns.length + 1) + "-" + cols.length + " '" + line + "'"); log.debug("CVALUES: " + (columns.length + 1) + "-" + cols.length + " '" + line + "'"); String dmesg = "("; for (int i = 0; i < columns.length; i++) { String col = cols[i]; // If first column character is '"' strip quotes. if (stripquotes) { int len = col.length(); if (len > 1 && ((col.charAt(0) == '"' && col.charAt(len - 1) == '"') || (col.charAt(0) == '\'' && col.charAt(len - 1) == '\''))) col = col.substring(1, len - 1); } if (col != null && col.equals("")) col = null; dmesg = dmesg + col; if (i < columns.length - 1) dmesg = dmesg + ", "; stm.setObject(i + 1, col, datatypes[i]); } dmesg = dmesg + ")"; log.debug(dmesg); stm.execute(); } catch (Exception e) { conn.rollback(); throw new OntopiaRuntimeException("Cannot read line " + lineno + ": '" + line + "'", e); } } conn.commit(); } finally { if (conn != null) conn.close(); } }
From source file:org.agnitas.web.ImportProfileColumnsAction.java
/** * Adds column mappings from uploaded csv-file using import profile settings * for parsing csv file/*from w ww. j av a 2 s. c om*/ * * @param aForm a form * @param request request */ private void addColumnsFromFile(ImportProfileColumnsForm aForm, HttpServletRequest request) { RecipientDao recipientDao = (RecipientDao) getWebApplicationContext().getBean("RecipientDao"); if (aForm.getProfile() == null) { loadImportProfile(aForm, request); } ImportProfile profile = aForm.getProfile(); List<ColumnMapping> columnMappings = profile.getColumnMapping(); File file = getCurrentFile(request); if (file == null) { return; } try { Map dbColumns = recipientDao.readDBColumns(AgnUtils.getCompanyID(request)); String profileCharset = Charset.getValue(profile.getCharset()); String fileString = FileUtils.readFileToString(file, profileCharset); LineNumberReader aReader = new LineNumberReader(new StringReader(fileString)); String firstLine = aReader.readLine(); firstLine = firstLine.trim(); CsvTokenizer tokenizer = new CsvTokenizer(firstLine, Separator.getValue(profile.getSeparator()), TextRecognitionChar.getValue(profile.getTextRecognitionChar())); String[] newCsvColumns = tokenizer.toArray(); List<ColumnMapping> newMappings = new ArrayList<ColumnMapping>(); for (String newCsvColumn : newCsvColumns) { if (!aForm.columnExists(newCsvColumn, columnMappings) && !StringUtils.isEmpty(newCsvColumn)) { ColumnMapping newMapping = new ColumnMappingImpl(); newMapping.setProfileId(profile.getId()); newMapping.setMandatory(false); newMapping.setFileColumn(newCsvColumn); if (dbColumns.get(newCsvColumn) != null) { String dbColumn = getInsensetiveMapKey(dbColumns, newCsvColumn); if (dbColumn == null) { // if dbColumn is NULL mapping is invalid continue; } newMapping.setDatabaseColumn(dbColumn); String defaultValue = aForm.getDbColumnsDefaults().get(dbColumn); if (defaultValue != null) { newMapping.setDefaultValue(defaultValue); } } newMappings.add(newMapping); } } profile.getColumnMapping().addAll(newMappings); } catch (IOException e) { AgnUtils.logger().error("Error reading csv-file: " + e + "\n" + AgnUtils.getStackTrace(e)); } catch (Exception e) { AgnUtils.logger().error("Error reading csv-file: " + e + "\n" + AgnUtils.getStackTrace(e)); } }
From source file:org.lockss.plugin.metapress.LocalRisMetadataExtractor.java
/** * Extract metadata from the content of the cu, which should be an RIS file. * Reads line by line inserting the 2 character code and value into the raw map. * The first line should be a material type witch if it is book or journal will * determine if we interpret the SN tag as IS beltSN or ISBN. * @param target/* w ww . j a va 2s . c o m*/ * @param cu */ public final ArticleMetadata extract(MetadataTarget target, CachedUrl cu) throws IOException, PluginException { if (cu == null) { throw new IllegalArgumentException(); } log.debug2("Parsing " + cu.getUrl()); am = new ArticleMetadata(); refType = null; currentTag = null; currentValue = new StringBuilder(); boolean hasBegun = false; LineNumberReader reader = new LineNumberReader(cu.openForReading()); try { for (String line = reader.readLine(); line != null; line = reader.readLine()) { // Skip empty lines at beginning of file if (!hasBegun && line.trim().isEmpty()) { continue; } hasBegun = true; Matcher mat = risPattern.matcher(line); if (mat.find()) { // process old tag processTag(); // set up new tag currentTag = mat.group(1); currentValue = new StringBuilder(mat.group(2).trim()); } else { if (currentTag == null) { log.debug(String.format("%s line %d: ignoring continuation line in preamble", cu.getUrl(), reader.getLineNumber())); continue; } // process continuation of current tag String continuation = line.trim(); if (!continuation.isEmpty()) { currentValue.append(' '); currentValue.append(continuation); } } } processTag(); } finally { IOUtil.safeClose(reader); } am.cook(risTagToMetadataField); return am; }
From source file:ru.jkff.antro.ProfileListener.java
private Map<String, AnnotatedFile> getAnnotatedFiles() { Map<String, AnnotatedFile> res = new HashMap<String, AnnotatedFile>(); Map<OurLocation, Stat> statsByLoc = getStatsByLocation(); for (String file : getUsedBuildFiles()) { List<String> lines = new ArrayList<String>(); List<Stat> stats = new ArrayList<Stat>(); try {/*from ww w .j a va2s . c om*/ LineNumberReader r = new LineNumberReader(new FileReader(file)); String line; while (null != (line = r.readLine())) { OurLocation loc = new OurLocation(file, r.getLineNumber()); Stat stat = statsByLoc.get(loc); lines.add(line); stats.add(stat); } } catch (IOException e) { } res.put(file, new AnnotatedFile(lines.toArray(new String[0]), stats.toArray(new Stat[0]), file)); } return res; }
From source file:gtu._work.etc.GoogleContactUI.java
void googleTableMouseClicked(MouseEvent evt) { try {/*from w ww.j a v a 2 s.c o m*/ JPopupMenuUtil popupUtil = JPopupMenuUtil.newInstance(googleTable).applyEvent(evt); //CHANGE ENCODE popupUtil.addJMenuItem("set encode", new ActionListener() { public void actionPerformed(ActionEvent e) { try { String code = StringUtils.defaultString(JOptionPaneUtil.newInstance().iconPlainMessage() .showInputDialog("input file encode", "ENCODE"), "UTF8"); encode = Charset.forName(code).displayName(); } catch (Exception ex) { JCommonUtil.handleException(ex); } System.err.println("encode : " + encode); } }); //SIMPLE LOAD GOOGLE CSV FILE popupUtil.addJMenuItem("open Google CSV file", new ActionListener() { public void actionPerformed(ActionEvent e) { File file = JFileChooserUtil.newInstance().selectFileOnly().addAcceptFile("csv", ".csv") .showOpenDialog().getApproveSelectedFile(); if (file == null) { errorMessage("file is not correct!"); return; } try { if (file.getName().endsWith(".csv")) { DefaultTableModel model = (DefaultTableModel) googleTable.getModel(); LineNumberReader reader = new LineNumberReader( new InputStreamReader(new FileInputStream(file), GOOGLE_CVS_ENCODE)); for (String line = null; (line = reader.readLine()) != null;) { if (reader.getLineNumber() == 1) { continue; } model.addRow(line.split(",")); } reader.close(); googleTable.setModel(model); JTableUtil.newInstance(googleTable).hiddenAllEmptyColumn(); } } catch (Exception ex) { JCommonUtil.handleException(ex); } } }); //SAVE CSV FILE FOR GOOGLE popupUtil.addJMenuItem("save to Google CVS file", new ActionListener() { public void actionPerformed(ActionEvent e) { File file = JFileChooserUtil.newInstance().selectFileOnly().addAcceptFile(".csv", ".csv") .showSaveDialog().getApproveSelectedFile(); if (file == null) { errorMessage("file is not correct!"); return; } file = FileUtil.getIndicateFileExtension(file, ".csv"); try { BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(file), GOOGLE_CVS_ENCODE)); StringBuilder sb = new StringBuilder(); for (Object title : googleColumns) { sb.append(title + ","); } sb.deleteCharAt(sb.length() - 1); System.out.println(sb); writer.write(sb.toString()); writer.newLine(); DefaultTableModel model = (DefaultTableModel) googleTable.getModel(); for (int row = 0; row < model.getRowCount(); row++) { sb = new StringBuilder(); for (int col = 0; col < model.getColumnCount(); col++) { String colVal = StringUtils.defaultString((String) model.getValueAt(row, col), ""); if (colVal.equalsIgnoreCase("null")) { colVal = ""; } sb.append(colVal + ","); } sb.deleteCharAt(sb.length() - 1); System.out.println(sb); writer.write(sb.toString()); writer.newLine(); } writer.flush(); writer.close(); } catch (Exception ex) { JCommonUtil.handleException(ex); } } }); //PASTE CLIPBOARD popupUtil.addJMenuItem("paste clipboard", new ActionListener() { public void actionPerformed(ActionEvent paramActionEvent) { JTableUtil.newInstance(googleTable).pasteFromClipboard_multiRowData(true); } }); popupUtil.addJMenuItem("paste clipboard to selected cell", new ActionListener() { public void actionPerformed(ActionEvent paramActionEvent) { JTableUtil.newInstance(googleTable).pasteFromClipboard_singleValueToSelectedCell(); } }); JMenuItem addEmptyRowItem = JTableUtil.newInstance(googleTable).jMenuItem_addRow(false, "add row count?"); addEmptyRowItem.setText("add row"); JMenuItem removeColumnItem = JTableUtil.newInstance(googleTable).jMenuItem_removeColumn(null); removeColumnItem.setText("remove column"); JMenuItem removeRowItem = JTableUtil.newInstance(googleTable).jMenuItem_removeRow(null); removeRowItem.setText("remove row"); JMenuItem removeAllRowItem = JTableUtil.newInstance(googleTable) .jMenuItem_removeAllRow("remove all row?"); removeAllRowItem.setText("remove all row"); JMenuItem clearSelectedCellItem = JTableUtil.newInstance(googleTable) .jMenuItem_clearSelectedCell("are you sure clear selected area?"); clearSelectedCellItem.setText("clear selected area"); popupUtil.addJMenuItem(addEmptyRowItem, removeColumnItem, removeRowItem, removeAllRowItem, clearSelectedCellItem); popupUtil.show(); } catch (Exception ex) { JCommonUtil.handleException(ex); } }