下面列出了java.io.LineNumberReader#readLine ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private String getErrorContext(int actualLine) throws IOException {
int minLine = Math.max(0, actualLine -1);
int maxLine = Math.min(getLinesInSource(), actualLine + 1);
LineNumberReader r = new LineNumberReader(new StringReader(templateSource.toString()));
int lineNr;
StringBuilder result = new StringBuilder();
while ((lineNr = r.getLineNumber()+1) <= maxLine) {
String line = r.readLine();
if (lineNr < minLine) continue;
String nr = Integer.toString(lineNr);
if (lineNr == actualLine) {
nr = " --> " + nr;
}
result.append(padLeft(nr, 10));
result.append(": ");
result.append(line);
result.append('\n');
}
return result.toString();
}
public static String loadOptions(FileReader fr, String filename) {
LineNumberReader lnr = new LineNumberReader(fr);
Group.restoreAllDefaults();
String line;
try {
while ((line = lnr.readLine()) != null) {
String reason = Group.root.setOption(line);
if (reason != null) {
System.err.println("Option "+line+
" at line "+lnr.getLineNumber()+
" ignored: "+reason);
}
}
} catch (IOException e) {
Group.restoreAllDefaults();
return ("IO Error reading "+filename+
" at line "+lnr.getLineNumber());
}
return null;
}
/**
* Read a script from the provided {@code LineNumberReader}, using the supplied
* comment prefix and statement separator, and build a {@code String} containing
* the lines.
* <p>Lines <em>beginning</em> with the comment prefix are excluded from the
* results; however, line comments anywhere else — for example, within
* a statement — will be included in the results.
* @param lineNumberReader the {@code LineNumberReader} containing the script
* to be processed
* @param commentPrefix the prefix that identifies comments in the SQL script —
* typically "--"
* @param separator the statement separator in the SQL script — typically ";"
* @return a {@code String} containing the script lines
* @throws IOException in case of I/O errors
*/
public static String readScript(LineNumberReader lineNumberReader, String commentPrefix, String separator)
throws IOException {
String currentStatement = lineNumberReader.readLine();
StringBuilder scriptBuilder = new StringBuilder();
while (currentStatement != null) {
if (commentPrefix != null && !currentStatement.startsWith(commentPrefix)) {
if (scriptBuilder.length() > 0) {
scriptBuilder.append('\n');
}
scriptBuilder.append(currentStatement);
}
currentStatement = lineNumberReader.readLine();
}
appendSeparatorToScriptIfNecessary(scriptBuilder, separator);
return scriptBuilder.toString();
}
/**
* Read a script from the provided {@code LineNumberReader}, using the supplied
* comment prefix and statement separator, and build a {@code String} containing
* the lines.
* <p>Lines <em>beginning</em> with the comment prefix are excluded from the
* results; however, line comments anywhere else — for example, within
* a statement — will be included in the results.
* @param lineNumberReader the {@code LineNumberReader} containing the script
* to be processed
* @param commentPrefix the prefix that identifies comments in the SQL script —
* typically "--"
* @param separator the statement separator in the SQL script — typically ";"
* @return a {@code String} containing the script lines
* @throws IOException in case of I/O errors
*/
public static String readScript(LineNumberReader lineNumberReader, String commentPrefix, String separator)
throws IOException {
String currentStatement = lineNumberReader.readLine();
StringBuilder scriptBuilder = new StringBuilder();
while (currentStatement != null) {
if (commentPrefix != null && !currentStatement.startsWith(commentPrefix)) {
if (scriptBuilder.length() > 0) {
scriptBuilder.append('\n');
}
scriptBuilder.append(currentStatement);
}
currentStatement = lineNumberReader.readLine();
}
appendSeparatorToScriptIfNecessary(scriptBuilder, separator);
return scriptBuilder.toString();
}
private ResponseHandler<Object> createExecResponseHandler(LogOutputSpec outputSpec) throws FileNotFoundException {
final LogCallback callback = new DefaultLogCallback(outputSpec);
return new ResponseHandler<Object>() {
@Override
public Object handleResponse(HttpResponse response) throws IOException {
try (InputStream stream = response.getEntity().getContent()) {
LineNumberReader reader = new LineNumberReader(new InputStreamReader(stream));
String line;
try {
callback.open();
while ( (line = reader.readLine()) != null) {
callback.log(1, new Timestamp(), line);
}
} catch (LogCallback.DoneException e) {
// Ok, we stop here ...
} finally {
callback.close();
}
}
return null;
}
};
}
/**
* Write the response body on stdout.
*
* @param conn
* The connection.
*
* @throws Exception
*/
protected void showResults(final HttpURLConnection conn)
throws Exception {
final LineNumberReader r = new LineNumberReader(
new InputStreamReader(conn.getInputStream(), conn
.getContentEncoding() == null ? "ISO-8859-1" : conn
.getContentEncoding()));
try {
String s;
while ((s = r.readLine()) != null) {
System.out.println(s);
}
} finally {
r.close();
// conn.disconnect();
}
}
public Boolean call() {
String line = null;
boolean result = false;
try {
Process process = Runtime.getRuntime().exec("ping "+this.ipAddress);
LineNumberReader lineNumberReader = new LineNumberReader(new InputStreamReader(process.getInputStream(),"gbk"));
int count = 1;
while ((line = lineNumberReader.readLine()) != null) {
if(line.contains("TTL")){
result = true;
break;
}
if(count>2){//只需尝试三次即可(为什么大于2?因为lineNumberReader.readLine()时就已经有一次了)
break;
}
count++;
}
lineNumberReader.close();
process.destroy();
} catch (IOException e) {
}
return result;
}
@Test
public void testYoutubeChannelProvider() throws Exception {
String configfile = "./target/test-classes/YoutubeChannelProviderIT.conf";
String outfile = "./target/test-classes/YoutubeChannelProviderIT.stdout.txt";
String[] args = new String[2];
args[0] = configfile;
args[1] = outfile;
File confFile = new File(configfile);
assert (confFile.exists());
assert (confFile.canRead());
assert (confFile.isFile());
Thread testThread = new Thread(() -> {
try {
YoutubeChannelProvider.main(args);
} catch ( Exception ex ) {
LOGGER.error("Test Exception!", ex);
}
});
testThread.start();
testThread.join(60000);
File out = new File(outfile);
assertTrue (out.exists());
assertTrue (out.canRead());
assertTrue (out.isFile());
FileReader outReader = new FileReader(out);
LineNumberReader outCounter = new LineNumberReader(outReader);
while (outCounter.readLine() != null) {}
assertTrue (outCounter.getLineNumber() >= 1);
}
private Set<IString> loadCountsFile(String filename) {
Counter<IString> counter = new ClassicCounter<IString>();
LineNumberReader reader = IOTools.getReaderFromFile(filename);
try {
for (String line; (line = reader.readLine()) != null;) {
String[] fields = line.trim().split("\\s+");
if (fields.length == 2) {
String wordType = fields[0];
if ( ! (TokenUtils.isNumericOrPunctuationOrSymbols(wordType) ||
wordType.equals(TokenUtils.START_TOKEN.toString()) ||
wordType.equals(TokenUtils.END_TOKEN.toString()))) {
counter.setCount(new IString(wordType), Double.valueOf(fields[1]));
}
} else {
System.err.printf("%s: Discarding line %s%n", this.getClass().getName(), line);
}
}
reader.close();
Set<IString> set = new HashSet<>(Counters.topKeys(counter, rankCutoff));
for (IString word : set) {
System.err.printf(" %s%n", word);
}
return set;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private List read(File f) throws IOException {
List list = new ArrayList();
LineNumberReader lr = new LineNumberReader(new FileReader(f));
String line = null;
while ((line = lr.readLine()) != null) {
String[] s = line.split("\t");
// logger.debug((i++) + " " + s[0]);
list.add(new Double(s[0]));
}
lr.close();
return list;
}
@Test(groups = "InstagramRecentMediaProviderIT")
public void testInstagramRecentMediaProvider() throws Exception {
String configfile = "./target/test-classes/InstagramRecentMediaProviderIT.conf";
String outfile = "./target/test-classes/InstagramRecentMediaProviderIT.stdout.txt";
String[] args = new String[2];
args[0] = configfile;
args[1] = outfile;
Thread testThread = new Thread(() -> {
try {
InstagramRecentMediaProvider.main(args);
} catch ( Exception ex ) {
LOGGER.error("Test Exception!", ex);
}
});
testThread.start();
testThread.join(60000);
File out = new File(outfile);
Assert.assertTrue(out.exists());
Assert.assertTrue(out.canRead());
Assert.assertTrue(out.isFile());
FileReader outReader = new FileReader(out);
LineNumberReader outCounter = new LineNumberReader(outReader);
while (outCounter.readLine() != null) {}
assertThat(outCounter.getLineNumber(), Matchers.greaterThanOrEqualTo(1));
}
private String[] readAnswer(File ans, int size) throws Exception
{
LineNumberReader lr = new LineNumberReader(new FileReader(ans));
int i = 0;
String line = null;
String[] a = new String[size];
while ((line = lr.readLine()) != null)
a[i++] = line;
return a;
}
private TsvFormatHeader readFormat(LineNumberReader aIn) throws IOException
{
String line = aIn.readLine();
expectStartsWith(line, HEADER_PREFIX_FORMAT);
Matcher m = FORMAT_PATTERN.matcher(line);
if (!m.matches()) {
throw new IOException("Illlegal format header: [" + line + "]");
}
TsvFormatHeader format = new TsvFormatHeader(m.group("NAME"), m.group("VERSION"));
return format;
}
@Test(dataProvider = "TwitterFollowingProviderIT")
public void testTwitterFollowingProvider(String endpoint, Boolean ids_only, Integer max_items) throws Exception {
String configfile = "./target/test-classes/TwitterFollowingProviderIT.conf";
String outfile = "./target/test-classes/TwitterFollowingProviderIT-"+endpoint+"-"+ids_only+".stdout.txt";
String[] args = new String[2];
args[0] = configfile;
args[1] = outfile;
File conf = new File(configfile);
Assert.assertTrue (conf.exists());
Assert.assertTrue (conf.canRead());
Assert.assertTrue (conf.isFile());
System.setProperty("ENDPOINT", endpoint);
System.setProperty("IDS_ONLY", Boolean.toString(ids_only));
System.setProperty("MAX_ITEMS", Integer.toString(max_items));
ConfigFactory.invalidateCaches();
StreamsConfigurator.setConfig(ConfigFactory.load());
Thread testThread = new Thread(() -> {
try {
TwitterFollowingProvider.main(args);
} catch ( Exception ex ) {
LOGGER.error("Test Exception!", ex);
}
});
testThread.start();
testThread.join(60000);
File out = new File(outfile);
Assert.assertTrue (out.exists());
Assert.assertTrue (out.canRead());
Assert.assertTrue (out.isFile());
FileReader outReader = new FileReader(out);
LineNumberReader outCounter = new LineNumberReader(outReader);
while (outCounter.readLine() != null) {}
Assert.assertEquals (outCounter.getLineNumber(), max_items.intValue());
}
/**
* 读取cpu相关信息
* @param proc
* @return
*/
private static long[] readCpu(final Process proc)
{
long[] retn = new long[2];
try
{
proc.getOutputStream().close();
InputStreamReader ir = new InputStreamReader(proc.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
String line = input.readLine();
if (line == null || line.length() < FAULTLENGTH)
{
return null;
}
int capidx = line.indexOf("Caption");
int cmdidx = line.indexOf("CommandLine");
int rocidx = line.indexOf("ReadOperationCount");
int umtidx = line.indexOf("UserModeTime");
int kmtidx = line.indexOf("KernelModeTime");
int wocidx = line.indexOf("WriteOperationCount");
long idletime = 0;
long kneltime = 0;
long usertime = 0;
while ((line = input.readLine()) != null)
{
if (line.length() < wocidx)
{
continue;
}
// 字段出现顺序:Caption,CommandLine,KernelModeTime,ReadOperationCount,
// ThreadCount,UserModeTime,WriteOperation
String caption = substring(line, capidx, cmdidx - 1).trim();
String cmd = substring(line, cmdidx, kmtidx - 1).trim();
if (cmd.indexOf("wmic.exe") >= 0)
{
continue;
}
String s1 = substring(line, kmtidx, rocidx - 1).trim();
String s2 = substring(line, umtidx, wocidx - 1).trim();
if (caption.equals("System Idle Process") || caption.equals("System"))
{
if (s1.length() > 0)
idletime += Long.valueOf(s1).longValue();
if (s2.length() > 0)
idletime += Long.valueOf(s2).longValue();
continue;
}
if (s1.length() > 0)
kneltime += Long.valueOf(s1).longValue();
if (s2.length() > 0)
usertime += Long.valueOf(s2).longValue();
}
retn[0] = idletime;
retn[1] = kneltime + usertime;
return retn;
}
catch (Exception ex)
{
ex.printStackTrace();
}
finally
{
try
{
proc.getInputStream().close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
return null;
}
/**
*
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
if (args.length < 2) {
System.err.print(usage());
System.exit(-1);
}
Properties options = StringUtils.argsToProperties(args, argDefs());
boolean disableTokenization = PropertiesUtils.getBool(options, "no-nist", false);
boolean doCased = PropertiesUtils.getBool(options, "cased", false);
// Setup the metric tokenization scheme. Applies to both the references and
// hypotheses
if (doCased) NISTTokenizer.lowercase(false);
NISTTokenizer.normalize( ! disableTokenization);
// Load the references
String[] parsedArgs = options.getProperty("").split("\\s+");
final String evalMetric = parsedArgs[0];
String[] refs= Arrays.copyOfRange(parsedArgs, 1, parsedArgs.length);
final List<List<Sequence<IString>>> references = MetricUtils.readReferences(refs, true);
System.out.printf("Metric: %s with %d references%n", evalMetric, refs.length);
EvaluationMetric<IString,String> metric = CorpusLevelMetricFactory.newMetric(evalMetric, references);
IncrementalEvaluationMetric<IString,String> incMetric = metric.getIncrementalMetric();
LineNumberReader reader = new LineNumberReader(new InputStreamReader(
System.in));
for (String line; (line = reader.readLine()) != null; ) {
line = NISTTokenizer.tokenize(line);
Sequence<IString> translation = IStrings.tokenize(line);
ScoredFeaturizedTranslation<IString, String> tran = new ScoredFeaturizedTranslation<>(
translation, null, 0);
incMetric.add(tran);
}
// Check for an incomplete set of translations
if (reader.getLineNumber() < references.size()) {
System.err.printf("WARNING: Translation candidate file is shorter than references (%d/%d)%n",
reader.getLineNumber(), references.size());
}
reader.close();
System.out.printf("%s = %.3f%n", evalMetric, 100 * Math.abs(incMetric.score()));
System.out.printf("Details:%n%s%n", incMetric.scoreDetails());
}
/**
* Compares a log file with its reference log.
*
* <p>Usually, the log file and the reference log are in the same directory,
* one ending with '.log' and the other with '.ref'.
*
* <p>If the files are identical, removes logFile.
*
* @param logFile Log file
* @param refFile Reference log
*/
protected void diffFile(File logFile, File refFile) throws IOException {
int n = 0;
BufferedReader logReader = null;
BufferedReader refReader = null;
try {
// NOTE: Use of diff.mask is deprecated, use diff_mask.
String diffMask = System.getProperty("diff.mask", null);
if (diffMask != null) {
addDiffMask(diffMask);
}
diffMask = System.getProperty("diff_mask", null);
if (diffMask != null) {
addDiffMask(diffMask);
}
logReader = Util.reader(logFile);
refReader = Util.reader(refFile);
LineNumberReader logLineReader = new LineNumberReader(logReader);
LineNumberReader refLineReader = new LineNumberReader(refReader);
for (;;) {
String logLine = logLineReader.readLine();
String refLine = refLineReader.readLine();
while ((logLine != null) && matchIgnorePatterns(logLine)) {
// System.out.println("logMatch Line:" + logLine);
logLine = logLineReader.readLine();
}
while ((refLine != null) && matchIgnorePatterns(refLine)) {
// System.out.println("refMatch Line:" + logLine);
refLine = refLineReader.readLine();
}
if ((logLine == null) || (refLine == null)) {
if (logLine != null) {
diffFail(
logFile,
logLineReader.getLineNumber());
}
if (refLine != null) {
diffFail(
logFile,
refLineReader.getLineNumber());
}
break;
}
logLine = applyDiffMask(logLine);
refLine = applyDiffMask(refLine);
if (!logLine.equals(refLine)) {
diffFail(
logFile,
logLineReader.getLineNumber());
}
}
} finally {
if (logReader != null) {
logReader.close();
}
if (refReader != null) {
refReader.close();
}
}
// no diffs detected, so delete redundant .log file
logFile.delete();
}
/**
* Reads a matrix from a reader. The first line in the file should
* contain the number of rows and columns. Subsequent lines
* contain elements of the matrix.
* (FracPete: taken from old keel.Algorithms.Statistical_Classifiers.Logistic.core.Matrix class)
*
* @param r the reader containing the matrix
* @throws Exception if an error occurs
* @see #write(Writer)
*/
public Matrix(Reader r) throws Exception {
LineNumberReader lnr = new LineNumberReader(r);
String line;
int currentRow = -1;
while ((line = lnr.readLine()) != null) {
// Comments
if (line.startsWith("%"))
continue;
StringTokenizer st = new StringTokenizer(line);
// Ignore blank lines
if (!st.hasMoreTokens())
continue;
if (currentRow < 0) {
int rows = Integer.parseInt(st.nextToken());
if (!st.hasMoreTokens())
throw new Exception("Line " + lnr.getLineNumber()
+ ": expected number of columns");
int cols = Integer.parseInt(st.nextToken());
A = new double[rows][cols];
m = rows;
n = cols;
currentRow++;
continue;
}
else {
if (currentRow == getRowDimension())
throw new Exception("Line " + lnr.getLineNumber()
+ ": too many rows provided");
for (int i = 0; i < getColumnDimension(); i++) {
if (!st.hasMoreTokens())
throw new Exception("Line " + lnr.getLineNumber()
+ ": too few matrix elements provided");
set(currentRow, i, Double.valueOf(st.nextToken()).doubleValue());
}
currentRow++;
}
}
if (currentRow == -1)
throw new Exception("Line " + lnr.getLineNumber()
+ ": expected number of rows");
else if (currentRow != getRowDimension())
throw new Exception("Line " + lnr.getLineNumber()
+ ": too few rows provided");
}
/**
* Read a DSL file and convert it to a String. Comment lines are removed.
* Split lines are joined, inserting a space for an EOL, but maintaining the
* original number of lines by inserting EOLs. Options are recognized.
* Keeps track of original line lengths for fixing parser error messages.
* @param reader for the DSL file data
* @return the transformed DSL file
* @throws IOException
*/
private String readFile( Reader reader ) throws IOException {
lineLengths = new ArrayList<Integer>();
lineLengths.add( null );
LineNumberReader lnr = new LineNumberReader( reader );
StringBuilder sb = new StringBuilder();
int nlCount = 0;
boolean inEntry = false;
String line;
while( (line = lnr.readLine()) != null ){
lineLengths.add( line.length() );
Matcher commentMat = commentPat.matcher( line );
if( commentMat.matches() ){
if( inEntry ){
nlCount++;
} else {
sb.append( '\n' );
}
if( "#/".equals( commentMat.group( 2 ) ) ){
String[] options = commentMat.group( 1 ).substring( 2 ).trim().split( "\\s+" );
for( String option: options ){
optionSet.add( option );
}
}
continue;
}
if( entryPat.matcher( line ).matches() ){
if( inEntry ){
for( int i = 0; i < nlCount; i++ ) sb.append( '\n' );
}
sb.append( line );
nlCount = 1;
inEntry = true;
continue;
}
sb.append( ' ').append( line );
nlCount++;
}
if( inEntry ) sb.append( '\n' );
lnr.close();
// logger.info( "====== DSL definition:" );
// logger.info( sb.toString() );
return sb.toString();
}
public NMONDataSet parse(String datasetName, Reader reader, TimeZone timeZone, boolean scaleProcessesByCPU)
throws IOException {
long start = System.nanoTime();
this.scaleProcessesByCPU = scaleProcessesByCPU;
in = new LineNumberReader(reader);
try {
data = new NMONDataSet(datasetName);
NMON_FORMAT.setTimeZone(timeZone);
data.setMetadata("parsed_gmt_offset",
Double.toString(timeZone.getOffset(System.currentTimeMillis()) / 3600000.0d));
String line = parseHeaders();
// no timestamp records after the headers => no other data
if ((line == null) || !line.startsWith("ZZZZ")) {
throw new IOException("file '" + datasetName + "' does not appear to have any data records");
}
// else line contains the first timestamp record, so start parsing
for (DataPostProcessor processor : processors) {
processor.addDataTypes(data);
}
do {
parseLine(line);
} while ((line = in.readLine()) != null);
// save file's system info
for (String name : systemInfo.keySet()) {
String value = systemInfo.get(name).toString();
data.setSystemInfo(name, value);
}
// final record completes when the file is completely read
if (currentRecord != null) {
completeCurrentRecord();
}
DataHelper.aggregateProcessData(data, LOGGER);
return data;
}
finally {
if (in != null) {
try {
in.close();
}
catch (Exception e) {
// ignore
}
in = null;
}
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Parse complete for {} in {}ms", data.getSourceFile(),
(System.nanoTime() - start) / 1000000.0d);
}
data = null;
currentRecord = null;
topFields = null;
topCommandIndex = -1;
summaryFields = null;
fileCPUs = 1;
seenFirstDataType = false;
isAIX = false;
processes.clear();
systemInfo.clear();
transforms.clear();
}
}