下面列出了org.apache.commons.io.LineIterator#next ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Returns the package name by parsing the content of the class.
*
* @param javaClassPath the path of the class to parse
* @return the package name if it could be parsed from the content
*/
private static Optional<String> getPackageNameFromClassContent(final Path javaClassPath) {
try {
final LineIterator lineIterator = FileUtils.lineIterator(javaClassPath.toFile());
while (lineIterator.hasNext()) {
final String line = lineIterator.next();
if (line.startsWith("package ")) {
return Optional.of(line.substring(8, line.indexOf(';')));
}
}
LogUtil.getLogger().warning("No Package could be parsed from the Java file content: " + javaClassPath);
} catch (IOException e) {
LogUtil.getLogger().log(Level.SEVERE,
"An error occurred during parsing the Java file content: " + javaClassPath, e);
}
return Optional.empty();
}
public InMemoryArchiveIdentificationService addMappingsFrom(File file)
{
try (FileInputStream inputStream = new FileInputStream(file))
{
LineIterator iterator = IOUtils.lineIterator(inputStream, "UTF-8");
int lineNumber = 0;
while (iterator.hasNext())
{
lineNumber++;
String line = iterator.next();
if (line.startsWith("#") || line.trim().isEmpty())
continue;
String[] parts = StringUtils.split(line, ' ');
if (parts.length < 2)
throw new IllegalArgumentException("Expected 'SHA1 GROUP_ID:ARTIFACT_ID:[PACKAGING:[COORDINATE:]]VERSION', but was: [" + line
+ "] in [" + file + "] at line [" + lineNumber + "]");
addMapping(parts[0], parts[1]);
}
}
catch (IOException e)
{
throw new WindupException("Failed to load SHA1 to " + Coordinate.class.getSimpleName() + " definitions from [" + file + "]", e);
}
return this;
}
/**
* Load the given configuration file.
*/
public static void load(File file)
{
try(FileInputStream inputStream = new FileInputStream(file))
{
LineIterator it = IOUtils.lineIterator(inputStream, "UTF-8");
while (it.hasNext())
{
String line = it.next();
if (!line.startsWith("#") && !line.trim().isEmpty())
{
add(line);
}
}
}
catch (Exception e)
{
throw new WindupException("Failed loading archive ignore patterns from [" + file.toString() + "]", e);
}
}
public static Table<String, String, Long> loadTable(InputStream stream)
throws IOException
{
Table<String, String, Long> result = TreeBasedTable.create();
LineIterator lineIterator = IOUtils.lineIterator(stream, "utf-8");
while (lineIterator.hasNext()) {
String line = lineIterator.next();
System.out.println(line);
String[] split = line.split("\t");
String language = split[0];
String license = split[1];
Long documents = Long.valueOf(split[2]);
Long tokens = Long.valueOf(split[3]);
result.put(language, "docs " + license, documents);
result.put(language, "tokens " + license, tokens);
}
return result;
}
private void waitForStartupCompleted() throws IOException {
for (int i = 0; i < 100; i++) {
if (!this.process.isAlive())
throw new IOException("Unable to start Cassandra process, more details here: " + this.logFile.getAbsolutePath());
LineIterator lines = FileUtils.lineIterator(this.logFile, UTF8Charset.get().name());
while (lines.hasNext()) {
String line = lines.next();
if (line.contains("Starting listening for CQL clients"))
return;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new IOException("Unexpected interruption", e);
}
}
throw new IOException("Cassandra process startup timeout, more details here: " + this.logFile.getAbsolutePath());
}
@Override
public void defaultBuildCommand(String eclipseProjectName, String dockerBuildContext) {
File baseDir = new File(dockerBuildContext);
InputStream response = dockerClient.buildImageCmd(baseDir).exec();
StringWriter logwriter = new StringWriter();
messageConsole.getDockerConsoleOut().println(">>> Building " + dockerBuildContext + "/Dockerfile with default options");
messageConsole.getDockerConsoleOut().println("");
try {
messageConsole.getDockerConsoleOut().flush();
LineIterator itr = IOUtils.lineIterator(response, "UTF-8");
while (itr.hasNext()) {
String line = itr.next();
logwriter.write(line);
messageConsole.getDockerConsoleOut().println(line);
messageConsole.getDockerConsoleOut().flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(response);
}
messageConsole.getDockerConsoleOut().println("");
messageConsole.getDockerConsoleOut().println("<<< Build ended");
}
@Test
public void testResolvingActual() throws Exception {
File f = Resources.asFile("data/irisSvmLight.txt");
assertTrue(f.exists());
//System.out.println(f.getAbsolutePath());
int count = 0;
try(Reader r = new BufferedReader(new FileReader(f))){
LineIterator iter = IOUtils.lineIterator(r);
while(iter.hasNext()){
String line = iter.next();
//System.out.println("LINE " + i + ": " + line);
count++;
}
}
assertEquals(12, count); //Iris normally has 150 examples; this is subset with 12
}
/**
* Check if a TAB-Sep training file is in correct format before importing
*/
private boolean isTabSepFileFormatCorrect(File aFile)
{
try {
LineIterator it = new LineIterator(new FileReader(aFile));
while (it.hasNext()) {
String line = it.next();
if (line.trim().length() == 0) {
continue;
}
if (line.split("\t").length != 2) {
return false;
}
}
}
catch (Exception e) {
return false;
}
return true;
}
/**
* @return The output slurped into a string.
*/
public static String asString(ClientResponse response) throws IOException {
StringWriter out = new StringWriter();
try {
LineIterator itr = IOUtils.lineIterator(
response.getEntityInputStream(), "UTF-8");
while (itr.hasNext()) {
String line = itr.next();
out.write(line + (itr.hasNext() ? "\n" : ""));
}
} finally {
closeQuietly(response.getEntityInputStream());
}
return out.toString();
}
public static String collectResponse(InputStream response) {
StringWriter logwriter = new StringWriter();
try {
LineIterator itr = IOUtils.lineIterator(response, "UTF-8");
while (itr.hasNext()) {
String line = (String) itr.next();
logwriter.write(line + (itr.hasNext() ? "\n" : ""));
}
response.close();
return logwriter.toString();
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
IOUtils.closeQuietly(response);
}
}
public void deserialize(final InputStream in) {
// currently, this always creates a dense dyad ranking dataset
this.clear();
try {
LineIterator input = IOUtils.lineIterator(in, StandardCharsets.UTF_8);
while (input.hasNext()) {
String row = input.next();
if (row.isEmpty()) {
break;
}
List<IDyad> dyads = new LinkedList<>();
String[] dyadTokens = row.split("\\|");
for (String dyadString : dyadTokens) {
String[] values = dyadString.split(";");
if (values[0].length() > 1 && values[1].length() > 1) {
String[] instanceValues = values[0].substring(1, values[0].length() - 1).split(",");
String[] alternativeValues = values[1].substring(1, values[1].length() - 1).split(",");
IVector instance = new DenseDoubleVector(instanceValues.length);
for (int i = 0; i < instanceValues.length; i++) {
instance.setValue(i, Double.parseDouble(instanceValues[i]));
}
IVector alternative = new DenseDoubleVector(alternativeValues.length);
for (int i = 0; i < alternativeValues.length; i++) {
alternative.setValue(i, Double.parseDouble(alternativeValues[i]));
}
IDyad dyad = new Dyad(instance, alternative);
dyads.add(dyad);
}
}
this.add(new DenseDyadRankingInstance(dyads));
}
} catch (IOException e) {
this.logger.warn(e.getMessage());
}
}
@Test
public void testNginxDockerfileBuilder() throws DockerException,
IOException {
File baseDir = new File(Thread.currentThread().getContextClassLoader()
.getResource("nginx").getFile());
ClientResponse response = dockerClient.build(baseDir);
StringWriter logwriter = new StringWriter();
try {
LineIterator itr = IOUtils.lineIterator(
response.getEntityInputStream(), "UTF-8");
while (itr.hasNext()) {
String line = itr.next();
logwriter.write(line + "\n");
LOG.info(line);
}
} finally {
IOUtils.closeQuietly(response.getEntityInputStream());
}
String fullLog = logwriter.toString();
assertThat(fullLog, containsString("Successfully built"));
String imageId = StringUtils.substringBetween(fullLog,
"Successfully built ", "\\n\"}").trim();
ImageInspectResponse imageInspectResponse = dockerClient
.inspectImage(imageId);
assertThat(imageInspectResponse, not(nullValue()));
LOG.info("Image Inspect: {}", imageInspectResponse.toString());
tmpImgs.add(imageInspectResponse.getId());
assertThat(imageInspectResponse.getAuthor(),
equalTo("Guillaume J. Charmes \"[email protected]\""));
}
private static void buildTrainFile(File aBaseFile, File aTrainFile,
List<List<String>> aPredictions)
throws IOException
{
LineIterator it = IOUtils.lineIterator(new FileReader(aBaseFile));
StringBuilder trainBuffer = new StringBuilder();
int i = 0;
while (it.hasNext()) {
String line = it.next();
if (line.trim().equals("")) {
trainBuffer.append("\n");
continue;
}
StringTokenizer st = new StringTokenizer(line, " ");
String label = "";
String feature = "";
// Except the last token, which is the label, maintain the line
while (st.hasMoreTokens()) {
feature = st.nextToken();
if (label.equals("")) { // first time
label = feature;
continue;
}
trainBuffer.append(label).append(" ");
label = feature;
}
for (List<String> prediction : aPredictions) {
trainBuffer.append(prediction.get(i)).append(" ");
}
// add its own label
trainBuffer.append(label).append("\n");
i++;
}
IOUtils.write(trainBuffer.toString(), new FileOutputStream(aTrainFile));
}
private static void buildPredictFile(File apredFt, File aPredFile,
List<List<String>> aPredictions, AnnotationFeature aFeature)
throws IOException
{
LineIterator it = IOUtils.lineIterator(new FileReader(apredFt));
StringBuilder predBuffer = new StringBuilder();
int i = 0;
while (it.hasNext()) {
String line = it.next();
if (line.trim().equals("")) {
predBuffer.append("\n");
continue;
}
StringTokenizer st = new StringTokenizer(line, " ");
// if the target feature is on multiple token, we do not need the morphological features
// in the prediction file
switch (aFeature.getLayer().getAnchoringMode()) {
case TOKENS:
predBuffer.append(st.nextToken()).append(" ");
break;
case SINGLE_TOKEN:
while (st.hasMoreTokens()) {
predBuffer.append(st.nextToken()).append(" ");
}
break;
default:
throw new IllegalStateException("Unsupported anchoring mode: ["
+ aFeature.getLayer().getAnchoringMode() + "]");
}
for (List<String> prediction : aPredictions) {
predBuffer.append(prediction.get(i)).append(" ");
}
// add its
predBuffer.append("\n");
i++;
}
IOUtils.write(predBuffer.toString(), new FileOutputStream(aPredFile));
}
@Test
public void testNetCatDockerfileBuilder() throws DockerException,
IOException, InterruptedException {
File baseDir = new File(Thread.currentThread().getContextClassLoader()
.getResource("netcat").getFile());
ClientResponse response = dockerClient.build(baseDir);
StringWriter logwriter = new StringWriter();
try {
LineIterator itr = IOUtils.lineIterator(
response.getEntityInputStream(), "UTF-8");
while (itr.hasNext()) {
String line = itr.next();
logwriter.write(line + "\n");
LOG.info(line);
}
} finally {
IOUtils.closeQuietly(response.getEntityInputStream());
}
String fullLog = logwriter.toString();
assertThat(fullLog, containsString("Successfully built"));
String imageId = StringUtils.substringBetween(fullLog,
"Successfully built ", "\\n\"}").trim();
ImageInspectResponse imageInspectResponse = dockerClient
.inspectImage(imageId);
assertThat(imageInspectResponse, not(nullValue()));
LOG.info("Image Inspect: {}", imageInspectResponse.toString());
tmpImgs.add(imageInspectResponse.getId());
ContainerConfig containerConfig = new ContainerConfig();
containerConfig.setImage(imageInspectResponse.getId());
ContainerCreateResponse container = dockerClient
.createContainer(containerConfig);
assertThat(container.getId(), not(isEmptyString()));
dockerClient.startContainer(container.getId());
tmpContainers.add(container.getId());
ContainerInspectResponse containerInspectResponse = dockerClient
.inspectContainer(container.getId());
assertThat(containerInspectResponse.getId(), notNullValue());
assertThat(containerInspectResponse.getNetworkSettings().ports,
notNullValue());
// No use as such if not running on the server
for (String portstr : containerInspectResponse.getNetworkSettings().ports
.getAllPorts().keySet()) {
Ports.Port p = containerInspectResponse.getNetworkSettings().ports
.getAllPorts().get(portstr);
int port = Integer.valueOf(p.getHostPort());
LOG.info("Checking port {} is open", port);
assertThat(available(port), is(false));
}
dockerClient.stopContainer(container.getId(), 0);
}
private String dockerfileBuild(File baseDir, String expectedText)
throws DockerException, IOException {
// Build image
ClientResponse response = dockerClient.build(baseDir);
StringWriter logwriter = new StringWriter();
try {
LineIterator itr = IOUtils.lineIterator(
response.getEntityInputStream(), "UTF-8");
while (itr.hasNext()) {
String line = itr.next();
logwriter.write(line + "\n");
LOG.info(line);
}
} finally {
IOUtils.closeQuietly(response.getEntityInputStream());
}
String fullLog = logwriter.toString();
assertThat(fullLog, containsString("Successfully built"));
String imageId = StringUtils.substringBetween(fullLog,
"Successfully built ", "\\n\"}").trim();
// Create container based on image
ContainerConfig containerConfig = new ContainerConfig();
containerConfig.setImage(imageId);
ContainerCreateResponse container = dockerClient
.createContainer(containerConfig);
LOG.info("Created container: {}", container.toString());
assertThat(container.getId(), not(isEmptyString()));
dockerClient.startContainer(container.getId());
dockerClient.waitContainer(container.getId());
tmpContainers.add(container.getId());
// Log container
ClientResponse logResponse = dockerClient.logContainer(container
.getId());
assertThat(logResponseStream(logResponse), containsString(expectedText));
return container.getId();
}
public static void main(String[] args) throws IOException {
Properties p = CommandLineUtils.getProperties(args, helpMessage, new String[]{"-i", "-l"});
File in = new File(p.getProperty("-i"));
int split = 1000;
try {
split = Integer.parseInt(p.getProperty("-l"));
} catch (NumberFormatException e) {
System.exit(1);
System.err.printf("Number of lines as given is not a number: %s\n", p.getProperty("-l"));
}
if (!in.exists()) {
System.exit(1);
System.err.printf("File %s does not exist.\n", in.getName());
}
// ok, let's split
int count = 0;
int filenum = 0;
String filePattern = FilenameUtils.getBaseName(p.getProperty("-i")) + "%03d." + FilenameUtils.getExtension(p.getProperty("-i"));
if (p.get("-d")!=null) {
filePattern = p.getProperty("-d") + '/' + filePattern;
}
String fileExec = FilenameUtils.getBaseName(p.getProperty("-i")) + "-index.sh";
String currentFileName = String.format(filePattern, filenum);
System.out.printf("Writing file %s ...\n", currentFileName);
BufferedWriter bw = new BufferedWriter(new FileWriter(currentFileName), charBufferSize);
BufferedWriter bwExec = null;
if (p.get("-x") != null) bwExec = new BufferedWriter(new FileWriter(fileExec));
addToShellFile(bwExec, currentFileName);
LineIterator lineIterator = FileUtils.lineIterator(in);
bw.write("<add>\n");
while (lineIterator.hasNext()) {
String currentLine = lineIterator.next();
if (!(currentLine.startsWith("<add>") || currentLine.startsWith("#") || currentLine.startsWith("</add>"))) {
// check if the old file is full ...
if (count >= split) {
bw.write("</add>\n");
bw.close();
currentFileName = String.format(filePattern, ++filenum);
System.out.printf("Writing file %s ...\n", currentFileName);
bw = new BufferedWriter(new FileWriter(currentFileName), charBufferSize);
bw.write("<add>\n");
count = 0;
addToShellFile(bwExec, currentFileName);
}
// write to the current file ...
bw.write(currentLine);
bw.write('\n');
count++;
}
}
bw.write("</add>\n");
bw.close();
if (bwExec != null) bwExec.close();
System.out.println("Finished.");
}
/**
* The main core logic for all testing.
*
* @param outputFileName
* @param function
* @throws IOException
*/
private void testHashes(String outputFileName, StringHashFunction function) throws IOException {
LineIterator iterator = FileUtils.lineIterator(new File(BASE_PATH + "/english-wordlist.txt"));
LineIterator results = FileUtils.lineIterator(new File(BASE_PATH + "/" + outputFileName));
int matched = 0;
int total = 0;
while(iterator.hasNext()) {
String line = iterator.next();
byte[] bytes = line.getBytes();
String computed = function.getHash(bytes);
String actual = results.next();
if(actual.contains(",")) {
// result has multiple values
String[] act = actual.split(",");
String[] com = computed.split(",");
if(act.length == com.length) {
boolean allMatch = true;
for(int index = 0; index < act.length; index++) {
allMatch = allMatch & bigMatch(act[index], com[index]);
}
if(allMatch) {
matched++;
}
}
} else {
// result has only a single value
if(actual.equals(computed)) {
matched++;
} else {
if(bigMatch(actual, computed)) {
matched++;
}
}
}
total++;
}
Assert.assertEquals("Total number of hashes did not match", total, matched);
}
private Process startKafka(String netInterface, int port, int zookeperPort) throws IOException {
if (!this.data.isDirectory())
FileUtils.forceMkdir(this.data);
Properties properties = new Properties();
properties.setProperty("broker.id", "0");
properties.setProperty("listeners", "PLAINTEXT://" + netInterface + ":" + port);
properties.setProperty("log.dirs", this.data.getAbsolutePath());
properties.setProperty("num.partitions", "1");
properties.setProperty("log.retention.hours", "8760000");
properties.setProperty("zookeeper.connect", "localhost:" + zookeperPort);
File config = new File(this.runtime, "kafka.properties");
write(properties, config);
ProcessBuilder builder = new ProcessBuilder(this.kafkaBin.getAbsolutePath(), config.getAbsolutePath());
builder.redirectError(ProcessBuilder.Redirect.appendTo(this.logFile));
builder.redirectOutput(ProcessBuilder.Redirect.appendTo(this.logFile));
Process kafka = builder.start();
boolean success = false;
try {
for (int i = 0; i < 20; i++) {
if (!kafka.isAlive())
throw new IOException("Unable to start Kafka process, more details here: " + this.logFile.getAbsolutePath());
LineIterator lines = FileUtils.lineIterator(this.logFile, UTF8Charset.get().name());
while (lines.hasNext()) {
String line = lines.next();
if (line.contains("INFO [KafkaServer id=0] started (kafka.server.KafkaServer)")) {
success = true;
return kafka;
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new IOException("Unexpected interruption", e);
}
}
throw new IOException("Kafka process startup timeout, more details here: " + this.logFile.getAbsolutePath());
} finally {
if (!success)
kafka.destroyForcibly();
}
}
public static void addTabSepTrainDocument(MiraTemplate aTemplate,
AutomationService aAutomationService)
throws IOException, UIMAException, ClassNotFoundException, AutomationException
{
File miraDir = aAutomationService.getMiraDir(aTemplate.getTrainFeature());
if (!miraDir.exists()) {
FileUtils.forceMkdir(miraDir);
}
AutomationStatus status = aAutomationService.getAutomationStatus(aTemplate);
boolean documentChanged = false;
for (TrainingDocument document : aAutomationService
.listTabSepDocuments(aTemplate.getTrainFeature().getProject())) {
if (!document.isProcessed()) {
documentChanged = true;
break;
}
}
if (!documentChanged) {
return;
}
for (TrainingDocument trainingDocument : aAutomationService.listTabSepDocuments(aTemplate
.getTrainFeature().getProject())) {
if (trainingDocument.getFeature() != null) { // This is a target layer train document
continue;
}
File trainFile = new File(miraDir, trainingDocument.getId()
+ trainingDocument.getProject().getId() + ".train");
BufferedWriter trainOut = new BufferedWriter(new FileWriter(trainFile));
File tabSepFile = new File(aAutomationService.getDocumentFolder(trainingDocument),
trainingDocument.getName());
LineIterator it = IOUtils.lineIterator(new FileReader(tabSepFile));
while (it.hasNext()) {
String line = it.next();
if (line.trim().equals("")) {
trainOut.append("\n");
}
else {
StringTokenizer st = new StringTokenizer(line, "\t");
if (st.countTokens() != 2) {
trainOut.close();
throw new AutomationException("This is not a valid TAB-SEP document");
}
trainOut.append(getMiraLineForTabSep(st.nextToken(), st.nextToken()));
}
}
trainingDocument.setProcessed(false);
status.setTrainDocs(status.getTrainDocs() - 1);
trainOut.close();
}
}