org.openl.util.IOUtils.copyAndClose()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(12.5k)|赞(0)|评价(0)|浏览(143)

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

IOUtils.copyAndClose介绍

[英]Copy bytes from InputStream to an OutputStream and close them after.

This method uses the provided buffer, so there is no need to use a BufferedInputStream.

The buffer size is given by #DEFAULT_BUFFER_SIZE.
[中]将字节从InputStream复制到OutputStream,然后关闭它们。
此方法使用提供的缓冲区,因此不需要使用BufferedInputStream
缓冲区大小由#默认_buffer_size给出。

代码示例

代码示例来源:origin: org.openl/org.openl.commons

public static File toTempFile(InputStream source, String fileName) {
  File file = null;
  try {
    file = File.createTempFile(fileName, null);
    IOUtils.copyAndClose(source, new FileOutputStream(file));
  } catch (IOException e) {
    final Logger log = LoggerFactory.getLogger(FileTool.class);
    log.error("Error when creating file: {}", fileName, e);
  }
  return file;
}

代码示例来源:origin: openl-tablets/openl-tablets

public static File toTempFile(InputStream source, String fileName) {
  File file = null;
  try {
    file = File.createTempFile(fileName, null);
    IOUtils.copyAndClose(source, new FileOutputStream(file));
  } catch (IOException e) {
    final Logger log = LoggerFactory.getLogger(FileTool.class);
    log.error("Error when creating file: {}", fileName, e);
  }
  return file;
}

代码示例来源:origin: openl-tablets/openl-tablets

private void deployInternal(String originalName, InputStream in, boolean overridable) throws Exception {
  ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  IOUtils.copyAndClose(in, outputStream);
  String name = originalName != null ? originalName : DEFAULT_DEPLOYMENT_NAME + System.currentTimeMillis();
  if (outputStream.size() == 0) {
    throw new RuntimeException("Zip file input stream is empty");
  }
  DeploymentDTO deployment = new DeploymentDTO(name,
    new ByteArrayInputStream(outputStream.toByteArray()),
    outputStream.size());
  deployInternal(deployment, overridable);
}

代码示例来源:origin: org.openl.rules/org.openl.rules.ruleservice.deployer

private void deployInternal(String originalName, InputStream in, boolean overridable) throws Exception {
  ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  IOUtils.copyAndClose(in, outputStream);
  String name = originalName != null ? originalName : DEFAULT_DEPLOYMENT_NAME + System.currentTimeMillis();
  if (outputStream.size() == 0) {
    throw new RuntimeException("Zip file input stream is empty");
  }
  DeploymentDTO deployment = new DeploymentDTO(name,
    new ByteArrayInputStream(outputStream.toByteArray()),
    outputStream.size());
  deployInternal(deployment, overridable);
}

代码示例来源:origin: org.openl.rules/org.openl.rules.repository.jcr.jackrabbit

/**
 * Starts Jackrabbit repository. If there was no repository it will be
 * created automatically. (this is how Jacrabbit works)
 *
 * @throws RepositoryException if failed
 */
private void init() throws RepositoryException {
  try {
    String jackrabbitConfig;
    if (StringUtils.isEmpty(login)) {
      jackrabbitConfig = "/jackrabbit-repository.xml";
    } else {
      jackrabbitConfig = "/secure-jackrabbit-repository.xml";
    }
    // obtain real path to repository configuration file
    InputStream input = this.getClass().getResourceAsStream(jackrabbitConfig);
    File tempRepositorySettings = File.createTempFile("jackrabbit-repository", ".xml");
    // It could be cleaned-up on exit
    tempRepositorySettings.deleteOnExit();
    OutputStream tempRepositorySettingsStream = new FileOutputStream(tempRepositorySettings);
    IOUtils.copyAndClose(input, tempRepositorySettingsStream);
    createTransientRepo(tempRepositorySettings);
  } catch (IOException e) {
    throw new RepositoryException("Failed to init: " + e.getMessage(), e);
  }
}

代码示例来源:origin: openl-tablets/openl-tablets

/**
 * Starts Jackrabbit repository. If there was no repository it will be
 * created automatically. (this is how Jacrabbit works)
 *
 * @throws RepositoryException if failed
 */
private void init() throws RepositoryException {
  try {
    String jackrabbitConfig;
    if (StringUtils.isEmpty(login)) {
      jackrabbitConfig = "/jackrabbit-repository.xml";
    } else {
      jackrabbitConfig = "/secure-jackrabbit-repository.xml";
    }
    // obtain real path to repository configuration file
    InputStream input = this.getClass().getResourceAsStream(jackrabbitConfig);
    File tempRepositorySettings = File.createTempFile("jackrabbit-repository", ".xml");
    // It could be cleaned-up on exit
    tempRepositorySettings.deleteOnExit();
    OutputStream tempRepositorySettingsStream = new FileOutputStream(tempRepositorySettings);
    IOUtils.copyAndClose(input, tempRepositorySettingsStream);
    createTransientRepo(tempRepositorySettings);
  } catch (IOException e) {
    throw new RepositoryException("Failed to init: " + e.getMessage(), e);
  }
}

