final String stringURL=IOUtils.getStringFromStream(inStream); final URL fileURL=new URL(stringURL); final File inputFile= IOUtils.URLToFile(fileURL); if(inputFile!=null && inputFile.exists() && inputFile.canRead()) { IOUtils.copyFile(inputFile, newFile); } else { final InputStream inputStream = fileURL.openStream(); final OutputStream outStream = new FileOutputStream(newFile); IOUtils.copyStream(inputStream, outStream, true, true);
/** * Inflate the provided {@link ZipFile} in the provided output directory. * * @param archive the {@link ZipFile} to inflate. * @param outputDirectory the directory where to inflate the archive. * @param fileName name of the file if present. * @throws IOException in case something bad happens. */ public static void inflate(ZipFile archive, Resource outputDirectory, String fileName) throws IOException { inflate(archive, outputDirectory, fileName, null, null, null, false, false); }
/** * Reads content from the body of a request and writes it to a file. * * @param fileName The name of the file to write out. * @param directory The directory to write the file to. * @param deleteDirectoryContent Delete directory content if the file already exists. * @param request The request. * * @return The file object representing the newly written file. * * @throws IOException Any I/O errors that occur. * * TODO: move this to IOUtils. */ public static File handleBinUpload(String fileName, File directory, boolean deleteDirectoryContent, Request request) throws IOException { final File newFile = new File(directory, fileName); if (deleteDirectoryContent && newFile.exists()) { FileUtils.cleanDirectory(directory); } final ReadableByteChannel source =request.getEntity().getChannel(); final FileChannel outputChannel = IOUtils.getOuputChannel(newFile); IOUtils.copyChannel(1024*1024, source,outputChannel ); IOUtils.closeQuietly(source); IOUtils.closeQuietly(outputChannel); return newFile; }
/** * Convert the input from the provided {@link InputStream} into a {@link String}. * * @param inputStream the {@link InputStream} to copy from. * @return a {@link String} that contains the content of the provided {@link InputStream}. * @throws IOException in case something bad happens. */ public static String getStringFromStream(InputStream inputStream) throws IOException { inputNotNull(inputStream); final Reader inReq = new InputStreamReader(inputStream); return getStringFromReader(inReq); }
/** * Convert the input from the provided {@link Reader} into a {@link String}. * * @param src the {@link StreamSource} to copy from. * @return a {@link String} that contains the content of the provided {@link Reader}. * @throws IOException in case something bad happens. */ public static String getStringFromStreamSource(StreamSource src) throws IOException { inputNotNull(src); InputStream inputStream = src.getInputStream(); if (inputStream != null) { return getStringFromStream(inputStream); } else { final Reader r = src.getReader(); return getStringFromReader(r); } }
/** * Unzips a zip a file to a specified directory, deleting the zip file after unpacking. * * @param zipFile The zip file. * @param outputDirectory The directory to unpack the contents to. * * @throws IOException Any I/O errors that occur. * * TODO: move this to IOUtils */ public static void unzipFile( File zipFile, File outputDirectory ) throws IOException { if ( outputDirectory == null ) { outputDirectory = zipFile.getParentFile(); } if ( outputDirectory != null && !outputDirectory.exists() ) { outputDirectory.mkdir(); } ZipFile archive = new ZipFile(zipFile); IOUtils.inflate(archive, outputDirectory, null); IOUtils.deleteFile(zipFile); }
@Test public void testHarvestNotAllowedOnSimpleCoverageStore() throws Exception { // add bluemarble getTestData().addDefaultRasterLayer(SystemTestData.TASMANIA_BM, getCatalog()); // Harvesting of the Mosaic URL zipHarvest = MockData.class.getResource("harvesting.zip"); // Extract a Byte array from the zip file InputStream is = null; byte[] bytes; try { is = zipHarvest.openStream(); bytes = IOUtils.toByteArray(is); } finally { IOUtils.closeQuietly(is); } // Create the POST request MockHttpServletRequest request = createRequest( RestBaseController.ROOT_PATH + "/workspaces/wcs/coveragestores/BlueMarble"); request.setMethod("POST"); request.setContentType("application/zip"); request.setContent(bytes); request.addHeader("Content-type", "application/zip"); // Get The response MockHttpServletResponse response = dispatch(request); // not allowed assertEquals(405, response.getStatus()); }
/** * Copy {@link InputStream} to {@link OutputStream}. * * @param sourceStream {@link InputStream} to copy from. * @param destinationStream {@link OutputStream} to copy to. * @param closeInput quietly close {@link InputStream}. * @param closeOutput quietly close {@link OutputStream} * @throws IOException in case something bad happens. */ public static void copyStream( InputStream sourceStream, OutputStream destinationStream, boolean closeInput, boolean closeOutput) throws IOException { copyStream(sourceStream, destinationStream, DEFAULT_SIZE, closeInput, closeOutput); }
/** * Simple class implementing a periodic Thread that periodically tries to delete the files that * were provided to him. * * <p>It tries to delete each file at most {@link FileCleaner#maxAttempts} number of times. If * this number is exceeded it simply throws the file away notifying the users with a warning * message. * * @author Simone Giannecchini, GeoSolutions. */ public static final class FileCleaner extends Thread { /** * Maximum number of attempts to delete a given {@link File}. * * <p>If the provided number of attempts is exceeded we simply drop warn the user and we * remove the {@link File} from our list. */ private int maxAttempts = DEF_MAX_ATTEMPTS; /** Period in seconds between two checks. */ private volatile long period = DEFAULT_PERIOD; /** * Asks this {@link FileCleaner} to clean up this file. * * @param fileToDelete {@link File} that we want to permanently delete. */ public void addFile(final File fileToDelete) { // does it exists
try { is = zip.openStream(); bytes = IOUtils.toByteArray(is); } finally { IOUtils.closeQuietly(is); Resource newZip = Files.asResource(new File("./target/harvesting2.zip")); IOUtils.copyStream(zipHarvest.openStream(), newZip.out(), true, true); Resource outputDirectory = Files.asResource(new File("./target/harvesting")); RESTUtils.unzipFile(newZip, outputDirectory);
final String stringURL = IOUtils.getStringFromStream(inStream); final URL fileURL = new URL(stringURL); IOUtils.copyStream(inputStream, outStream, true, true);
/** * Handles an upload using the EXTERNAL method. * * @param request * @throws IOException */ public static File handleEXTERNALUpload(Request request) throws IOException { //get the URL for this file to upload final InputStream inStream=request.getEntity().getStream(); final String stringURL=IOUtils.getStringFromStream(inStream); final URL fileURL=new URL(stringURL); final File inputFile= IOUtils.URLToFile(fileURL); if(inputFile!=null && inputFile.exists() && inputFile.canRead()) { return inputFile; } return null; }
FileInputStream fileInputStream = new FileInputStream(file); IOUtils.copy(fileInputStream, zipOutputStream); IOUtils.closeQuietly(zipOutputStream); IOUtils.closeQuietly(bufferedOutputStream); IOUtils.closeQuietly(byteArrayOutputStream);
/** * Delete all the files with matching the specified {@link FilenameFilter} in the specified * directory. The method can work recursively. * * @param sourceDirectory the directory to delete files from. * @param filter the {@link FilenameFilter} to use for selecting files to delete. * @param recursive boolean that specifies if we want to delete files recursively or not. */ public static boolean deleteDirectory( File sourceDirectory, FilenameFilter filter, boolean recursive, boolean deleteItself) { inputNotNull(sourceDirectory, filter); if (!sourceDirectory.exists() || !sourceDirectory.canRead() || !sourceDirectory.isDirectory()) throw new IllegalStateException("Source is not in a legal state."); final File[] files = (filter != null ? sourceDirectory.listFiles(filter) : sourceDirectory.listFiles()); for (File file : files) { if (file.isDirectory()) { if (recursive) { deleteDirectory(file, filter, recursive, deleteItself); } } else { if (!file.delete()) return false; } } return !deleteItself || sourceDirectory.delete(); }
inputNotNull(source, destDir); if (!source.exists() || !source.canRead() || source.isDirectory()) throw new IllegalStateException("Source is not in a legal state."); copyFile(source, new File(destDir, source.getName()));
inputNotNull(sourceFile, destinationFile); if (!sourceFile.exists() || !sourceFile.canRead() || !sourceFile.isFile()) throw new IllegalStateException("Source is not in a legal state."); destination = new RandomAccessFile(destinationFile, "rw").getChannel(); try { copyFileChannel(size, source, destination); } finally { try {
@Override protected String readInternal(Class<? extends String> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { return IOUtils.getStringFromStream(inputMessage.getBody()); }
/** * Writes the content of an input stream to a style resource, without validation * * @param info Style info object, containing details about the style format and location * @param input The style contents * @throws IOException if there was an error persisting the style */ private void writeStyleRaw(StyleInfo info, InputStream input) throws IOException { try { catalog.getResourcePool().writeStyle(info, input); } finally { IOUtils.closeQuietly(input); } }
/** * Copy the input file onto the output file using a default buffer size. * * @param sourceFile the {@link File} to copy from. * @param destinationFile the {@link File} to copy to. * @throws IOException in case something bad happens. */ public static void copyFile(File sourceFile, File destinationFile) throws IOException { copyFile(sourceFile, destinationFile, DEFAULT_SIZE); }
IOUtils.copy(request.getInputStream(), os);