Java Code Examples for org.apache.commons.io.IOUtils.lineIterator()

The following are Jave code examples for showing how to use lineIterator() of the org.apache.commons.io.IOUtils class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: AndroidApktool   File: AndrolibResources.java   View Source Code Vote up 6 votes
public boolean detectWhetherAppIsFramework(File appDir)
        throws AndrolibException {
    File publicXml = new File(appDir, "res/values/public.xml");
    if (!publicXml.exists()) {
        return false;
    }

    Iterator<String> it;
    try {
        it = IOUtils.lineIterator(new FileReader(new File(appDir,
                "res/values/public.xml")));
    } catch (FileNotFoundException ex) {
        throw new AndrolibException(
                "Could not detect whether app is framework one", ex);
    }
    it.next();
    it.next();
    return it.next().contains("0x01");
}
 
Example 2
Project: vind   File: CollectionManagementService.java   View Source Code Vote up 6 votes
protected List<String> listRuntimeDependencies(String collectionName) throws IOException, SolrServerException {
    ModifiableSolrParams params = new ModifiableSolrParams().set("file",RUNTIME_LIB_FILE_NAME);
    SolrRequest request = new QueryRequest(params);
    request.setPath("/admin/file");
    request.setResponseParser(new InputStreamResponseParser("json"));

    NamedList o = client.request(request, collectionName);

    LineIterator it = IOUtils.lineIterator((InputStream) o.get("stream"), "utf-8");

    List<String> returnValues = Streams.stream(it).collect(Collectors.toList());

    //if file not exists (a little hacky..)
    if(returnValues.size() == 1 && returnValues.get(0).startsWith("{\"responseHeader\":{\"status\":404")) {
        logger.warn("Release does not yet contain rumtimelib configuration file. Runtimelibs have to be installed manually.");
        return Collections.emptyList();
    };
    return returnValues;
}
 
Example 3
Project: flume-release-1.7.0   File: TestNetcatSource.java   View Source Code Vote up 6 votes
/**
 * Test that line above MaxLineLength are discarded
 *
 * @throws InterruptedException
 * @throws IOException
 */
@Test
public void testMaxLineLengthwithAck() throws InterruptedException, IOException {
  String encoding = "UTF-8";
  String ackEvent = "OK";
  String ackErrorEvent = "FAILED: Event exceeds the maximum length (10 chars, including newline)";
  startSource(encoding, "true", "1", "10");
  Socket netcatSocket = new Socket(localhost, selectedPort);
  LineIterator inputLineIterator = IOUtils.lineIterator(netcatSocket.getInputStream(), encoding);
  try {
    sendEvent(netcatSocket, "123456789", encoding);
    Assert.assertArrayEquals("Channel contained our event",
                             "123456789".getBytes(defaultCharset), getFlumeEvent());
    Assert.assertEquals("Socket contained the Ack", ackEvent, inputLineIterator.nextLine());
    sendEvent(netcatSocket, english, encoding);
    Assert.assertEquals("Channel does not contain an event", null, getRawFlumeEvent());
    Assert.assertEquals("Socket contained the Error Ack", ackErrorEvent, inputLineIterator
        .nextLine());
  } finally {
    netcatSocket.close();
    stopSource();
  }
}
 
Example 4
Project: smarti   File: PosCollector.java   View Source Code Vote up 6 votes
private Set<String> loadWords() throws IOException {
    Set<String> words = new HashSet<>();
    
    try (Reader r = new InputStreamReader(getClass().getClassLoader().getResourceAsStream(IGNORED_RESOURCE),UTF8)){
        Iterator<String> lines = IOUtils.lineIterator(r);
        while(lines.hasNext()){
            String word = StringUtils.trimToNull(lines.next());
            if(word != null){
                words.add(word.toLowerCase(Locale.ROOT));
            }
        }
    } catch (NullPointerException e) {
        log.warn("Wordlist for ignored adjectives (resource: {}) not present", IGNORED_RESOURCE);
        log.debug("EXCEPTION", e);
    }
    return words;
}
 
Example 5
Project: devops-cstack   File: JSONClient.java   View Source Code Vote up 5 votes
public DockerResponse sendGet(URI uri) throws JSONClientException {

        if (logger.isDebugEnabled()) {
            logger.debug("Send a get request to : " + uri);
        }
        StringBuilder builder = new StringBuilder();


        HttpGet httpGet = new HttpGet(uri);
        HttpResponse response = null;
        try {
            CloseableHttpClient httpclient = buildSecureHttpClient();
            response = httpclient.execute(httpGet);
            LineIterator iterator = IOUtils.lineIterator(response.getEntity()
                    .getContent(), "UTF-8");
            while (iterator.hasNext()) {
                builder.append(iterator.nextLine());
            }
        } catch (IOException e) {
            throw new JSONClientException("Error in sendGet method due to : " + e.getMessage(), e);
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Status code : " + response.getStatusLine().getStatusCode());
            logger.debug("Server response : " + builder.toString());
        }

        return new DockerResponse(response.getStatusLine().getStatusCode(), builder.toString());

    }
 