代码示例来源:origin: openl-tablets/openl-tablets

public static File prepareDeploymentOpenLResource(String deploymentId, String resource) throws IOException {
    RepositoryService repositoryService = Context.getProcessEngineConfiguration().getRepositoryService();
    InputStream inputStream = repositoryService.getResourceAsStream(deploymentId, resource);
    if (inputStream == null) {
      throw new ResourceNotFoundException(String.format("No resource found with name '%s'!", resource));
    }

    final File workspaceFolder = Files.createTempDirectory("openl").toFile();

    if (resource.endsWith(".zip")) {
      // Unzip
      ZipUtils.extractAll(inputStream, workspaceFolder);
    } else {
      // Copy
      File resourceFile = new File(workspaceFolder, resource);
      FileOutputStream fos = new FileOutputStream(resourceFile);
      IOUtils.copyAndClose(inputStream, fos);
    }

    Runtime.getRuntime().addShutdownHook(new Thread() {
      @Override
      public void run() {
        FileUtils.deleteQuietly(workspaceFolder);
      }
    });
    return workspaceFolder;
  }
}

代码示例来源:origin: org.openl.rules/org.openl.rules.activiti

public static File prepareDeploymentOpenLResource(String deploymentId, String resource) throws IOException {
    RepositoryService repositoryService = Context.getProcessEngineConfiguration().getRepositoryService();
    InputStream inputStream = repositoryService.getResourceAsStream(deploymentId, resource);
    if (inputStream == null) {
      throw new ResourceNotFoundException(String.format("No resource found with name '%s'!", resource));
    }

    final File workspaceFolder = Files.createTempDirectory("openl").toFile();

    if (resource.endsWith(".zip")) {
      // Unzip
      ZipUtils.extractAll(inputStream, workspaceFolder);
    } else {
      // Copy
      File resourceFile = new File(workspaceFolder, resource);
      FileOutputStream fos = new FileOutputStream(resourceFile);
      IOUtils.copyAndClose(inputStream, fos);
    }

    Runtime.getRuntime().addShutdownHook(new Thread() {
      @Override
      public void run() {
        FileUtils.deleteQuietly(workspaceFolder);
      }
    });
    return workspaceFolder;
  }
}

代码示例来源:origin: org.openl.rules/org.openl.rules.webstudio

protected InputStream changeFileIfNeeded(String fileName, InputStream inputStream) throws ProjectException {
  if (ProjectDescriptorBasedResolvingStrategy.PROJECT_DESCRIPTOR_FILE_NAME.equals(fileName)) {
    // Read the stream to memory and try to parse it and then change project name. If it can't be parsed return original rules.xml.
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try {
      IOUtils.copyAndClose(inputStream, outputStream);
    } catch (IOException e) {
      throw new ProjectException(e.getMessage(), e);
    }
    ByteArrayInputStream copy = new ByteArrayInputStream(outputStream.toByteArray());
    try {
      XmlProjectDescriptorSerializer serializer = new XmlProjectDescriptorSerializer(false);
      ProjectDescriptor projectDescriptor = serializer.deserialize(copy);
      projectDescriptor.setName(getProjectName());
      return IOUtils.toInputStream(serializer.serialize(projectDescriptor));
    } catch (Exception e) {
      log.warn(e.getMessage(), e);
      copy.reset();
      return copy;
    }
  } else {
    return inputStream;
  }
}

代码示例来源:origin: openl-tablets/openl-tablets

protected InputStream changeFileIfNeeded(String fileName, InputStream inputStream) throws ProjectException {
  if (ProjectDescriptorBasedResolvingStrategy.PROJECT_DESCRIPTOR_FILE_NAME.equals(fileName)) {
    // Read the stream to memory and try to parse it and then change project name. If it can't be parsed return original rules.xml.
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try {
      IOUtils.copyAndClose(inputStream, outputStream);
    } catch (IOException e) {
      throw new ProjectException(e.getMessage(), e);
    }
    ByteArrayInputStream copy = new ByteArrayInputStream(outputStream.toByteArray());
    try {
      XmlProjectDescriptorSerializer serializer = new XmlProjectDescriptorSerializer(false);
      ProjectDescriptor projectDescriptor = serializer.deserialize(copy);
      projectDescriptor.setName(getProjectName());
      return IOUtils.toInputStream(serializer.serialize(projectDescriptor));
    } catch (Exception e) {
      log.warn(e.getMessage(), e);
      copy.reset();
      return copy;
    }
  } else {
    return inputStream;
  }
}

代码示例来源:origin: org.openl.rules/org.openl.rules.webstudio

