private static void unZipFile(InputStream source, FileObject projectRoot) throws IOException { try { ZipInputStream str = new ZipInputStream(source); ZipEntry entry; while ((entry = str.getNextEntry()) != null) { if (entry.isDirectory()) { FileUtil.createFolder(projectRoot, entry.getName()); } else { FileObject fo = FileUtil.createData(projectRoot, 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(); } }
private void writeIntoFile(FileObject file, String what) throws Exception { FileLock lock = file.lock(); OutputStream out = file.getOutputStream(lock); try { out.write(what.getBytes()); } finally { out.close(); lock.releaseLock(); } }
/** * Create a new application manifest file with minimal initial contents. * * @param dir the directory to create it in * @param path the relative path of the file * @throws IOException in case of problems */ private static void createManifest(FileObject dir, String path) throws IOException { FileObject manifest = dir.createData(MANIFEST_FILE); FileLock lock = manifest.lock(); try { OutputStream os = manifest.getOutputStream(lock); try { PrintWriter pw = new PrintWriter(os); pw.println("Manifest-Version: 1.0"); // NOI18N pw.println("X-COMMENT: Main-Class will be added automatically by build"); // NOI18N pw.println(); // safest to end in \n\n due to JRE parsing bug pw.flush(); } finally { os.close(); } } finally { lock.releaseLock(); } }
/** * Stores the Ant deployment properties in the specified file. * * @param create if false the deployment properties file won't be created if it does not exist. * @throws IOException if a problem occurs. */ public void storeAntDeploymentProperties(File file, boolean create) throws IOException { if (!create && !file.exists()) { return; } EditableProperties antProps = new EditableProperties(false); antProps.setProperty("tomcat.home", homeDir.getAbsolutePath()); // NOI18N antProps.setProperty("tomcat.url", getWebUrl()); // NOI18N antProps.setProperty("tomcat.username", getUsername()); // NOI18N file.createNewFile(); FileObject fo = FileUtil.toFileObject(file); FileLock lock = fo.lock(); try { OutputStream os = fo.getOutputStream(lock); try { antProps.store(os); } finally { os.close(); } } finally { lock.releaseLock(); } }
private void init(FileObject file) throws IOException { lock = file.lock(); OutputStream os = new FileOutputStream(FileUtil.toFile(file), false); writer = new ObjectOutputStream(new BufferedOutputStream(os, BUFFER_SIZE)); }
public static void overwriteFile(Node srcNode, Node destNode) throws IOException { DataObject srcDO = srcNode.getLookup().lookup(DataObject.class); FileObject srcFO = srcDO.getPrimaryFile(); DataObject destDO = destNode.getLookup().lookup(DataObject.class); FileObject destFO = destDO.getPrimaryFile(); // To avoid any confusion, save modified source first. if (srcDO.isModified()) { NotifyDescriptor d = new NotifyDescriptor.Confirmation( NbBundle.getMessage( FileNodeUtil.class, "MSG_SaveModifiedSource", srcFO.getNameExt()), // NOI18N NbBundle.getMessage(FileNodeUtil.class, "TTL_SaveModifiedSource"), // NOI18N NotifyDescriptor.OK_CANCEL_OPTION); if (DialogDisplayer.getDefault().notify(d) == NotifyDescriptor.OK_OPTION) { EditorCookie srcEditorCookie = srcDO.getCookie(EditorCookie.class); srcEditorCookie.saveDocument(); } } // // Alternatively, we could use the in-memory copy of the source file // EditorCookie srcEditorCookie = // (EditorCookie) srcDO.getCookie(EditorCookie.class); // Document srcDocument = srcEditorCookie.getDocument(); // if (srcDocument == null) { // Task loadTask = srcEditorCookie.prepareDocument(); // new RequestProcessor().post(loadTask); // // loadTask.waitFinished(); // srcDocument = srcEditorCookie.getDocument(); // } // String srcText = srcDocument.getText(0, srcDocument.getLength()); // From now on, we will only be using the on-disk copy of the source file. if (destDO.isModified()) { NotifyDescriptor d = new NotifyDescriptor.Confirmation( NbBundle.getMessage( FileNodeUtil.class, "MSG_OverwriteModifiedDestination", destFO.getNameExt()), // NOI18N NbBundle.getMessage(FileNodeUtil.class, "TTL_OverwriteModifiedDestination"), // NOI18N NotifyDescriptor.OK_CANCEL_OPTION); if (DialogDisplayer.getDefault().notify(d) == NotifyDescriptor.CANCEL_OPTION) { return; } EditorCookie destEditorCookie = destDO.getCookie(EditorCookie.class); InputStream inputStream = null; try { inputStream = srcFO.getInputStream(); String srcText = getInputStreamContents(inputStream); Document outputDocument = destEditorCookie.getDocument(); try { outputDocument.remove(0, outputDocument.getLength()); } catch (java.lang.Exception e) { // Ignore exception here on purpose. // One of the listener from xml module throws NPE: // at // org.netbeans.modules.xml.text.completion.GrammarManager.isGuarded(GrammarManager.java:170) // at // org.netbeans.modules.xml.text.completion.GrammarManager.removeUpdate(GrammarManager.java:140) // at // org.netbeans.lib.editor.util.swing.PriorityDocumentListenerList.removeUpdate(PriorityDocumentListenerList.java:63) // at javax.swing.text.AbstractDocument.fireRemoveUpdate(AbstractDocument.java:242) // at org.netbeans.editor.BaseDocument.fireRemoveUpdate(BaseDocument.java:1305) // at org.netbeans.editor.BaseDocument.remove(BaseDocument.java:737) } outputDocument.insertString(0, srcText, null); destEditorCookie.saveDocument(); } catch (BadLocationException e) { e.printStackTrace(); throw new IOException(e.getMessage()); } finally { try { if (inputStream != null) { inputStream.close(); } } catch (Exception e) {; } } } else { FileLock lock = destFO.lock(); InputStream inputStream = null; OutputStream outputStream = null; try { outputStream = destFO.getOutputStream(lock); inputStream = srcFO.getInputStream(); FileUtil.copy(inputStream, outputStream); } finally { try { if (inputStream != null) { inputStream.close(); } if (outputStream != null) { outputStream.close(); } lock.releaseLock(); } catch (Exception e) {; } } } }