Example 6
Project: csap-core   File: Docker_Java.java   View Source Code Vote up 5 votes
public static String consumeAsString ( InputStream response, int maxLines ) {

		StringWriter logwriter = new StringWriter();

		try {
			LineIterator itr = IOUtils.lineIterator( response, "UTF-8" );

			int numLines = 0;
			while (itr.hasNext() && numLines < maxLines) {
				String line = itr.next();
				logwriter.write( line + (itr.hasNext() ? "\n" : "") );
				numLines++;
				System.out.println( line );
				// logger.info( line);
				// LOG.info("line: " + line);
			}
			while (itr.hasNext())
				itr.next();
			// response.close();

		} catch (Exception e) {
			logger.error( "Failed to close: {}",
				CSAP.getCsapFilteredStackTrace( e ) );
			return "Failed";
		} finally {
			IOUtils.closeQuietly( response );
		}
		return logwriter.toString();
	}
 
Example 7
Project: flume-release-1.7.0   File: TestNetcatSource.java   View Source Code Vote up 5 votes
/**
 * Test if an ack is sent for every event in the correct encoding
 *
 * @throws InterruptedException
 * @throws IOException
 */
@Test
public void testAck() throws InterruptedException, IOException {
  String encoding = "UTF-8";
  String ackEvent = "OK";
  startSource(encoding, "true", "1", "512");
  Socket netcatSocket = new Socket(localhost, selectedPort);
  LineIterator inputLineIterator = IOUtils.lineIterator(netcatSocket.getInputStream(), encoding);
  try {
    // Test on english text snippet
    for (int i = 0; i < 20; i++) {
      sendEvent(netcatSocket, english, encoding);
      Assert.assertArrayEquals("Channel contained our event", english.getBytes(defaultCharset),
          getFlumeEvent());
      Assert.assertEquals("Socket contained the Ack", ackEvent, inputLineIterator.nextLine());
    }
    // Test on french text snippet
    for (int i = 0; i < 20; i++) {
      sendEvent(netcatSocket, french, encoding);
      Assert.assertArrayEquals("Channel contained our event", french.getBytes(defaultCharset),
          getFlumeEvent());
      Assert.assertEquals("Socket contained the Ack", ackEvent, inputLineIterator.nextLine());
    }
  } finally {
    netcatSocket.close();
    stopSource();
  }
}
 
Example 8
Project: OperatieBRP   File: AfnemerindicatieConversie.java   View Source Code Vote up 5 votes
private long converteerPersafnemerindicatieTabel(final long maxAfnemerindicatieId) throws IOException {
    afnemerindicatieDatabaseInteractieStrategy.dumpAfnemerindicatieTabelNaarFile(persafnemerindicatieOudFile);
    try (OutputStream os = new BufferedOutputStream(new FileOutputStream(persafnemerindicatieNieuwFile))) {
        try (InputStream is = new BufferedInputStream(new FileInputStream(persafnemerindicatieOudFile))) {
            final LineIterator it = IOUtils.lineIterator(is, StandardCharsets.UTF_8);
            converteerAfnemerindicatiebestand(maxAfnemerindicatieId, os, it);
        }
    }
    afnemerindicatieDatabaseInteractieStrategy.vulAfnemerindicatieTabel(persafnemerindicatieNieuwFile);
    return maxAfnemerindicatieId;
}
 
Example 9
Project: OperatieBRP   File: AfnemerindicatieConversie.java   View Source Code Vote up 5 votes
private void converteerHisPersafnemerindicatieTabel(final long maxAfnemerindicatieId)
        throws IOException {
    afnemerindicatieDatabaseInteractieStrategy.dumpHisAfnemerindicatieTabel(hisPersafnemerindicatieOudFile);
    final long maxHisAfnemerindicatieId = bepaalMaxId(PersoonAfnemerindicatieHistorie.class).longValue();
    try (OutputStream os = new BufferedOutputStream(new FileOutputStream(hisPersafnemerindicatieNieuwFile))) {
        try (InputStream is = new BufferedInputStream(new FileInputStream(hisPersafnemerindicatieOudFile))) {
            final LineIterator it = IOUtils.lineIterator(is, StandardCharsets.UTF_8);
            converteerHisAfnemerindicatiebestand(maxAfnemerindicatieId, maxHisAfnemerindicatieId, os, it);
        }
    }
    afnemerindicatieDatabaseInteractieStrategy.vulHisAfnemerindicatieTabel(hisPersafnemerindicatieNieuwFile);
}
 
Example 10
Project: iab-spiders-and-robots-java-client   File: IpRanges.java   View Source Code Vote up 5 votes
private void parseRecords(InputStream stream) throws IOException {
    LineIterator it = IOUtils.lineIterator(buffer(stream), IabFile.CHARSET);
    while (it.hasNext()) {
        String record = StringUtils.trimToNull(it.nextLine());
        if (record != null) {
            if (isCidrNotation(record)) {
                cidrAddresses.add(subnetInfo(record));
            } else {
                plainAddresses.add(record);
            }
        }
    }
}
 