String modifiedRules = serializer.serialize(projectDescriptor);
  IOUtils.copyAndClose(IOUtils.toInputStream(modifiedRules), new FileOutputStream(rules));
} catch (Exception e) {
  log.warn(e.getMessage(), e);

代码示例来源:origin: openl-tablets/openl-tablets

String modifiedRules = serializer.serialize(projectDescriptor);
  IOUtils.copyAndClose(IOUtils.toInputStream(modifiedRules), new FileOutputStream(rules));
} catch (Exception e) {
  log.warn(e.getMessage(), e);

代码示例来源:origin: openl-tablets/openl-tablets

@Override
public InputStream transform(AProjectResource resource) throws ProjectException {
  if (isProjectDescriptor(resource)) {
    // Read the stream to memory and try to parse it and then change project name. If it can't be parsed return original rules.xml.
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try {
      IOUtils.copyAndClose(resource.getContent(), outputStream);
    } catch (IOException e) {
      throw new ProjectException(e.getMessage(), e);
    }
    ByteArrayInputStream copy = new ByteArrayInputStream(outputStream.toByteArray());
    try {
      IProjectDescriptorSerializer serializer = WebStudioUtils.getBean(ProjectDescriptorSerializerFactory.class)
          .getSerializer(resource);
      ProjectDescriptor projectDescriptor = serializer.deserialize(copy);
      projectDescriptor.setName(newProjectName);
      return IOUtils.toInputStream(serializer.serialize(projectDescriptor));
    } catch (Exception e) {
      log.warn(e.getMessage(), e);
      copy.reset();
      return copy;
    }
  }
  return resource.getContent();
}

代码示例来源:origin: org.openl.rules/org.openl.rules.webstudio

@Override
public InputStream transform(AProjectResource resource) throws ProjectException {
  if (isProjectDescriptor(resource)) {
    // Read the stream to memory and try to parse it and then change project name. If it can't be parsed return original rules.xml.
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try {
      IOUtils.copyAndClose(resource.getContent(), outputStream);
    } catch (IOException e) {
      throw new ProjectException(e.getMessage(), e);
    }
    ByteArrayInputStream copy = new ByteArrayInputStream(outputStream.toByteArray());
    try {
      IProjectDescriptorSerializer serializer = WebStudioUtils.getBean(ProjectDescriptorSerializerFactory.class)
          .getSerializer(resource);
      ProjectDescriptor projectDescriptor = serializer.deserialize(copy);
      projectDescriptor.setName(newProjectName);
      return IOUtils.toInputStream(serializer.serialize(projectDescriptor));
    } catch (Exception e) {
      log.warn(e.getMessage(), e);
      copy.reset();
      return copy;
    }
  }
  return resource.getContent();
}

代码示例来源:origin: openl-tablets/openl-tablets

IOUtils.copyAndClose(fis, outputStream);
} else {
  response.setStatus(HttpServletResponse.SC_NOT_FOUND);

代码示例来源:origin: openl-tablets/openl-tablets

try {
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  IOUtils.copyAndClose(inputStream, out);

代码示例来源:origin: openl-tablets/openl-tablets

@Override
public FileData copy(String srcName, FileData destData) throws IOException {
  Lock writeLock = repositoryLock.writeLock();
  try {
    writeLock.lock();
    File src = new File(localRepositoryPath, srcName);
    File dest = new File(localRepositoryPath, destData.getName());
    IOUtils.copyAndClose(new FileInputStream(src), new FileOutputStream(dest));
    git.add().addFilepattern(destData.getName()).call();
    RevCommit commit = git.commit()
        .setMessage(StringUtils.trimToEmpty(destData.getComment()))
        .setCommitter(destData.getAuthor(), "")
        .call();
    addTagToCommit(commit);
    push();
  } catch (Exception e) {
    reset();
    throw new IOException(e);
  } finally {
    writeLock.unlock();
  }
  return check(destData.getName());
}

代码示例来源:origin: org.openl.rules/org.openl.rules.webstudio

IOUtils.copyAndClose(fis, outputStream);
} else {
  response.setStatus(HttpServletResponse.SC_NOT_FOUND);

代码示例来源:origin: openl-tablets/openl-tablets

@Override
public FileData save(FileData data, InputStream stream) throws IOException {
  Lock writeLock = repositoryLock.writeLock();
  try {
    writeLock.lock();
    String fileInRepository = data.getName();
    File file = new File(localRepositoryPath, fileInRepository);
    createParent(file);
    IOUtils.copyAndClose(stream, new FileOutputStream(file));
    git.add().addFilepattern(fileInRepository).call();
    // TODO: Add possibility to set committer email
    RevCommit commit = git.commit()
        .setMessage(StringUtils.trimToEmpty(data.getComment()))
        .setCommitter(data.getAuthor(), "")
        .setOnly(fileInRepository)
        .call();
    addTagToCommit(commit);
    push();
  } catch (Exception e) {
    reset();
    throw new IOException(e);
  } finally {
    writeLock.unlock();
  }
  return check(data.getName());
}

代码示例来源:origin: openl-tablets/openl-tablets

IOUtils.copyAndClose(fileItem.getStream(), fileOutputStream);

相关文章