Java Code Examples for org.openide.filesystems.FileUtil#copy()

The following examples show how to use org.openide.filesystems.FileUtil#copy() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestUtils.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public static void unZipFile(InputStream source, FileObject rootFolder) throws IOException {
    try {
        ZipInputStream str = new ZipInputStream(source);
        ZipEntry entry;
        while ((entry = str.getNextEntry()) != null) {
            if (entry.isDirectory()) {
                FileUtil.createFolder(rootFolder, entry.getName());
                continue;
            }
            FileObject fo = FileUtil.createData(rootFolder, entry.getName());
            FileLock lock = fo.lock();
            try {
                OutputStream out = fo.getOutputStream(lock);
                try {
                    FileUtil.copy(str, out);
                } finally {
                    out.close();
                }
            } finally {
                lock.releaseLock();
            }
        }
    } finally {
        source.close();
    }
}
 
Example 2
Source File: LibraryProvider.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Downloads the specified file of the given library version. The data are saved
 * into a temporary file that is returned.
 * 
 * @param version library version whose file should be downloaded
 * (only libraries/versions returned by this provider can be downloaded).
 * @param fileIndex 0-based index of the file (in the version's list of files).
 * @return downloaded (temporary) file.
 * @throws IOException when the downloading of the file failed.
 */
public File downloadLibraryFile(Library.Version version, int fileIndex) throws IOException {
    String libraryName = version.getLibrary().getName();
    String versionName = version.getName();
    String[] fileNames = version.getFiles();
    String fileName = fileNames[fileIndex];
    String url = MessageFormat.format(LIBRARY_FILE_URL_PATTERN, libraryName, versionName, fileName);
    URL urlObject = new URL(url);
    URLConnection urlConnection = urlObject.openConnection();
    try (InputStream input = urlConnection.getInputStream()) {
        int index = fileName.lastIndexOf('.');
        String prefix = (index == -1) ? fileName : fileName.substring(0,index);
        if (prefix.length() < 3) {
            prefix = "tmp" + prefix; // NOI18N
        }
        String suffix = (index == -1) ? "" : fileName.substring(index);
        File file = File.createTempFile(prefix, suffix);
        try (OutputStream output = new FileOutputStream(file)) {
            FileUtil.copy(input, output);
            return file;
        }
    }
}
 
Example 3
Source File: ProvidedExtensionsTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public ProvidedExtensions.IOHandler getCopyHandler(final File from, final File to) {
    to.getClass();
    if (from.isDirectory()) {
        return null;
    }
    implsCopyCalls++;
    return (!isImplsCopyRetVal() || to == null) ? null : new ProvidedExtensions.IOHandler(){
        @Override
        public void handle() throws IOException {
            copyImplCalls++;
            if (to.exists()) {
                throw new IOException();
            }
            assertTrue(from.exists());
            assertFalse(to.exists());
            
            assertFalse(from.equals(to));
            InputStream inputStream = new FileInputStream(from);
            OutputStream outputStream = new FileOutputStream(to);
            try {
                FileUtil.copy(inputStream, outputStream);
            } finally {
                if (inputStream != null) inputStream.close();
                if (outputStream != null) outputStream.close();
            }
            assertTrue(from.exists());
            assertTrue(to.exists());
        }
    };
}
 
Example 4
Source File: TestBase.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static final FileObject copyStringToFileObject(FileObject fo, String content) throws IOException {
    OutputStream os = fo.getOutputStream();
    try {
        InputStream is = new ByteArrayInputStream(content.getBytes("UTF-8"));
        try {
            FileUtil.copy(is, os);
            return fo;
        } finally {
            is.close();
        }
    } finally {
        os.close();
    }
}
 
Example 5
Source File: TestUtilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static final FileObject copyStringToFileObject(FileObject fo, String content) 
    throws IOException 
 {
    OutputStream os = fo.getOutputStream();
    try {
        InputStream is = new ByteArrayInputStream(content.getBytes("UTF-8"));
        FileUtil.copy(is, os);
        return fo;
    } finally {
        os.close();
    }
}
 
Example 6
Source File: PatchUtilsTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private File createFile(File file) throws IOException {
    final FileInputStream fileInputStream = new FileInputStream(new File(dataDir, "goldenFileBefore"));
    final FileOutputStream fileOutputStream = new FileOutputStream(file);
    try {
        FileUtil.copy(fileInputStream, fileOutputStream);
    } finally {
        fileInputStream.close();
        fileOutputStream.close();
    }
    return file;
}
 
Example 7
Source File: WLJpa2SwitchSupport.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void copy(File source, File dest) throws IOException {
    InputStream is = new BufferedInputStream(new FileInputStream(source));
    try {
        OutputStream os = new BufferedOutputStream(new FileOutputStream(dest));
        try {
            FileUtil.copy(is, os);
        } finally {
            os.close();
        }
    } finally {
        is.close();
    }
}
 
Example 8
Source File: apptypeWizardIterator.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
private static void filterProjectXML(FileObject fo, ZipInputStream str, String name) throws IOException {
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        FileUtil.copy(str, baos);
        Document doc = XMLUtil.parse(new InputSource(new ByteArrayInputStream(baos.toByteArray())), false, false, null, null);
        NodeList nl = doc.getDocumentElement().getElementsByTagName("name");
        if (nl != null) {
            for (int i = 0; i < nl.getLength(); i++) {
                Element el = (Element) nl.item(i);
                if (el.getParentNode() != null && "data".equals(el.getParentNode().getNodeName())) {
                    NodeList nl2 = el.getChildNodes();
                    if (nl2.getLength() > 0) {
                        nl2.item(0).setNodeValue(name);
                    }
                    break;
                }
            }
        }
        OutputStream out = fo.getOutputStream();
        try {
            XMLUtil.write(doc, out, "UTF-8");
        } finally {
            out.close();
        }
    } catch (Exception ex) {
        Exceptions.printStackTrace(ex);
        writeFile(str, fo);
    }

}
 
