List of usage examples for java.io LineNumberReader LineNumberReader
public LineNumberReader(Reader in)
From source file:org.latticesoft.util.common.FileUtil.java
/** Gets a line number reader */ public static LineNumberReader getLineNumberReader(InputStream is) { if (is == null) return null; LineNumberReader lr = null;//from ww w. j a v a 2 s .co m try { lr = new LineNumberReader(new InputStreamReader(is)); } catch (Exception e) { if (log.isErrorEnabled()) { log.error(e); } } return lr; }
From source file:org.agnitas.web.ImportWizardForm.java
/** * check in the columnMapping for the key column, and eventually for gender * and mailtype read first csv line again; do not parse (allready parsed in * parseFirstline) prepare download-files for errors and parsed data read * the rest of the csv-file/* w ww. ja v a2s .c o m*/ */ protected ActionErrors parseContent(HttpServletRequest req) { ApplicationContext aContext = this.getWebApplicationContext(); JdbcTemplate jdbc = new JdbcTemplate((DataSource) aContext.getBean("dataSource")); LinkedList aLineContent = null; String firstline = null; String csvString = new String(""); ActionErrors errors = new ActionErrors(); boolean hasGENDER = false; boolean hasMAILTYPE = false; boolean hasKeyColumn = false; this.uniqueValues = new HashSet(); this.parsedContent = new LinkedList(); this.linesOK = 0; // this.csvMaxUsedColumn=0; this.dbInsertStatus = 0; try { csvString = new String(this.getCsvFile().getFileData(), status.getCharset()); } catch (Exception e) { AgnUtils.logger().error("parseContent: " + e); errors.add("global", new ActionMessage("error.import.charset")); return errors; } try { this.loadBlacklist(this.getCompanyID(req), jdbc); } catch (Exception e) { errors.add("global", new ActionMessage("import.blacklist.read")); return errors; } LineNumberReader aReader = new LineNumberReader(new StringReader(csvString)); String myline = null; // check in the columnMapping for the key column, // and eventually for gender and mailtype: String aKey = ""; CsvColInfo aCol = null; Enumeration aMapEnu = this.columnMapping.keys(); while (aMapEnu.hasMoreElements()) { aKey = (String) aMapEnu.nextElement(); aCol = (CsvColInfo) this.columnMapping.get(aKey); if (aCol.getName().equalsIgnoreCase(GENDER_KEY)) { hasGENDER = true; } if (aCol.getName().equalsIgnoreCase(MAILTYPE_KEY)) { hasMAILTYPE = true; } if (aCol.getName().equalsIgnoreCase(this.status.getKeycolumn())) { hasKeyColumn = true; } } if (!hasKeyColumn) { errors.add("global", new ActionMessage("error.import.no_keycolumn_mapping")); } if (this.getMode() == ImportWizardForm.MODE_ADD || this.getMode() == ImportWizardForm.MODE_ADD_UPDATE) { if (!hasGENDER) { errors.add("global", new ActionMessage("error.import.no_gender_mapping")); } if (!hasMAILTYPE) { errors.add("global", new ActionMessage("error.import.no_mailtype_mapping")); } } try { // read first csv line again; do not parse (allready parsed in // parseFirstline): if ((myline = aReader.readLine()) != null) { firstline = myline; } // prepare download-files for errors and parsed data errorData.put(DATE_ERROR, new StringBuffer(firstline + '\n')); errorData.put(EMAIL_ERROR, new StringBuffer(firstline + '\n')); errorData.put(EMAILDOUBLE_ERROR, new StringBuffer(firstline + '\n')); errorData.put(GENDER_ERROR, new StringBuffer(firstline + '\n')); errorData.put(MAILTYPE_ERROR, new StringBuffer(firstline + '\n')); errorData.put(NUMERIC_ERROR, new StringBuffer(firstline + '\n')); errorData.put(STRUCTURE_ERROR, new StringBuffer(firstline + '\n')); errorData.put(BLACKLIST_ERROR, new StringBuffer(firstline + '\n')); parsedData = new StringBuffer(firstline + '\n'); // read the rest of the csv-file: // StringTokenizer file = new StringTokenizer(csvString, "\n"); if (errors.isEmpty()) { readlines = 0; int maxrows = BLOCK_SIZE; this.linesOK = 0; while ((myline = aReader.readLine()) != null && this.linesOK < maxrows) { // Bug-Fix just read the first 1000 lines to avoid trouble with heap space if (myline.trim().length() > 0) { aLineContent = parseLine(myline, (Locale) req.getSession().getAttribute(org.apache.struts.Globals.LOCALE_KEY)); if (aLineContent != null) { parsedContent.add(aLineContent); this.parsedData.append(myline + "\n"); this.linesOK++; } } readlines++; } aReader.close(); } } catch (Exception e) { AgnUtils.logger().error("parseContent: " + e); } return errors; }
From source file:org.voltdb.compiler.DDLCompiler.java
/** * Read until the next newline/*from w ww . j a va 2s . co m*/ * @throws IOException */ String readToEndOfLine(FileReader reader) throws IOException { LineNumberReader lnr = new LineNumberReader(reader); String retval = lnr.readLine(); m_currLineNo++; return retval; }
From source file:org.apache.zookeeper.server.quorum.QuorumPeerMainTest.java
/** * Verify that a node without the leader in its view will not attempt to connect to the leader. *//*ww w . j av a2 s.co m*/ @Test public void testLeaderOutOfView() throws Exception { ClientBase.setupTestEnv(); int numServers = 3; // used for assertions later boolean foundLeading = false; boolean foundFollowing = false; // capture QuorumPeer logging ByteArrayOutputStream os = new ByteArrayOutputStream(); WriterAppender appender = getConsoleAppender(os, Level.DEBUG); Logger qlogger = Logger.getLogger("org.apache.zookeeper.server.quorum"); qlogger.addAppender(appender); try { Servers svrs = new Servers(); svrs.clientPorts = new int[numServers]; for (int i = 0; i < numServers; i++) { svrs.clientPorts[i] = PortAssignment.unique(); } String quorumCfgIncomplete = getUniquePortCfgForId(1) + "\n" + getUniquePortCfgForId(2); String quorumCfgComplete = quorumCfgIncomplete + "\n" + getUniquePortCfgForId(3); svrs.mt = new MainThread[3]; // Node 1 is started without the leader (3) in its config view svrs.mt[0] = new MainThread(1, svrs.clientPorts[0], quorumCfgIncomplete); for (int i = 1; i < numServers; i++) { svrs.mt[i] = new MainThread(i + 1, svrs.clientPorts[i], quorumCfgComplete); } // Node 1 must be started first, before quorum is formed, to trigger the attempted invalid connection to 3 svrs.mt[0].start(); QuorumPeer quorumPeer1 = waitForQuorumPeer(svrs.mt[0], CONNECTION_TIMEOUT); Assert.assertTrue(quorumPeer1.getPeerState() == QuorumPeer.ServerState.LOOKING); // Node 3 started second to avoid 1 and 2 forming a quorum before 3 starts up int highestServerIndex = numServers - 1; svrs.mt[highestServerIndex].start(); QuorumPeer quorumPeer3 = waitForQuorumPeer(svrs.mt[highestServerIndex], CONNECTION_TIMEOUT); Assert.assertTrue(quorumPeer3.getPeerState() == QuorumPeer.ServerState.LOOKING); // Node 2 started last, kicks off leader election for (int i = 1; i < highestServerIndex; i++) { svrs.mt[i].start(); } // Nodes 2 and 3 now form quorum and fully start. 1 attempts to vote for 3, fails, returns to LOOKING state for (int i = 1; i < numServers; i++) { Assert.assertTrue("waiting for server to start", ClientBase.waitForServerUp("127.0.0.1:" + svrs.clientPorts[i], CONNECTION_TIMEOUT)); } Assert.assertTrue(svrs.mt[0].getQuorumPeer().getPeerState() == QuorumPeer.ServerState.LOOKING); Assert.assertTrue( svrs.mt[highestServerIndex].getQuorumPeer().getPeerState() == QuorumPeer.ServerState.LEADING); for (int i = 1; i < highestServerIndex; i++) { Assert.assertTrue(svrs.mt[i].getQuorumPeer().getPeerState() == QuorumPeer.ServerState.FOLLOWING); } // Look through the logs for output that indicates Node 1 is LEADING or FOLLOWING LineNumberReader r = new LineNumberReader(new StringReader(os.toString())); Pattern leading = Pattern.compile(".*myid=1.*QuorumPeer.*LEADING.*"); Pattern following = Pattern.compile(".*myid=1.*QuorumPeer.*FOLLOWING.*"); String line; while ((line = r.readLine()) != null && !foundLeading && !foundFollowing) { foundLeading = leading.matcher(line).matches(); foundFollowing = following.matcher(line).matches(); } } finally { qlogger.removeAppender(appender); } Assert.assertFalse("Corrupt peer should never become leader", foundLeading); Assert.assertFalse("Corrupt peer should not attempt connection to out of view leader", foundFollowing); }
From source file:ua.utility.kfsdbupgrade.App.java
/** * From the {@link #upgradeRoot}//from ww w .j a v a 2s. co m * <code>/post-upgrade/sql/kfs-indexes.sql</code> file, create any indices * that are present in the SQL file but in the database that is being worked * against TODO there's more going on here... come back after digging * through submethods * * @param conn * @param stmt */ protected boolean createExistingIndexes(Connection conn, Statement stmt, File kfsIndexesSqlFile) { boolean success = true; LineNumberReader lnr = null; logHeader2("creating KFS indexes that existed prior to upgrade where required "); try { lnr = new LineNumberReader(new FileReader(kfsIndexesSqlFile)); String line = null; while ((line = lnr.readLine()) != null) { if (StringUtils.isNotBlank(line) && line.startsWith("--")) { // Skip lines starting with a comment continue; } String tableName = getIndexTableName(line); String indexName = getIndexName(line); if (StringUtils.isNotBlank(tableName) && StringUtils.isNotBlank(indexName)) { if (tableExists(conn, stmt, tableName)) { boolean unique = line.contains(" UNIQUE "); List<String> columnNames = getIndexColumnNames(line); if (!indexExists(conn, stmt, tableName, columnNames)) { if (indexNameExists(conn, stmt, tableName, indexName)) { indexName = getNextTableIndexName(conn, stmt, tableName); } StringBuilder sql = new StringBuilder(256); sql.append("CREATE "); if (unique) { sql.append("UNIQUE "); } sql.append("INDEX KULOWNER."); sql.append(indexName); sql.append(" ON KULOWNER."); sql.append(tableName); sql.append("("); String comma = ""; for (String columnName : columnNames) { sql.append(comma); sql.append(columnName); comma = ","; } sql.append(")"); try { stmt.execute(sql.toString()); } catch (SQLException ex) { success = false; LOGGER.error("failed to create index: " + sql.toString(), ex); } } } } } } catch (Exception ex) { success = false; LOGGER.error(ex); } finally { try { if (lnr != null) { lnr.close(); } } catch (Exception ex) { } ; } postUpgradeFilesProcessed.add(kfsIndexesSqlFile); return success; }
From source file:org.ngrinder.perftest.service.PerfTestService.java
/** * Get the monitor data interval value. In the normal, the image width is 700, and if the data count is too big, * there will be too many points in the chart. So we will calculate the interval to get appropriate count of data to * display. For example, interval value "2" means, get one record for every "2" records. * * @param testId test id//from w ww . ja v a 2s . c o m * @param targetIP ip address of monitor target * @param imageWidth image with of the chart. * @return interval value. */ public int getMonitorGraphInterval(long testId, String targetIP, int imageWidth) { File monitorDataFile = new File(config.getHome().getPerfTestReportDirectory(String.valueOf(testId)), MONITOR_FILE_PREFIX + targetIP + ".data"); int pointCount = Math.max(imageWidth, MAX_POINT_COUNT); FileInputStream in = null; InputStreamReader isr = null; LineNumberReader lnr = null; int interval = 0; try { in = new FileInputStream(monitorDataFile); isr = new InputStreamReader(in); lnr = new LineNumberReader(isr); lnr.skip(monitorDataFile.length()); int lineNumber = lnr.getLineNumber() + 1; interval = Math.max(lineNumber / pointCount, 1); } catch (FileNotFoundException e) { LOGGER.info("Monitor data file does not exist at {}", monitorDataFile); } catch (IOException e) { LOGGER.info("Error while getting monitor:{} data file:{}", targetIP, monitorDataFile); } finally { IOUtils.closeQuietly(lnr); IOUtils.closeQuietly(isr); IOUtils.closeQuietly(in); } return interval; }
From source file:de.innovationgate.wgpublisher.WGPDispatcher.java
/** * @param request/*from w w w. ja v a2s . c o m*/ * @param response * @param session */ private void sendJobLog(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException { if (!isAdminLoggedIn(request)) { response.sendError(HttpServletResponse.SC_FORBIDDEN, "You must be logged in as WGA administrator!"); return; } String jobToShow = request.getParameter("name"); Job job = getCore().getScheduler().getJob(jobToShow); if (job == null) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Unknown job: " + jobToShow); return; } response.setContentType("text/html"); Writer out = response.getWriter(); out.write("<HTML><HEAD>"); out.write("\n<script>\nvar running=" + Boolean.valueOf(job.isRunning()).toString() + ";\n</script>\n"); /* * if (job.isRunning()) { * out.write("<META HTTP-EQUIV=\"refresh\" CONTENT=\"3\"/>"); } else { * out.write("<META HTTP-EQUIV=\"refresh\" CONTENT=\"6\"/>"); } */ out.write("</HEAD>"); out.write("<BODY style=\"background-color:white; font-family:sans-serif; font-size:10pt\">"); String log = job.getLog(); LineNumberReader reader = new LineNumberReader(new StringReader(log)); String line; while ((line = reader.readLine()) != null) { out.write(line); out.write("<BR/>"); } if (!job.isRunning() && job.getEndMessage() != null) { out.write("<p>"); out.write("<table border=\"1\" cellpadding=\"5\"><tr><td>"); out.write(job.getEndMessage()); out.write("</td></tr></table>"); out.write("</p>"); } out.write("<a id=\"bottomLink\" name=\"bottom\"> </a></BODY></HTML>"); }
From source file:org.agnitas.web.ImportWizardForm.java
/** * read all lines of the file/*from w w w .jav a 2 s .c o m*/ * @param aForm * @param req * @return * @throws IOException */ public int getLinesOKFromFile(HttpServletRequest req) throws IOException { String csvString = new String(this.getCsvFile().getFileData(), this.getStatus().getCharset()); LineNumberReader aReader = new LineNumberReader(new StringReader(csvString)); String myline = ""; int linesOK = 0; this.getUniqueValues().clear(); aReader.readLine(); // skip header while ((myline = aReader.readLine()) != null) { if (myline.trim().length() > 0) { if (this.parseLine(myline, (Locale) req.getSession().getAttribute(org.apache.struts.Globals.LOCALE_KEY)) != null) { linesOK++; } } } aReader.close(); return linesOK; }
From source file:com.castis.sysComp.PoisConverterSysComp.java
public void parseRegionFile(File file) throws Exception { String line = ""; FileInputStream in = null;//from w w w . ja va 2s . co m Reader isReader = null; LineNumberReader bufReader = null; FileOutputStream fos = null; String fileName = file.getName(); int index = fileName.indexOf("-"); if (index != -1) { fileName = fileName.substring(index + 1, fileName.length()); } String dir = filePolling.getValidFileDirectory(resultDir); String tempDir = dir + "/temp/"; File targetDirectory = new File(CiFileUtil.getReplaceFullPath(tempDir)); if (!targetDirectory.isDirectory()) { CiFileUtil.createDirectory(tempDir); } fos = new FileOutputStream(tempDir + fileName); int byteSize = 2048; ByteBuffer byteBuffer = ByteBuffer.allocateDirect(byteSize); GatheringByteChannel outByteCh = fos.getChannel(); try { in = new FileInputStream(file); isReader = new InputStreamReader(in, "UTF-16LE"); bufReader = new LineNumberReader(isReader); boolean first = true; while ((line = bufReader.readLine()) != null) { byte[] utf8 = line.getBytes("UTF-8"); String string = new String(utf8, "UTF-8"); String data[] = string.split("\t"); if (first == true) { first = false; if (data[0] == null || data[0].contains("region") == false) { throw new DataParsingException("data parsing error(not formatted)"); } continue; } if (data[0] == null || data[0].equals("")) { throw new DataParsingException("data parsing error(region id)"); } if (data[1] == null || data[1].equals("")) { throw new DataParsingException("data parsing error(region name)"); } if (data[2] == null || data[2].equals("")) { throw new DataParsingException("data parsing error(parent id)"); } StringBuffer strBuffer = new StringBuffer(); strBuffer.append(data[0]); strBuffer.append("\t"); strBuffer.append(data[1]); strBuffer.append("\t"); strBuffer.append(data[2]); strBuffer.append("\r\n"); byte[] outByte = null; try { outByte = strBuffer.toString().getBytes("UTF-8"); } catch (UnsupportedEncodingException e2) { e2.printStackTrace(); } byteBuffer.put(outByte); byteBuffer.flip(); try { outByteCh.write(byteBuffer); } catch (IOException e) { } byteBuffer.clear(); } fos.close(); index = fileName.indexOf("_"); String targetDir = resultDir; File sourceFile = new File(tempDir + fileName); if (index != -1) { String directory = fileName.substring(0, index); targetDir += "/" + directory; } try { File resultTargetDir = new File(CiFileUtil.getReplaceFullPath(targetDir)); if (!resultTargetDir.isDirectory()) { CiFileUtil.createDirectory(targetDir); } CiFileUtil.renameFile(sourceFile, targetDir, fileName); } catch (Exception e) { log.error(e.getMessage()); } } catch (Exception e) { String errorMsg = "Fail to parsing Line.[current line(" + bufReader.getLineNumber() + ") :" + line + "] : "; log.error(errorMsg, e); throw new DataParsingException(errorMsg, e); //throw(e); } finally { if (in != null) in.close(); if (isReader != null) isReader.close(); if (bufReader != null) bufReader.close(); } }
From source file:com.stratelia.silverpeas.silvertrace.SilverTrace.java
static public String[] getEndFileTrace(String nbLines) { LineNumberReader lnr = null;//from w w w . j a v a 2 s . c o m File theFile = new File(errorDir + "/traces.txt"); List<String> ar = new ArrayList<String>(); try { // Get file length long fileLength = theFile.length(); if (fileLength == 0L) { return ArrayUtils.EMPTY_STRING_ARRAY; } int nbl = Integer.parseInt(nbLines); lnr = new LineNumberReader(new FileReader(theFile)); if (nbl > 0) { if ((nbl + 1) * 100 < fileLength) { lnr.skip(fileLength - ((nbl + 1) * 100)); } } String line = lnr.readLine(); while (line != null) { line = lnr.readLine(); if (line != null) { ar.add(line); } } return ar.toArray(new String[ar.size()]); } catch (Exception e) { SilverTrace.error("silvertrace", "SilverTrace.getEndFileTrace()", "silvertrace.ERR_RUNTIME_ERROR_OCCUR", "File NOT FOUND :" + errorDir + "/traces.txt", e); return ArrayUtils.EMPTY_STRING_ARRAY; } finally { IOUtils.closeQuietly(lnr); } }