List of usage examples for org.apache.commons.io IOUtils copyLarge
public static long copyLarge(Reader input, Writer output) throws IOException
Reader
to a Writer
. From source file:org.hawkular.wildfly.agent.installer.AgentInstaller.java
/** * Downloads the Hawkular WildFly Agent ZIP file from a URL * * @param url where the agent zip is//from ww w. ja v a 2 s . c om * @return absolute path to module downloaded locally or null if it could not be retrieved; * this is a temporary file that should be cleaned once it is used */ private static File downloadModuleZip(URL url) { File tempFile; try { tempFile = File.createTempFile("hawkular-wildfly-agent", ".zip"); } catch (Exception e) { throw new RuntimeException("Cannot create temp file to hold module zip", e); } try (FileOutputStream fos = new FileOutputStream(tempFile); InputStream ios = url.openStream()) { IOUtils.copyLarge(ios, fos); return tempFile; } catch (Exception e) { log.warn("Unable to download hawkular wildfly agent ZIP: " + url, e); tempFile.delete(); } return null; }
From source file:org.hawkular.wildfly.agent.installer.InstallerConfigurationTest.java
@Test public void testLoadPropertiesFileFromFile() throws Exception { URL url = InstallerConfigurationTest.class.getResource("/test-installer.properties"); File tempFile = File.createTempFile("InstallerConfigurationTest", ".properties"); try (FileOutputStream fos = new FileOutputStream(tempFile); InputStream ios = url.openStream()) { IOUtils.copyLarge(ios, fos); // make a copy of the test-installer.properties in our filesystem }//from www. j a va2 s . c o m try { ProcessedCommand<?> options = InstallerConfiguration.buildCommandLineOptions(); CommandLineParser<?> parser = new CommandLineParserBuilder().processedCommand(options).create(); CommandLine<?> commandLine = parser.parse(args("--installer-config", tempFile.getAbsolutePath())); InstallerConfiguration installerConfig = new InstallerConfiguration(commandLine); assertTestProperties(installerConfig); } finally { tempFile.delete(); } }
From source file:org.jahia.services.importexport.ImportExportBaseService.java
public void getFileList(Resource file, Map<String, Long> sizes, List<String> fileList) throws IOException { ZipInputStream zis = getZipInputStream(file); try {//from w ww .j a va 2 s .co m while (true) { ZipEntry zipentry = zis.getNextEntry(); if (zipentry == null) break; String name = zipentry.getName().replace('\\', '/'); if (expandImportedFilesOnDisk) { final File file1 = new File(expandImportedFilesOnDiskPath + File.separator + name); if (zipentry.isDirectory()) { file1.mkdirs(); } else { long timer = System.currentTimeMillis(); if (logger.isDebugEnabled()) { logger.debug("Expanding {} into {}", zipentry.getName(), file1); } file1.getParentFile().mkdirs(); final OutputStream output = new BufferedOutputStream(new FileOutputStream(file1), 1024 * 64); try { IOUtils.copyLarge(zis, output); if (logger.isDebugEnabled()) { logger.debug("Expanded {} in {}", zipentry.getName(), DateUtils.formatDurationWords(System.currentTimeMillis() - timer)); } } finally { output.close(); } } } if (name.endsWith(".xml")) { BufferedReader br = new BufferedReader(new InputStreamReader(zis)); try { long i = 0; while (br.readLine() != null) { i++; } sizes.put(name, i); } finally { IOUtils.closeQuietly(br); } } else { sizes.put(name, zipentry.getSize()); } if (name.contains("/")) { fileList.add("/" + name); } zis.closeEntry(); } } finally { closeInputStream(zis); } }
From source file:org.jahia.utils.migration.Migrators.java
public List<String> migrate(InputStream inputStream, OutputStream outputStream, String filePath, Version fromVersion, Version toVersion, boolean performModification) { List<String> messages = new ArrayList<String>(); for (Migration migration : migrationsConfig.getMigrations()) { if (migration.getFromVersion().equals(fromVersion) && migration.getToVersion().equals(toVersion)) { for (MigrationResource migrationResource : migration.getMigrationResources()) { Matcher resourcePatternMatcher = migrationResource.getCompiledPattern().matcher(filePath); if (resourcePatternMatcher.matches()) { // we use byte array input and output stream to be able to iterate over the contents multiple times, feeding in the result of the last iteration into the next. ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); try { IOUtils.copyLarge(inputStream, byteArrayOutputStream); } catch (IOException e) { e.printStackTrace(); }//from w w w.j a v a 2 s . c o m byte[] inputByteArray = byteArrayOutputStream.toByteArray(); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(inputByteArray); byteArrayOutputStream.reset(); for (MigrationOperation migrationOperation : migrationResource.getOperations()) { messages.addAll(migrationOperation.execute(byteArrayInputStream, byteArrayOutputStream, filePath, performModification)); byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); byteArrayOutputStream.reset(); } try { IOUtils.copyLarge(byteArrayInputStream, outputStream); } catch (IOException e) { e.printStackTrace(); } } } } } return messages; }
From source file:org.jasig.portlet.calendar.adapter.ConfigurableHttpCalendarAdapter.java
/** * Uses Commons HttpClient to retrieve the specified url (optionally with the provided * {@link Credentials}.// w w w .j a v a2 s .co m * The response body is returned as an {@link InputStream}. * * @param url URL of the calendar to be retrieved * @param credentials {@link Credentials} to use with the request, if necessary (null is ok if credentials not required) * @return the body of the http response as a stream * @throws CalendarException wraps all potential {@link Exception} types */ protected InputStream retrieveCalendarHttp(String url, Credentials credentials) throws CalendarException { HttpClient client = new HttpClient(); if (null != credentials) { client.getState().setCredentials(AuthScope.ANY, credentials); } GetMethod get = null; try { if (log.isDebugEnabled()) { log.debug("Retrieving calendar " + url); } get = new GetMethod(url); int rc = client.executeMethod(get); if (rc == HttpStatus.SC_OK) { // return the response body log.debug("request completed successfully"); InputStream in = get.getResponseBodyAsStream(); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); IOUtils.copyLarge(in, buffer); return new ByteArrayInputStream(buffer.toByteArray()); } else { log.warn("HttpStatus for " + url + ":" + rc); throw new CalendarException( "non successful status code retrieving " + url + ", status code: " + rc); } } catch (HttpException e) { log.warn("Error fetching iCalendar feed", e); throw new CalendarException("Error fetching iCalendar feed", e); } catch (IOException e) { log.warn("Error fetching iCalendar feed", e); throw new CalendarException("Error fetching iCalendar feed", e); } finally { if (get != null) { get.releaseConnection(); } } }
From source file:org.jasig.portlet.calendar.processor.ICalendarContentProcessorTest.java
@Test public void test() throws IOException { Resource calendarFile = applicationContext.getResource("classpath:/sampleEvents.ics"); DateMidnight start = new DateMidnight(2010, 1, 1, DateTimeZone.UTC); Interval interval = new Interval(start, start.plusYears(3)); InputStream in = calendarFile.getInputStream(); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); IOUtils.copyLarge(in, buffer); TreeSet<VEvent> events = new TreeSet<VEvent>(new VEventStartComparator()); net.fortuna.ical4j.model.Calendar c = processor.getIntermediateCalendar(interval, new ByteArrayInputStream(buffer.toByteArray())); events.addAll(processor.getEvents(interval, c)); assertEquals(5, events.size());/*from ww w . j a va2 s .c o m*/ Iterator<VEvent> iterator = events.iterator(); VEvent event = iterator.next(); assertEquals("Independence Day", event.getSummary().getValue()); assertNull(event.getStartDate().getTimeZone()); event = iterator.next(); assertEquals("Vikings @ Saints [NBC]", event.getSummary().getValue()); DateTime eventStart = new DateTime(event.getStartDate().getDate(), DateTimeZone.UTC); assertEquals(0, eventStart.getHourOfDay()); assertEquals(30, eventStart.getMinuteOfHour()); event = iterator.next(); assertEquals("Independence Day", event.getSummary().getValue()); assertNull(event.getStartDate().getTimeZone()); }
From source file:org.jasig.portlet.proxy.mvc.portlet.proxy.ProxyPortletController.java
@ResourceMapping public void proxyResourceTarget(final @RequestParam("proxy.url") String url, final ResourceRequest request, final ResourceResponse response) { final PortletPreferences preferences = request.getPreferences(); // locate the content service to use to retrieve our HTML content final String contentServiceKey = preferences.getValue(CONTENT_SERVICE_KEY, null); final IContentService contentService = applicationContext.getBean(contentServiceKey, IContentService.class); // construct the proxy request final IContentRequest proxyRequest; try {/*from ww w . j ava2s . c om*/ proxyRequest = contentService.getRequest(request); } catch (RuntimeException e) { log.error("URL {} was not in the proxy list", url); response.setProperty(ResourceResponse.HTTP_STATUS_CODE, String.valueOf(HttpServletResponse.SC_UNAUTHORIZED)); return; } // retrieve the HTML content final IContentResponse proxyResponse = contentService.getContent(proxyRequest, request); OutputStream out = null; try { // TODO: find a cleaner way to handle this. we probably can't ever // have anything except an HTTP response if (proxyResponse instanceof HttpContentResponseImpl) { // replay any response headers from the proxied target HttpContentResponseImpl httpProxyResponse = (HttpContentResponseImpl) proxyResponse; for (Map.Entry<String, String> header : httpProxyResponse.getHeaders().entrySet()) { response.setProperty(header.getKey(), header.getValue()); } } // write out all proxied content out = response.getPortletOutputStream(); IOUtils.copyLarge(proxyResponse.getContent(), out); out.flush(); } catch (IOException e) { response.setProperty(ResourceResponse.HTTP_STATUS_CODE, String.valueOf(HttpServletResponse.SC_UNAUTHORIZED)); log.error("Exception writing proxied content", e); } finally { if (proxyResponse != null) { proxyResponse.close(); } IOUtils.closeQuietly(out); } }
From source file:org.jenkinsci.plugins.mber.FileDownloadCallable.java
@Override public JSONObject invoke(final File file, final VirtualChannel channel) { InputStream istream = null;/*w ww.j av a2s .co m*/ OutputStream ostream = null; try { this.fileName = file.getName(); // Avoid I/O errors by setting attributes on the connection before getting data from it. final String redirectedURL = followRedirects(this.url); final URLConnection connection = new URL(redirectedURL).openConnection(); connection.setUseCaches(false); // Track expected bytes vs. downloaded bytes so we can retry corrupt downloads. final long expectedByteCount = connection.getContentLength(); istream = connection.getInputStream(); ostream = new LoggingOutputStream(new FilePath(file).write(), this, expectedByteCount); final long downloadedByteCount = IOUtils.copyLarge(istream, ostream); if (downloadedByteCount < expectedByteCount) { final long missingByteCount = expectedByteCount - downloadedByteCount; return MberJSON.failed(String.format("Missing %d bytes in %s", missingByteCount, this.fileName)); } return MberJSON.success(); } catch (final LoggingInterruptedException e) { return MberJSON.aborted(e); } catch (final Exception e) { return MberJSON.failed(e); } finally { // Close the input and output streams so other build steps can access those files. IOUtils.closeQuietly(istream); IOUtils.closeQuietly(ostream); } }
From source file:org.jenkinsci.plugins.workflow.job.WorkflowRun.java
/** * Equivalent to calling {@link LargeText#writeLogTo(long, OutputStream)} without the unwanted override in {@link AnnotatedLargeText} that wraps in a {@link PlainTextConsoleOutputStream}. * TODO replace with {@code AnnotatedLargeText#writeRawLogTo} in 1.577 *//*from ww w . ja v a 2s . co m*/ private static long writeLogTo(AnnotatedLargeText<?> text, long position, OutputStream os) throws IOException { Charset c; try { Field f = LargeText.class.getDeclaredField("charset"); f.setAccessible(true); c = (Charset) f.get(text); } catch (Exception x) { LOGGER.log(Level.WARNING, null, x); return text.writeLogTo(position, os); // give up } Reader r = text.readAll(); try { InputStream is = new ReaderInputStream(r, c); is.skip(position); return position + IOUtils.copyLarge(is, os); } finally { r.close(); } }
From source file:org.kimios.client.controller.FileTransferController.java
public void downloadTemporaryFile(String sessionId, String temporaryFilePath, OutputStream out, long length) { try {//from w ww . j a va2s .c om FileInputStream in = new FileInputStream(new File(temporaryFilePath)); IOUtils.copyLarge(in, out); } catch (Exception e) { e.printStackTrace(); } }