List of usage examples for org.apache.commons.io FileUtils copyURLToFile
public static void copyURLToFile(URL source, File destination) throws IOException
source
to a file destination
. From source file:org.kalypso.commons.eclipse.core.runtime.PluginImageProvider.java
private URL getTmpUrl(final String path, final URL url) { if (url == null) return null; try {//from ww w.jav a 2 s . c o m final File imageDir = m_imageTmpDir; final File classDir = new File(imageDir, "resources"); //$NON-NLS-1$ final File imageFile = new File(classDir, path); if (imageFile.exists()) return imageFile.toURI().toURL(); FileUtils.copyURLToFile(url, imageFile); return imageFile.toURI().toURL(); } catch (final IOException e) { PluginUtilities.logToPlugin(m_plugin, IStatus.ERROR, "Could not create temporary file for " + url.toString(), e); //$NON-NLS-1$ return null; } }
From source file:org.kalypso.grid.AsciiRandomAccessGeoGrid.java
public AsciiRandomAccessGeoGrid(final URL asciiFileURL, final Coordinate origin, final Coordinate offsetX, final Coordinate offsetY, final String sourceCRS) throws IOException { super(origin, offsetX, offsetY, sourceCRS); /* Copy file to local folder, as we are going to open it as random access file */ m_ascTmpFile = File.createTempFile("ascTmp", ".asc"); m_ascTmpFile.deleteOnExit();/*from ww w . j av a 2 s . c o m*/ // TODO: maybe don't do it, if it is already a file FileUtils.copyURLToFile(asciiFileURL, m_ascTmpFile); /* open tmp-file as random-access */ m_randomAccessFile = new RandomAccessFile(m_ascTmpFile, "r"); /* reading header data */ final String[] data = new String[6]; for (int i = 0; i < 6; i++) { final String line = m_randomAccessFile.readLine(); final int index = line.indexOf(" "); //$NON-NLS-1$ final String subString = line.substring(index); data[i] = subString.trim(); } m_sizeX = Integer.parseInt(data[0]); m_sizeY = Integer.parseInt(data[1]); m_noDataValue = Double.parseDouble(data[5]); m_rowData = new double[m_sizeX]; m_rowPositions = new long[m_sizeY]; // read row-positions for (int i = 0; i < m_sizeY; i++) { m_rowPositions[i] = m_randomAccessFile.getFilePointer(); if (!seekLineEnd()) break; } }
From source file:org.kalypso.grid.BinaryGeoGrid.java
/** * Opens an existing grid for read-only access.<br> * Dispose the grid after it is no more needed in order to release the given resource. * /*from w ww .j a v a 2s .c o m*/ * @param writeable * If <code>true</code>, the grid is opened for writing and a {@link IWriteableGeoGrid} is returned. */ public static BinaryGeoGrid openGrid(final URL url, final Coordinate origin, final Coordinate offsetX, final Coordinate offsetY, final String sourceCRS, final boolean writeable) throws IOException { /* Tries to find a file from the given url. */ File fileFromUrl = ResourceUtilities.findJavaFileFromURL(url); File binFile = null; if (fileFromUrl == null) fileFromUrl = FileUtils.toFile(url); if (fileFromUrl == null) { /* * If url cannot be converted to a file, write its contents to a temporary file which will be deleted after the * grid gets disposed. */ fileFromUrl = File.createTempFile("local", ".bin"); fileFromUrl.deleteOnExit(); FileUtils.copyURLToFile(url, fileFromUrl); binFile = fileFromUrl; // set in order to delete on dispose } FileChannel channel; if (writeable) channel = FileChannel.open(fileFromUrl.toPath(), StandardOpenOption.WRITE, StandardOpenOption.READ); else channel = FileChannel.open(fileFromUrl.toPath(), StandardOpenOption.READ); return new BinaryGeoGrid(channel, binFile, origin, offsetX, offsetY, sourceCRS); }
From source file:org.kalypso.kalypsosimulationmodel.core.wind.BinaryGeoGridWrapperForPairsModel.java
/** * Opens an existing grid.<br>//from w ww. ja v a 2 s.c om * Dispose the grid after it is no more needed in order to release the given resource. * * @param writeable * If <code>true</code>, the grid is opened for writing and a {@link IWriteableGeoGrid} is returned. */ public static BinaryGeoGridWrapperForPairsModel openGrid(final URL url, final Coordinate origin, final Coordinate offsetX, final Coordinate offsetY, final String sourceCRS, final boolean writeable) throws IOException { /* Tries to find a file from the given url. */ File fileFromUrl = ResourceUtilities.findJavaFileFromURL(url); File binFile = null; if (fileFromUrl == null) fileFromUrl = FileUtils.toFile(url); if (fileFromUrl == null) { /* * If url cannot be converted to a file, write its contents to a temporary file which will be deleted after the * grid gets disposed. */ fileFromUrl = File.createTempFile("local", ".bin"); //$NON-NLS-1$ //$NON-NLS-2$ fileFromUrl.deleteOnExit(); FileUtils.copyURLToFile(url, fileFromUrl); binFile = fileFromUrl; // set in order to delete on dispose } FileChannel channel; if (writeable) channel = FileChannel.open(fileFromUrl.toPath(), StandardOpenOption.WRITE, StandardOpenOption.READ); else channel = FileChannel.open(fileFromUrl.toPath(), StandardOpenOption.READ); return new BinaryGeoGridWrapperForPairsModel(channel, binFile, origin, offsetX, offsetY, sourceCRS); }
From source file:org.kalypso.model.wspm.pdb.ui.internal.wspm.InitPerspectiveJob.java
IFile ensureMapFile() { final IFile mapFile = m_project.getMapFile(); if (mapFile.exists()) return mapFile; try {/* w w w .j a va2s .c o m*/ final File mapJavaFile = mapFile.getLocation().toFile(); final URL mapData = getClass().getResource("mapTemplate.gmt"); //$NON-NLS-1$ FileUtils.copyURLToFile(mapData, mapJavaFile); mapFile.refreshLocal(IResource.DEPTH_ONE, new NullProgressMonitor()); } catch (final IOException e) { e.printStackTrace(); } catch (final CoreException e) { e.printStackTrace(); } return mapFile; }
From source file:org.kalypso.model.wspm.tuhh.schema.simulation.MultipleRunoffReader.java
public void createResult(final ISimulationResultEater resultEater) throws SimulationException, IOException, GmlSerializeException { /* Sort by station before wrinting the result */ final Comparator<Object> featureComparator = new FeatureComparator(m_resultCollection, QIntervallResult.QNAME_P_QIntervallResult_station); Collections.sort(m_resultCollection.getQIntervalls(), featureComparator); final GMLWorkspace workspace = m_resultCollection.getWorkspace(); GmlSerializer.serializeWorkspace(m_targetGmlFile, workspace, CharEncoding.UTF_8); resultEater.addResult(WspmTuhhCalcJob.OUTPUT_QINTERVALL_RESULT, m_targetGmlFile); final File gmvResultFile = new File(m_targetGmlFile.getParentFile(), ERGEBNISSE_GMV); final URL ergebnisseGmvLocation = PolynomeProcessor.class.getResource("resources/" + ERGEBNISSE_GMV); //$NON-NLS-1$ FileUtils.copyURLToFile(ergebnisseGmvLocation, gmvResultFile); resultEater.addResult(WspmTuhhCalcJob.OUTPUT_QINTERVALL_RESULT_GMV, gmvResultFile); }
From source file:org.kalypso.ogc.gml.loader.ShapeLoader.java
@Override protected CommandableWorkspace loadIntern(final IPoolableObjectType key, final IProgressMonitor monitor) throws LoaderException { final String location = key.getLocation(); final URL context = key.getContext(); /* Files that get deleted at the end of this operation. */ final List<File> filesToDelete = new ArrayList<>(); try {//w w w . ja v a 2 s. c om final String sourceSrs = parseSrs(location); final String shpSource = parseSource(location); final String taskMsg = Messages.getString("org.kalypso.ogc.gml.loader.GmlLoader.1", shpSource); //$NON-NLS-1$ final SubMonitor moni = SubMonitor.convert(monitor, taskMsg, 100); ProgressUtilities.worked(moni, 1); final URL sourceURL = UrlResolverSingleton.resolveUrl(context, shpSource); final URL shpURL = new URL(sourceURL.toExternalForm() + ".shp");//$NON-NLS-1$ final URL dbfURL = new URL(sourceURL.toExternalForm() + ".dbf"); //$NON-NLS-1$ final URL shxURL = new URL(sourceURL.toExternalForm() + ".shx"); //$NON-NLS-1$ final URL prjURL = new URL(sourceURL.toExternalForm() + ".prj"); //$NON-NLS-1$ // leider knnen Shapes nicht aus URL geladen werden -> protocoll checken final File sourceFile; final IPath resource = ResourceUtilities.findPathFromURL(sourceURL); if (resource != null) sourceFile = ResourceUtilities.makeFileFromPath(resource); else if (sourceURL.getProtocol().startsWith("file")) //$NON-NLS-1$ sourceFile = new File(sourceURL.getPath()); else { moni.subTask(Messages.getString("org.kalypso.ogc.gml.loader.ShapeLoader.0")); //$NON-NLS-1$ /* If everything else fails, we copy the resources to local files */ sourceFile = File.createTempFile("shapeLocalizedFiled", ""); //$NON-NLS-1$ //$NON-NLS-2$ final String sourceFilePath = sourceFile.getAbsolutePath(); final File shpFile = new File(sourceFilePath + ".shp"); //$NON-NLS-1$ final File dbfFile = new File(sourceFilePath + ".dbf"); //$NON-NLS-1$ final File shxFile = new File(sourceFilePath + ".shx"); //$NON-NLS-1$ filesToDelete.add(sourceFile); filesToDelete.add(shpFile); filesToDelete.add(dbfFile); filesToDelete.add(shxFile); FileUtils.copyURLToFile(shpURL, shpFile); FileUtils.copyURLToFile(dbfURL, dbfFile); FileUtils.copyURLToFile(shxURL, shxFile); } ProgressUtilities.worked(moni, 9); if (sourceFile == null) throw new LoaderException( Messages.getString("org.kalypso.ogc.gml.loader.ShapeLoader.10") + shpSource); //$NON-NLS-1$ /* Loading Shape */ final String sourceCrs = ShapeSerializer.loadCrs(prjURL, sourceSrs); final String targetCRS = KalypsoDeegreePlugin.getDefault().getCoordinateSystem(); // FIXME: we also need to specify the shape charset final ShapeCollection shapeCollection = ShapeSerializer.deserialize(sourceFile.getAbsolutePath(), sourceCrs, moni.newChild(70, SubMonitor.SUPPRESS_BEGINTASK)); final GMLWorkspace gmlWorkspace = shapeCollection.getWorkspace(); final CommandableWorkspace workspace = new CommandableWorkspace(gmlWorkspace); try { /* Transforming to Kalypso CRS */ moni.subTask(Messages.getString("org.kalypso.ogc.gml.loader.GmlLoader.3")); //$NON-NLS-1$ ProgressUtilities.worked(moni, 1); // check cancel workspace.accept(new TransformVisitor(targetCRS), workspace.getRootFeature(), FeatureVisitor.DEPTH_INFINITE); ProgressUtilities.worked(moni, 18); // check cancel } catch (final OperationCanceledException e) { throw new CoreException(Status.CANCEL_STATUS); } catch (final Exception e1) { e1.printStackTrace(); } return workspace; } catch (final CoreException ce) { if (!ce.getStatus().matches(IStatus.CANCEL)) ce.printStackTrace(); throw new LoaderException(ce); } catch (final GmlSerializeException ge) { final Throwable cause = ge.getCause(); if (cause instanceof CoreException) { final IStatus status = ((CoreException) cause).getStatus(); if (status.matches(IStatus.CANCEL)) throw new LoaderException(status); } ge.printStackTrace(); throw new LoaderException(ge); } catch (final Exception e) { e.printStackTrace(); throw new LoaderException(e); } finally { for (final File file : filesToDelete) file.delete(); } }
From source file:org.kalypso.ogc.sensor.diagview.grafik.GrafikLauncher.java
/** * Creates a new temporary file given its pathName and an InputStream. The content from the InputStream is written * into the file. The file will be deleted after the VM shuts down * // w w w . j a va 2s . c o m * @param charMode * @param prefix * prefix of file name * @param suffix * suffix of file name * @param ins * the input stream, that is the source * @param useCache * if true tries to use an existing file with these prefix/suffix * @return the newly created file or null if an exception was thrown. * @throws IOException * problems reading from stream or writing to temp. file */ private static File makeFileFromURL(final String prefix, final String suffix, final URL input, final boolean useCache) throws IOException { if (useCache) { try { return fileExistsInDir(prefix, suffix, System.getProperty(FileUtilities.JAVA_IO_TMPDIR)); } catch (final FileNotFoundException ignored) { // ignored // ignored.printStackTrace(); } } final File tmp = File.createTempFile(prefix, suffix); tmp.deleteOnExit(); FileUtils.copyURLToFile(input, tmp); return tmp; }
From source file:org.kalypso.optimize.SceJob.java
private void prepareExe() throws SimulationException { try {/*www. j a va2s . c om*/ final URL sceExeLocation = getClass().getResource("resource/sce.exe"); FileUtils.copyURLToFile(sceExeLocation, m_sceExe); } catch (final IOException e) { e.printStackTrace(); throw new SimulationException("sce.exe konnte nicht entpackt werden", e); } }
From source file:org.kalypso.test.diff.DiffUtilitiesTest.java
public void testZipDiff() throws Exception { final File tmpDir = FileUtilities.createNewTempDir("diffUtils"); try {//from w ww. j ava 2 s .com final File zipFile1 = new File(tmpDir, "test1.zip"); final File zipFile2 = new File(tmpDir, "test2.zip"); final URL zipURL1 = getClass().getResource("resources/test1.zip"); FileUtils.copyURLToFile(zipURL1, zipFile1); final URL zipURL2 = getClass().getResource("resources/test2.zip"); FileUtils.copyURLToFile(zipURL2, zipFile2); final StringBuffer buffer = new StringBuffer(); final ILogger logger = new ILogger() { @Override public void log(final Level level, final int code, final String message) { System.out.println(message); buffer.append(message); buffer.append("\n"); } }; DiffUtils.diffZips(logger, zipFile1, zipFile2, new String[0]); final String actual = buffer.toString().replaceAll("\\s", ""); final URL resource = getClass().getResource("resources/difflog.txt"); final String expected = IOUtils.toString(resource).replaceAll("\\s", ""); assertEquals(actual, expected); } catch (final Exception e) { e.printStackTrace(); throw e; } }