org.openide.filesystems.FileLock类的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(12.4k)|赞(0)|评价(0)|浏览(171)

本文整理了Java中org.openide.filesystems.FileLock类的一些代码示例,展示了FileLock类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。FileLock类的具体详情如下:
包路径:org.openide.filesystems.FileLock
类名称:FileLock

FileLock介绍

[英]Represents an acquired lock on a FileObject. Typical usage includes locking the file in the editor on first modification, and then using this object to ensure exclusive access when overwriting the file (saving) by using FileObject#getOutputStream. Also used for renames, deletes, &c.

Note that such locks are only used to protect against concurrent write accesses, and are not used for read operations (i.e. they are not write-one-read-many locks). Normally this is sufficient protection. If you really need an atomic read, you may simply lock the file, perform the read, and unlock it when done. The file will still be protected against writes, although the read operation did not request a lock.
[中]表示在FileObject上获取的锁。典型用法包括在第一次修改时在编辑器中锁定文件,然后使用此对象确保在使用FileObject#getOutputStream覆盖文件(保存)时独占访问。也用于重命名、删除和c。
请注意,此类锁仅用于防止并发写访问,不用于读操作(即,它们是写一读多锁)。通常这是足够的保护。如果您确实需要原子读取,您可以简单地锁定文件,执行读取,并在完成后解锁它。尽管读取操作未请求锁定,但文件仍将受到写保护。

代码示例

代码示例来源:origin: org.netbeans.api/org-openide-filesystems