Example 11
Project: dhus-core   File: SearchController.java   View Source Code Vote up 4 votes
/**
 * Provides the openSearch description file via /search/description API.
 *
 * @param res response
 * @throws IOException if file description cannot be accessed
 */
@PreAuthorize("hasRole('ROLE_SEARCH')")
@RequestMapping(value = "/description")
public void search(HttpServletResponse res) throws IOException
{
   String url = configurationManager.getServerConfiguration().getExternalUrl();
   if (url != null && url.endsWith("/"))
   {
      url = url.substring(0, url.length() - 1);
   }

   String long_name = configurationManager.getNameConfiguration().getLongName();
   String short_name = configurationManager.getNameConfiguration().getShortName();
   String contact_mail = configurationManager.getSupportConfiguration().getMail();

   InputStream is = ClassLoader.getSystemResourceAsStream(DESCRIPTION_FILE);
   if (is == null)
   {
      throw new IOException("Cannot find \"" + DESCRIPTION_FILE
            + "\" OpenSearch description file.");
   }

   LineIterator li = IOUtils.lineIterator(is, "UTF-8");

   try (ServletOutputStream os = res.getOutputStream())
   {
      while (li.hasNext())
      {
         String line = li.next();
         // Last line? -> the iterator eats LF
         if (li.hasNext())
         {
            line = line + "\n";
         }

         line = line.replace("[dhus_server]", url);
         if (long_name != null)
         {
            line = line.replace("[dhus_long_name]", long_name);
         }
         if (short_name != null)
         {
            line = line.replace("[dhus_short_name]", short_name);
         }
         if (contact_mail != null)
         {
            line = line.replace("[dhus_contact_mail]", contact_mail);
         }

         os.write(line.getBytes());
      }
   }
   finally
   {
      IOUtils.closeQuietly(is);
      LineIterator.closeQuietly(li);
   }
}
 
Example 12
Project: xtf   File: ProxiedConnectionManager.java   View Source Code Vote up 4 votes
private void createTCPProxy() {
	try {
		final String cleanCommand = String.format("sudo docker ps -a | grep 'hours ago' | awk '{print $1}' | xargs --no-run-if-empty sudo docker rm",
				containerName,
				this.targetHost,
				this.targetPort,
				sourceIPs,
				ImageRegistry.get().tcpProxy());
		LOGGER.info("Establishing SSH shell");
		ssh = SshUtil.getInstance()
				.createSshSession(TestConfiguration.proxyHostUsername(), getProxyHost());
		ssh.connect();
		LOGGER.debug("Connected to ssh console");

		final ChannelExec cleanChannel = (ChannelExec) ssh.openChannel("exec");
		cleanChannel.setPty(true);
		cleanChannel.setCommand(cleanCommand);
		cleanChannel.setInputStream(null);
		cleanChannel.setOutputStream(System.err);

		LOGGER.debug("Executing command: '{}'", cleanCommand);
		cleanChannel.connect();
		cleanChannel.disconnect();

		final String command = String.format("sudo docker run -it --name %s --net=host --rm -e AB_OFF=true -e TARGET_HOST=%s -e TARGET_PORT=%s -e TARGET_VIA=%s %s",
				containerName,
				this.targetHost,
				this.targetPort,
				sourceIPs,
				ImageRegistry.get().tcpProxy());
		LOGGER.info("Establishing SSH shell");
		ssh = SshUtil.getInstance()
				.createSshSession(TestConfiguration.proxyHostUsername(), getProxyHost());
		ssh.connect();
		LOGGER.debug("Connected to ssh console");

		final ChannelExec channel = (ChannelExec) ssh.openChannel("exec");
		channel.setPty(true);
		channel.setCommand(command);
		channel.setInputStream(null);
		channel.setOutputStream(System.err);

		LOGGER.debug("Executing command: '{}'", command);
		channel.connect();
		final LineIterator li = IOUtils.lineIterator(new InputStreamReader(channel.getInputStream()));
		final Pattern portLine = Pattern.compile(".*Listening on port ([0-9]*).*$");
		listenPort = 0;
		while (li.hasNext()) {
			final String line = li.next();
			LOGGER.trace("Shell line: {}", line);
			final Matcher m = portLine.matcher(line);
			if (m.matches()) {
				listenPort = Integer.parseInt(m.group(1));
				LOGGER.info("Connection listening on port {}", listenPort);
				break;
			}
		}
		channel.disconnect();
	} catch (final JSchException | IOException e) {
		LOGGER.debug("Error in creating SSH connection to proxy host", e);
		throw new IllegalStateException("Cannot open SSH connection", e);
	}
}
 
Example 13
Project: metadata-qa-marc   File: CodeFileReader.java   View Source Code Vote up 4 votes
private static LineIterator getLineIterator(String fileName) throws IOException {
	ClassLoader classLoader = CodeFileReader.class.getClassLoader();
	return IOUtils.lineIterator(classLoader.getResourceAsStream(fileName), Charset.defaultCharset());
}