List of usage examples for java.io LineNumberReader LineNumberReader
public LineNumberReader(Reader in)
From source file:gemlite.shell.commands.admin.ExecuteSql.java
@CliCommand(value = "execute sql", help = "execute sql") public boolean execute(@CliOption(key = "sql", mandatory = false) String sql, @CliOption(key = "file", mandatory = false) String file) { if (StringUtils.isEmpty(sql) && StringUtils.isEmpty(file)) return false; AsyncSqlProcessor processor = new AsyncSqlProcessor(new AntlrTableParser(), new SimpleMapperDao(), new GFDomainSender()); Date nowtime = new Date(); StringBuffer msgBuf = new StringBuffer(); msgBuf.append("timestamp:::" + nowtime.getTime() + "\n"); if (StringUtils.isNotEmpty(sql)) { msgBuf.append(sql);/*from w w w . ja v a2s .com*/ processor.parserOneMessage(msgBuf.toString()); processor.remoteProcess(); StringBuilder info = new StringBuilder( "ExecuteSql :" + msgBuf.toString() + "Time is:" + nowtime.toString()); LogUtil.getCoreLog().info(info.toString()); System.out.println(info.toString()); } else if (StringUtils.isNotEmpty(file)) { try { LineNumberReader lr = new LineNumberReader(new FileReader(file)); String sqlline = lr.readLine(); int recvCount = 0; while (sqlline != null) { sqlline = sqlline.trim(); if (StringUtils.isNotEmpty(sqlline)) { if (sqlline.startsWith("insert") || sqlline.startsWith("update") || sqlline.startsWith("delete") || sqlline.startsWith("INSERT") || sqlline.startsWith("UPDATE") || sqlline.startsWith("DELETE")) { msgBuf.append("\n" + sqlline); recvCount++; } else { msgBuf.append(" " + sqlline); } } sqlline = lr.readLine(); } lr.close(); StringBuilder info = new StringBuilder("Read file ok. The Sql Number is " + recvCount); LogUtil.getCoreLog().info(info.toString()); System.out.println(info.toString()); processor.parserOneMessage(msgBuf.toString()); processor.remoteProcess(); } catch (IOException e) { LogUtil.getCoreLog().error("execute error, file:" + file, e); return false; } } return true; }
From source file:org.kalypso.ogc.sensor.timeseries.wq.at.AtTable.java
/** * Reads a .at file from an InpuStream into a WQTableSet. * * @throws IOException/*w w w . jav a 2s . c o m*/ * @throws IOException */ private static AtTable readAt(final InputStream is) throws IOException { final LineNumberReader reader = new LineNumberReader(new InputStreamReader(is)); final String firstLine = reader.readLine(); if (firstLine == null) throw new IOException(Messages.getString("org.kalypso.ogc.sensor.timeseries.wq.at.AtTable.1")); //$NON-NLS-1$ final String secondLine = reader.readLine(); if (secondLine == null) throw new IOException(Messages.getString("org.kalypso.ogc.sensor.timeseries.wq.at.AtTable.2")); //$NON-NLS-1$ final Matcher matcher = PATTERN_SECOND_LINE.matcher(secondLine); if (!matcher.matches()) throw new IOException( Messages.getString("org.kalypso.ogc.sensor.timeseries.wq.at.AtTable.3", secondLine)); //$NON-NLS-1$ final int size = Integer.parseInt(matcher.group(1)); final String typeFrom = matcher.group(2); final String typeTo = matcher.group(3); // TODO: first line ist not parsed yet final String name = firstLine; final int elbaNr = -1; final BigDecimal min = null; final BigDecimal max = null; final Date validity = null; // TODO: get validity from a) the stream b) the atDictionary // TODO: discuss with moni? final List<WQPair> wqList = new ArrayList<>(size); // TODO: read table from stream while (reader.ready()) { final String line = reader.readLine(); if (line == null) break; final String[] strings = line.trim().split("\\s+"); //$NON-NLS-1$ if (strings.length != 2) throw new IOException(Messages.getString("org.kalypso.ogc.sensor.timeseries.wq.at.AtTable.6", //$NON-NLS-1$ reader.getLineNumber(), line)); try { final double w = Double.parseDouble(strings[0]); final double q = Double.parseDouble(strings[1]); wqList.add(new WQPair(w, q)); } catch (final NumberFormatException e) { throw new IOException(Messages.getString("org.kalypso.ogc.sensor.timeseries.wq.at.AtTable.8", //$NON-NLS-1$ reader.getLineNumber(), line)); } } if (wqList.size() != size) { // ignore; maybe produce a warning? } final WQPair[] pairs = wqList.toArray(new WQPair[wqList.size()]); return new AtTable(name, elbaNr, min, max, typeFrom, typeTo, validity, pairs); }
From source file:com.jkoolcloud.tnt4j.streams.sample.custom.SampleStream.java
/** * Initialize the stream.//from www.ja v a 2 s .c o m * * @throws Exception * indicates that stream is not configured properly and cannot continue. */ @Override public void initialize() throws Exception { super.initialize(); if (fileName == null) { throw new IllegalStateException("SampleStream: File name not defined"); // NON-NLS } logger().log(OpLevel.DEBUG, "Opening file: {0}", fileName); // NON-NLS activityFile = new File(fileName); lineReader = new LineNumberReader(new FileReader(activityFile)); }
From source file:com.esd.vs.interceptor.LoginInterceptor.java
public String getMACAddress(String ip) { String str = ""; String macAddress = ""; try {//from w w w. j av a 2 s . c o m Process p = Runtime.getRuntime().exec("nbtstat -A " + ip); InputStreamReader ir = new InputStreamReader(p.getInputStream()); LineNumberReader input = new LineNumberReader(ir); for (int i = 1; i < 100; i++) { str = input.readLine(); if (str != null) { if (str.indexOf("MAC Address") > 1) { macAddress = str.substring(str.indexOf("MAC Address") + 14, str.length()); break; } } } } catch (IOException e) { e.printStackTrace(System.out); } return macAddress; }
From source file:com.codecrate.shard.transfer.pcgen.PcgenObjectImporter.java
private int getNumberOfFileLines(File file) { int lines = 0; try {//from w w w .jav a 2 s.com long lastByte = file.length(); LineNumberReader lineRead = new LineNumberReader(new FileReader(file)); lineRead.skip(lastByte); lines = lineRead.getLineNumber() - 1; lineRead.close(); } catch (IOException e) { LOG.warn("Error counting the number of lines in file: " + file, e); } return lines; }
From source file:nl.b3p.viewer.util.databaseupdate.ScriptRunner.java
/** * Runs an SQL script (read in using the Reader parameter) using the * connection passed in//from w w w. j a va 2 s . c om * * @param conn - the connection to use for the script * @param reader - the source of the script * @throws SQLException if any SQL errors occur * @throws IOException if there is an error reading from the Reader */ private void runScript(Connection conn, Reader reader) throws IOException, SQLException { StringBuffer command = null; try { LineNumberReader lineReader = new LineNumberReader(reader); String line = null; while ((line = lineReader.readLine()) != null) { if (command == null) { command = new StringBuffer(); } String trimmedLine = line.trim(); if (trimmedLine.startsWith("--")) { log.debug(trimmedLine); } else if (trimmedLine.length() < 1 || trimmedLine.startsWith("//")) { // Do nothing } else if (trimmedLine.length() < 1 || trimmedLine.startsWith("--")) { // Do nothing } else if (!fullLineDelimiter && trimmedLine.endsWith(getDelimiter()) || fullLineDelimiter && trimmedLine.equals(getDelimiter())) { command.append(line.substring(0, line.lastIndexOf(getDelimiter()))); command.append(" "); Statement statement = conn.createStatement(); log.debug(command); boolean hasResults = false; if (stopOnError) { hasResults = statement.execute(command.toString()); } else { try { statement.execute(command.toString()); } catch (SQLException e) { e.fillInStackTrace(); log.error("Error executing: " + command, e); } } if (autoCommit && !conn.getAutoCommit()) { conn.commit(); } ResultSet rs = statement.getResultSet(); if (hasResults && rs != null) { ResultSetMetaData md = rs.getMetaData(); int cols = md.getColumnCount(); for (int i = 0; i < cols; i++) { String name = md.getColumnLabel(i); log.debug(name + "\t"); } while (rs.next()) { for (int i = 0; i < cols; i++) { String value = rs.getString(i); log.debug(value + "\t"); } } } command = null; try { statement.close(); } catch (Exception e) { // Ignore to workaround a bug in Jakarta DBCP } Thread.yield(); } else { command.append(line); command.append(" "); } } if (!autoCommit) { conn.commit(); } } catch (SQLException e) { e.fillInStackTrace(); log.error("Error executing: " + command, e); throw e; } catch (IOException e) { e.fillInStackTrace(); log.error("Error executing: " + command, e); throw e; } finally { if (!this.autoCommit) { conn.rollback(); } } }
From source file:org.springframework.xd.jdbc.SingleTableDatabaseInitializer.java
private Resource substitutePlaceholdersForResource(Resource resource) { StringBuilder script = new StringBuilder(); try {/*from w w w. ja v a 2 s .co m*/ EncodedResource er = new EncodedResource(resource); LineNumberReader lnr = new LineNumberReader(er.getReader()); String line = lnr.readLine(); while (line != null) { if (tableName != null && line.contains(TABLE_PLACEHOLDER)) { logger.debug( "Substituting '" + TABLE_PLACEHOLDER + "' with '" + tableName + "' in '" + line + "'"); line = line.replace(TABLE_PLACEHOLDER, tableName); } if (line.contains(COLUMNS_PLACEHOLDER)) { logger.debug( "Substituting '" + COLUMNS_PLACEHOLDER + "' with '" + columns + "' in '" + line + "'"); line = line.replace(COLUMNS_PLACEHOLDER, columns); } script.append(line + "\n"); line = lnr.readLine(); } lnr.close(); return new ByteArrayResource(script.toString().getBytes()); } catch (IOException e) { throw new InvalidDataAccessResourceUsageException("Unable to read script " + resource, e); } }
From source file:org.kalypso.model.wspm.pdb.internal.gaf.GafReader.java
public IStatus read(final File gafFile, final IProgressMonitor monitor) throws IOException { /* Reading gaf with progress stream to show nice progress for large files */ final long contentLength = gafFile.length(); monitor.beginTask(Messages.getString("GafReader.0"), (int) contentLength); //$NON-NLS-1$ /* Open Reader */ final InputStream fileStream = new BufferedInputStream(new FileInputStream(gafFile)); final ProgressInputStream progresStream = new ProgressInputStream(fileStream, contentLength, monitor); m_reader = new LineNumberReader(new InputStreamReader(progresStream)); readLines();//from w w w . j a v a 2 s.c o m final IStatusCollector logger = new StatusCollector(WspmPdbCorePlugin.PLUGIN_ID); logger.add(IStatus.INFO, String.format(Messages.getString("GafReader.1"), gafFile.getAbsolutePath())); //$NON-NLS-1$ if (m_goodLines > 0) logger.add(IStatus.OK, String.format(Messages.getString("GafReader.2"), m_goodLines)); //$NON-NLS-1$ if (m_badLines > 0) logger.add(IStatus.INFO, String.format(Messages.getString("GafReader.3"), m_badLines)); //$NON-NLS-1$ return logger.asMultiStatus(Messages.getString("GafReader.4")); //$NON-NLS-1$ }
From source file:com.googlecode.jweb1t.JWeb1TSearcher.java
private void initialize(final File baseDir) throws NumberFormatException, IOException { indexMap = new HashMap<Integer, FileMap>(); ngramCountMap = new HashMap<Integer, Long>(); ngramDistinctCountMap = new HashMap<Integer, Long>(); final File countFile = new File(baseDir, JWeb1TAggregator.AGGREGATED_COUNTS_FILE); if (countFile.exists()) { final LineNumberReader lineReader = new LineNumberReader(new FileReader(countFile)); String line;/*w ww . j a v a2 s. com*/ while ((line = lineReader.readLine()) != null) { final String[] parts = line.split("\t"); if (parts.length != 3) { continue; } final int ngramSize = Integer.valueOf(parts[0]); final long ngramDistinctCount = Long.valueOf(parts[1]); final long ngramCount = Long.valueOf(parts[2]); ngramCountMap.put(ngramSize, ngramCount); ngramDistinctCountMap.put(ngramSize, ngramDistinctCount); } lineReader.close(); } }
From source file:com.l2jfree.gameserver.util.GPLLicenseChecker.java
private static List<String> read(File f) throws IOException { ArrayList<String> list = new ArrayList<String>(); LineNumberReader lnr = null;// www . j a v a 2 s .co m try { lnr = new LineNumberReader(new FileReader(f)); for (String line; (line = lnr.readLine()) != null;) list.add(line); } finally { IOUtils.closeQuietly(lnr); } int i = 0; for (; i < LICENSE.length; i++) { if (!list.get(i).equals(LICENSE[i])) { MODIFIED.add(f.getPath() + ":" + i); return list; } } if (!startsWithPackageName(list.get(i))) { MODIFIED.add(f.getPath() + ":" + lnr.getLineNumber()); return list; } return null; }