FileObject dest = destFolder.createData(newName, newExt);
  lock = dest.lock();
  bufIn = source.getInputStream();
  copy(bufIn, bufOut);
  copyAttributes(source, dest);
} finally {
  if (bufIn != null) {
    bufOut.close();
    lock.releaseLock();

代码示例来源:origin: org.netbeans.api/org-openide-filesystems

final FileLock lock = lock();
final OutputStream os;
try {
  os = getOutputStream(lock);
  return new FilterOutputStream(os) {
    @Override
  if (lock.isValid()) {
    lock.releaseLock();

代码示例来源:origin: org.netbeans.api/org-openide-filesystems

/** Finalize this object. Calls {@link #releaseLock} to release the lock if the program
  * for some reason failed to.
  */
  @Override
  public void finalize() {
    if(isValid()) {
      releaseLock();
      boolean assertOn = false;
      assert assertOn = true;
      if (assertOn) {
        StreamPool.LOG.log(Level.SEVERE, 
          "Not released lock for file: " + toString() + " (trapped in finalizer)", lockedBy); // NOI18N
      }
    }
  }
}

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-web-jsf

public static void createFile(FileObject target, String content, String encoding) throws IOException{
  FileLock lock = target.lock();
  try {
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(target.getOutputStream(lock), encoding));
    bw.write(content);
    bw.close();
  } finally {
    lock.releaseLock();
  }
}

代码示例来源:origin: org.netbeans.api/org-netbeans-modules-j2me-cdc-platform

public void run() throws java.io.IOException {
  FileLock lck;
  FileObject data;
  
  if (holder != null) {
    data = holder.getPrimaryEntry().getFile();
    lck = holder.getPrimaryEntry().takeLock();
  } else {
    FileObject folder = f.getPrimaryFile();
    String fn = FileUtil.findFreeFileName(folder, name, "xml");
    data = folder.createData(fn, "xml");
    lck = data.lock();
  }
  try {
    OutputStream ostm = data.getOutputStream(lck);
    PrintWriter writer = new PrintWriter(
      new OutputStreamWriter(ostm, "UTF8"));
    write(writer);
    writer.flush();
    writer.close();
    ostm.close();
  } finally {
    lck.releaseLock();
  }
  if (holder == null) {
    holder = (MultiDataObject)DataObject.find(data);
  }
}

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-cnd-makeproject

/**
   * Save an XML config file to a named path.
   * If the file does not yet exist, it is created.
   */
  private static void saveXml(Document doc, FileObject dir, String path) throws IOException {
    FileObject xml = FileUtil.createData(dir, path);
    FileLock lock = xml.lock();
    try {
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      XMLUtil.write(doc, baos, "UTF-8"); // NOI18N
      final byte[] data = MakeProjectHelperImpl.convertLineSeparator(baos, xml, xml.getParent());
      OutputStream os = SmartOutputStream.getSmartOutputStream(xml, lock);
      try {
        os.write(data);
      } finally {
        os.close();
      }
    } finally {
      lock.releaseLock();
    }
  }
}

代码示例来源:origin: org.codehaus.mevenide/nb-mvn-embedder

try {
  Document doc;
  FileObject fo = pomDir.getFileObject("profiles.xml");
  if (fo == null) {
    fo = pomDir.createData("profiles.xml");
    doc = factory.document(factory.element("profilesXml"));
  } else {
    inStr = fo.getInputStream();
    SAXBuilder builder = new SAXBuilder();
    doc = builder.build(inStr);
  ProfilesJDOMWriter writer = new ProfilesJDOMWriter();
  String encoding = profilesRoot.getModelEncoding() != null ? profilesRoot.getModelEncoding() : "UTF-8";
  outStr = new OutputStreamWriter(fo.getOutputStream(lock), encoding);
  Format form = Format.getRawFormat().setEncoding(encoding);
  writer.write(profilesRoot, doc, outStr, form);
  IOUtil.close(outStr);
  if (lock != null) {
    lock.releaseLock();

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-visualweb-palette

FileObject itemFile = folder.createData(fileName, "xml");
buff.append("</" + CodeClipHandler.XML_ROOT + ">");
FileLock lock = itemFile.lock();
OutputStream os = itemFile.getOutputStream(lock);
OutputStreamWriter out = new OutputStreamWriter(os, "UTF8");
out.write(buff.toString());
out.close();
os.close();
lock.releaseLock();

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-websvc-metro-samples

private static void unzip(InputStream source, FileObject targetFolder) throws IOException {
  //installation
  ZipInputStream zip=new ZipInputStream(source);
  try {
    ZipEntry ent;
    while ((ent = zip.getNextEntry()) != null) {
      if (ent.isDirectory()) {
        FileUtil.createFolder(targetFolder, ent.getName());
      } else {
        FileObject destFile = FileUtil.createData(targetFolder,ent.getName());
        FileLock lock = destFile.lock();
        try {
          OutputStream out = destFile.getOutputStream(lock);
          try {
            FileUtil.copy(zip, out);
          } finally {
            out.close();
          }
        } finally {
          lock.releaseLock();
        }
      }
    }
  } finally {
    zip.close();
  }
}

代码示例来源:origin: net.sf.squirrel-sql.thirdparty-non-maven/openide-loaders

public FileObject createFromTemplate (FileObject f, String name) throws IOException {
  String ext = getFile ().getExt ();
    name = FileUtil.findFreeFileName(
          f,
          getFile ().getName (), ext
        );
  FileObject fo = f.createData (name, ext);
    FileLock lock = fo.lock ();
    try {
      BufferedWriter w = new BufferedWriter (new OutputStreamWriter (fo.getOutputStream (lock)));
      lock.releaseLock ();
  FileUtil.copyAttributes (getFile (), fo);

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-mobility-cldcplatform

private void installDescriptors() {
  FileObject descRoot = FileUtil.getConfigFile("emulator-descriptor-inst"); //NOI18N
  FileObject platformsFolder;
  try {
    platformsFolder = FileUtil.createFolder(FileUtil.getConfigRoot(), "Services/Platforms/org-netbeans-api-java-Platform"); // NOI18N
  } catch (IOException ioe) {
    ErrorManager.getDefault().notify(ioe);
  if (descRoot != null) for (FileObject desc : descRoot.getChildren()) {
    InputStream is = null;
    OutputStreamWriter os = null;
    FileLock fl = null;
    File installRoot = FileUtil.toFile(desc);
    if (installRoot.isFile() && (installRoot = installRoot.getParentFile()) != null && (installRoot = installRoot.getParentFile()) != null && (installRoot = installRoot.getParentFile()) != null && desc.getExt().equals("xml")) {
      if (platformsFolder.getFileObject(desc.getNameExt()) != null) {
        ErrorManager.getDefault().log("Emulator description file already installed: " + installRoot.getAbsolutePath()); // NOI18N
      } else try {    
        FileObject fo = platformsFolder.createData(desc.getNameExt());
        fl = fo.lock();
        os = new OutputStreamWriter(fo.getOutputStream(fl), "UTF8"); //NOI18N
        os.write(MessageFormat.format(sb.toString(), new Object[]{XMLUtil.toAttributeValue(installRoot.getAbsolutePath())}));
        os.close();
        os = null;
        desc.delete();
        if (is != null) try {is.close();} catch (IOException ioe) {}
        if (os != null) try {os.close();} catch (IOException ioe) {}
        if (fl != null && fl.isValid()) fl.releaseLock();

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-gsf

public @Override void close() throws IOException {
    try {
      this.delegate.close();
    } finally {
      this.lock.releaseLock();
      synchronized (SourceFileObject.this) {
        text = null;
      }
    }            
  }                
}

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-mobility-cldcplatform

if (desc == null  ||  desc.length != 2)
  return false;
final FileObject platformsFolder = FileUtil.getConfigFile("Services/Platforms/org-netbeans-api-java-Platform"); // NOI18N
if (platformsFolder.getFileObject(desc[0]) != null) {
  ErrorManager.getDefault().log("Emulator description file already installed: " + emulatorPack.getAbsolutePath()); // NOI18N
  return false;
FileLock fl = null;
try {
  final FileObject fo = platformsFolder.createData(desc[0]);
  fl = fo.lock();
  bos = new OutputStreamWriter(fo.getOutputStream(fl), "UTF8"); //NOI18N
  bos.write(s);
} catch (IOException e) {
  ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
} finally {
  try { if (bos != null) bos.close(); } catch (IOException e) {}
  if (fl != null) fl.releaseLock();

代码示例来源:origin: org.netbeans.api/org-openide-filesystems

createFolder(fo, name);
} else {
  FileObject fd = createData(fo, name);
  FileLock lock = fd.lock();
    OutputStream os = fd.getOutputStream(lock);
      copy(jis, os);
    } finally {
      os.close();
    lock.releaseLock();
String prefix = fo.isRoot() ? dirName : (fo.getPath() + '/' + dirName);

代码示例来源:origin: net.sf.squirrel-sql.thirdparty-non-maven/openide-loaders

private FileObject createSerFile(
  DataFolder df, String name, Object obj
) throws IOException {
  FileLock lock = null;
  OutputStream ostream = null;
  FileObject newFile = null;
  try {
    FileObject fo = df.getPrimaryFile ();
    if (name == null) {
      name = FileUtil.findFreeFileName(
            df.getPrimaryFile (), getPrimaryFile ().getName (), getPrimaryFile ().getExt ()
          );
    }
    newFile = fo.getFileObject (name, SER_EXT);
    if (newFile == null) newFile = fo.createData (name, SER_EXT);
    lock = newFile.lock ();
    ostream = newFile.getOutputStream(lock);
    ObjectOutputStream p = new ObjectOutputStream(ostream);
    p.writeObject(obj);
    p.flush();
  } finally {
    if (ostream != null)
      ostream.close();
    if (lock != null)
      lock.releaseLock ();
  }
  return newFile;
}

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-javaee-project

private void copy(FileObject sourceFile, FileObject destFile) throws IOException {
  InputStream is = null;
  OutputStream os = null;
  FileLock fl = null;
  try {
    is = sourceFile.getInputStream();
    fl = destFile.lock();
    os = destFile.getOutputStream(fl);
    FileUtil.copy(is, os);
  } finally {
    if (is != null) {
      is.close();
    }
    if (os != null) {
      os.close();
    }
    if (fl != null) {
      fl.releaseLock();
    }
  }
}

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-websvc-restapi

public static FileObject copyFile(File testdir, String name, String[] replaceKeys, boolean overwrite) throws IOException {
  FileObject dir = FileUtil.toFileObject(testdir);
  FileObject fo = dir.getFileObject(name);
  if (fo == null) {
    fo = dir.createData(name);
  } else {
    if (!overwrite) {
  BufferedReader reader = null;
  try {
    lock = fo.lock();
    OutputStream os = fo.getOutputStream(lock);
    writer = new BufferedWriter(new OutputStreamWriter(os, Charset.forName("UTF-8")));
    InputStream is = RestSupport.class.getResourceAsStream("resources/" + name);
    reader = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
      lock.releaseLock();

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-visualweb-project-jsf

public void run() throws IOException {
    String fileName = LIB_JSF_NAME + "-" + convertedVersion; //NOI18N
    FileObject jsf = libsFolder.getFileObject(fileName + ".xml");
    if (jsf == null) {
      jsf = libsFolder.createData(fileName + ".xml");
    }
    FileLock lock = jsf.lock();
    try {
      PrintWriter out = new PrintWriter(new OutputStreamWriter(jsf.getOutputStream(lock)));                    
      try {
        String[] resources = new String[]{
          FileUtil.getArchiveRoot(jsfApiJar.toURI().toURL()).toString(),
          FileUtil.getArchiveRoot(jsfImplJar.toURI().toURL()).toString(),
          FileUtil.getArchiveRoot(digesterJar.toURI().toURL()).toString(),
          FileUtil.getArchiveRoot(loggingJar.toURI().toURL()).toString(),
          FileUtil.getArchiveRoot(collectionsJar.toURI().toURL()).toString(),
          FileUtil.getArchiveRoot(beanutilsJar.toURI().toURL()).toString()
        };
        createLibraryFile(out, fileName, resources);
      } finally {
        out.close();
      }
    } finally {
      lock.releaseLock();
    }
  }
});

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-cnd

@Override
protected FileObject createFromTemplate(FileObject template, FileObject folder, String name, Map<String, Object> parameters) throws IOException {
  String ext = FileUtil.getExtension(name);
  if (ext.length() != 0) {
    name = name.substring(0, name.length() - ext.length() - 1);
  FileObject targetFile = folder.createData(name, ext);
  ExecutionEnvironment executionEnvironment = FileSystemProvider.getExecutionEnvironment(targetFile);
  String lsType = "\n"; // NOI18N
  BufferedReader reader = new BufferedReader(new InputStreamReader(template.getInputStream(), encoding));
  try {
    FileLock lock = targetFile.lock();
    try {
      BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(targetFile.getOutputStream(lock), encoding));
      try {
        String line;
      lock.releaseLock();

代码示例来源:origin: dschanoeh/Kayak

private void readConnections() {
  FileObject root = FileUtil.getConfigRoot();
  FileObject connectionStorage = root.getFileObject("Connections.xml");
  if(connectionStorage==null)
    return;
  FileLock lock;
  try {
    lock = connectionStorage.lock();
    try {
      InputStream stream = connectionStorage.getInputStream();
      ConnectionManager.getGlobalConnectionManager().loadFromFile(stream);
      stream.close();
    } catch (Exception ex) {
      Exceptions.printStackTrace(ex);
    } finally {
      lock.releaseLock();
    }
  } catch(IOException ex) {
  }
}

相关文章