Example 9
Source File: pluggableViewDemoSuiteWizardIterator.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
private static void writeFile(ZipInputStream str, FileObject fo) throws IOException {
    OutputStream out = fo.getOutputStream();
    try {
        FileUtil.copy(str, out);
    } finally {
        out.close();
    }
}
 
Example 10
Source File: DefaultTestCase.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void populateCatalog(InputStream is) throws FileNotFoundException, IOException {
    OutputStream os = new FileOutputStream(catalogFile);
    try {
        FileUtil.copy(is, os);
    } finally {
        is.close();
        os.close();
    }
}
 
Example 11
Source File: PHPSamplesWizardIterator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void filterProjectXML(FileObject fo, ZipInputStream str, String name) throws IOException {
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        FileUtil.copy(str, baos);
        Document doc = XMLUtil.parse(new InputSource(new ByteArrayInputStream(baos.toByteArray())), false, false, null, null);
        NodeList nl = doc.getDocumentElement().getElementsByTagName("name");
        if (nl != null) {
            for (int i = 0; i < nl.getLength(); i++) {
                Element el = (Element) nl.item(i);
                if (el.getParentNode() != null && "data".equals(el.getParentNode().getNodeName())) {
                    NodeList nl2 = el.getChildNodes();
                    if (nl2.getLength() > 0) {
                        nl2.item(0).setNodeValue(name);
                    }
                    break;
                }
            }
        }
        OutputStream out = fo.getOutputStream();
        try {
            XMLUtil.write(doc, out, "UTF-8");
        } finally {
            out.close();
        }
    } catch (Exception ex) {
        Exceptions.printStackTrace(ex);
        writeFile(str, fo);
    }

}
 
Example 12
Source File: AntDeploymentProviderImpl.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void writeDeploymentScript(OutputStream os, Object moduleType) throws IOException {
    InputStream is = AntDeploymentProviderImpl.class.getResourceAsStream("ant-deploy.xml"); // NOI18N            
    try {
        FileUtil.copy(is, os);
    } finally {
        is.close();
    }
}
 
Example 13
Source File: TestUtil.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static File createRT_JAR(File destDir) throws IOException {
    File src = new File( TestUtil.getJdkDir(), TestUtil.RT_JAR );

    if ( src.canRead() ) {
        TestUtil.copyFiles( TestUtil.getJdkDir(), destDir, TestUtil.RT_JAR );
        return new File( destDir, TestUtil.RT_JAR );
    }

    File rtJar = new File(destDir, "rt.jar");
    Set<String> seenPackages = new HashSet<>();

    try (OutputStream binOut = new FileOutputStream(rtJar);
         JarOutputStream out = new JarOutputStream(binOut)) {
        for (FileObject root : BootClassPathUtil.getBootClassPath().getRoots()) {
            Enumeration<? extends FileObject> en = root.getChildren(true);

            while (en.hasMoreElements()) {
                FileObject f = en.nextElement();

                if (f.isFolder()) {
                    String name = FileUtil.getRelativePath(root, f) + "/";
                    if (seenPackages.add(name)) {
                        out.putNextEntry(new ZipEntry(name));
                    }
                } else {
                    if (!f.getNameExt().equals("module-info.class")) {
                        out.putNextEntry(new ZipEntry(FileUtil.getRelativePath(root, f)));
                        try (InputStream in = f.getInputStream()) {
                            FileUtil.copy(in, out);
                        }
                    }
                }
            }
        }
    }

    return rtJar;
}
 
Example 14
Source File: InstallJasmineWizardDescriptorPanel.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static File copyToFile(InputStream is, File target) throws IOException {
    OutputStream os = new FileOutputStream(target);
    try {
        FileUtil.copy(is, os);
    } finally {
        os.close();
    }
    return target;
}
 
