Java 类org.eclipse.core.runtime.jobs.IJobFunction 实例源码

项目:org.csstudio.display.builder    文件:WorkspaceResourceHelperImpl.java   
@Override
public OutputStream writeWorkspaceResource(final String resource_name) throws Exception
{
    final IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    final IFile file = root.getFile(new Path(resource_name));

    // IFile API requires an InputStream for the content.
    // That content, however, doesn't exist at this time, because
    // it's about to be written to an OutputStream by the caller
    // of this function.
    // -> Provide pipe, with background job to read from pipe and write the file
    final PipedOutputStream buf = new PipedOutputStream();
    final PipedInputStream input = new PipedInputStream(buf);
    final IJobFunction writer = monitor ->
    {
        try
        {
            if (file.exists())
                file.setContents(input, true, false, monitor);
            else
                file.create(input, true, monitor);
        }
        catch (Exception ex)
        {
            logger.log(Level.WARNING, "Cannot write to " + resource_name, ex);
        }
        return Status.OK_STATUS;
    };

    Job.create("Workspace Writer", writer).schedule();

    // Provide caller with output end of pipe to fill
    return buf;
}