Example 15
Source File: subnodesWizardIterator.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
private static void filterProjectXML(FileObject fo, ZipInputStream str, String name) throws IOException {
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        FileUtil.copy(str, baos);
        Document doc = XMLUtil.parse(new InputSource(new ByteArrayInputStream(baos.toByteArray())), false, false, null, null);
        NodeList nl = doc.getDocumentElement().getElementsByTagName("name");
        if (nl != null) {
            for (int i = 0; i < nl.getLength(); i++) {
                Element el = (Element) nl.item(i);
                if (el.getParentNode() != null && "data".equals(el.getParentNode().getNodeName())) {
                    NodeList nl2 = el.getChildNodes();
                    if (nl2.getLength() > 0) {
                        nl2.item(0).setNodeValue(name);
                    }
                    break;
                }
            }
        }
        OutputStream out = fo.getOutputStream();
        try {
            XMLUtil.write(doc, out, "UTF-8");
        } finally {
            out.close();
        }
    } catch (Exception ex) {
        Exceptions.printStackTrace(ex);
        writeFile(str, fo);
    }

}
 
Example 16
Source File: CreateSiteTemplate.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void writeChildren(ZipOutputStream str, FileObject projectDirectory, FileObject siteRoot, Children children) throws IOException {
    for (Node node : children.getNodes(true)) {
        FileObject fo = node.getLookup().lookup(FileObject.class);
        InputStream is = null;
        if (!fo.isFolder()) {
            is = fo.getInputStream();
        }
        try {
            Checkable ch = node.getLookup().lookup(Checkable.class);
            if (!Boolean.TRUE.equals(ch.isSelected())) {
                continue;
            }
            String relPath = getRelativePath(projectDirectory, siteRoot, fo);
            if (fo.isFolder()) {
                relPath += "/"; //NOI18N
            }
            ZipEntry ze = new ZipEntry(relPath);
            str.putNextEntry(ze);
            if (is != null) {
                FileUtil.copy(fo.getInputStream(), str);
            }
        } finally {
            if (is != null) {
                is.close();
            }
        }
        if (!node.isLeaf()) {
            writeChildren(str, projectDirectory, siteRoot, node.getChildren());
        }
    }
}
 
Example 17
Source File: AntDeploymentProviderImpl.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void writeDeploymentScript(OutputStream os, Object moduleType) throws IOException {
    String name = null;
    switch (tm.getTomcatVersion()) {
        case TOMCAT_70:
            name = "resources/tomcat-ant-deploy70.xml";
            break;
        case TOMCAT_60:
            name = "resources/tomcat-ant-deploy60.xml";
            break;
        case TOMCAT_55:
        case TOMCAT_50:
        default:
            name = "resources/tomcat-ant-deploy.xml";
    }
    
    InputStream is = AntDeploymentProviderImpl.class.getResourceAsStream(name); // NOI18N
    if (is == null) {
        // this should never happen, but better make sure
        LOGGER.log(Level.SEVERE, "Missing resource {0}.", name); // NOI18N
        return;
    }
    try {
        FileUtil.copy(is, os);
    } finally {
        is.close();
    }
}
 
Example 18
Source File: SyncController.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private boolean copyContent(TmpLocalFile source, File target) throws IOException {
    if (source == null) {
        // no tmp files
        return true;
    }
    FileObject fileObject = FileUtil.toFileObject(target);
    if (fileObject == null || !fileObject.isValid()) {
        return false;
    }
    try (InputStream inputStream = source.getInputStream(); OutputStream outputStream = fileObject.getOutputStream()) {
        FileUtil.copy(inputStream, outputStream);
    }
    return true;
}
 
Example 19
Source File: BasicProjectWizardIterator.java    From nb-springboot with Apache License 2.0 4 votes vote down vote up
private static void writeFile(ZipInputStream zis, FileObject fo) throws IOException {
    try (OutputStream out = fo.getOutputStream()) {
        FileUtil.copy(zis, out);
    }
}
 
Example 20
Source File: LicenseHeaderPanelProvider.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public Impl(final ModelHandle2 handle, Project prj, AuxiliaryProperties props) {
    this.handle = handle;
    this.props = props;
    this.project = prj;
    operation = new ModelOperation<POMModel>() {
        @Override
        public void performOperation(POMModel model) {
            if (licenseContent == null) {
                return;
            }
            try {
                ExpressionEvaluator createEvaluator = PluginPropertyUtils.createEvaluator(project);
                Object evaluate = createEvaluator.evaluate(licensePath);
                if(evaluate != null) {
                    String eval = evaluate.toString();

                    File file = FileUtilities.resolveFilePath(handle.getProject().getBasedir(), eval);
                    FileObject fo;
                    if (!file.exists()) {
                        fo = FileUtil.createData(file);
                    } else {
                        fo = FileUtil.toFileObject(file);
                    }
                    if (fo.isData()) {
                        OutputStream out = fo.getOutputStream();
                        try {
                            FileUtil.copy(new ByteArrayInputStream(licenseContent.getBytes()), out);
                        } finally {
                            out.close();
                        }
                    }
                } else {
                    Logger.getLogger(LicenseHeaderPanelProvider.class.getName()).log(Level.WARNING, "Encountered problems evaluating license path: {0}", licensePath);
                }
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            } catch (ExpressionEvaluationException ex) {
                Exceptions.printStackTrace(ex);
            }
        }
    };  
    licensePath = getProjectLicenseLocation();
}