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

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

本文整理了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

  1. public static File toTempFile(InputStream source, String fileName) {
  2. File file = null;
  3. try {
  4. file = File.createTempFile(fileName, null);
  5. IOUtils.copyAndClose(source, new FileOutputStream(file));
  6. } catch (IOException e) {
  7. final Logger log = LoggerFactory.getLogger(FileTool.class);
  8. log.error("Error when creating file: {}", fileName, e);
  9. }
  10. return file;
  11. }

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

  1. public static File toTempFile(InputStream source, String fileName) {
  2. File file = null;
  3. try {
  4. file = File.createTempFile(fileName, null);
  5. IOUtils.copyAndClose(source, new FileOutputStream(file));
  6. } catch (IOException e) {
  7. final Logger log = LoggerFactory.getLogger(FileTool.class);
  8. log.error("Error when creating file: {}", fileName, e);
  9. }
  10. return file;
  11. }

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

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

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

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

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

  1. /**
  2. * Starts Jackrabbit repository. If there was no repository it will be
  3. * created automatically. (this is how Jacrabbit works)
  4. *
  5. * @throws RepositoryException if failed
  6. */
  7. private void init() throws RepositoryException {
  8. try {
  9. String jackrabbitConfig;
  10. if (StringUtils.isEmpty(login)) {
  11. jackrabbitConfig = "/jackrabbit-repository.xml";
  12. } else {
  13. jackrabbitConfig = "/secure-jackrabbit-repository.xml";
  14. }
  15. // obtain real path to repository configuration file
  16. InputStream input = this.getClass().getResourceAsStream(jackrabbitConfig);
  17. File tempRepositorySettings = File.createTempFile("jackrabbit-repository", ".xml");
  18. // It could be cleaned-up on exit
  19. tempRepositorySettings.deleteOnExit();
  20. OutputStream tempRepositorySettingsStream = new FileOutputStream(tempRepositorySettings);
  21. IOUtils.copyAndClose(input, tempRepositorySettingsStream);
  22. createTransientRepo(tempRepositorySettings);
  23. } catch (IOException e) {
  24. throw new RepositoryException("Failed to init: " + e.getMessage(), e);
  25. }
  26. }

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

  1. /**
  2. * Starts Jackrabbit repository. If there was no repository it will be
  3. * created automatically. (this is how Jacrabbit works)
  4. *
  5. * @throws RepositoryException if failed
  6. */
  7. private void init() throws RepositoryException {
  8. try {
  9. String jackrabbitConfig;
  10. if (StringUtils.isEmpty(login)) {
  11. jackrabbitConfig = "/jackrabbit-repository.xml";
  12. } else {
  13. jackrabbitConfig = "/secure-jackrabbit-repository.xml";
  14. }
  15. // obtain real path to repository configuration file
  16. InputStream input = this.getClass().getResourceAsStream(jackrabbitConfig);
  17. File tempRepositorySettings = File.createTempFile("jackrabbit-repository", ".xml");
  18. // It could be cleaned-up on exit
  19. tempRepositorySettings.deleteOnExit();
  20. OutputStream tempRepositorySettingsStream = new FileOutputStream(tempRepositorySettings);
  21. IOUtils.copyAndClose(input, tempRepositorySettingsStream);
  22. createTransientRepo(tempRepositorySettings);
  23. } catch (IOException e) {
  24. throw new RepositoryException("Failed to init: " + e.getMessage(), e);
  25. }
  26. }

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

  1. public static File prepareDeploymentOpenLResource(String deploymentId, String resource) throws IOException {
  2. RepositoryService repositoryService = Context.getProcessEngineConfiguration().getRepositoryService();
  3. InputStream inputStream = repositoryService.getResourceAsStream(deploymentId, resource);
  4. if (inputStream == null) {
  5. throw new ResourceNotFoundException(String.format("No resource found with name '%s'!", resource));
  6. }
  7. final File workspaceFolder = Files.createTempDirectory("openl").toFile();
  8. if (resource.endsWith(".zip")) {
  9. // Unzip
  10. ZipUtils.extractAll(inputStream, workspaceFolder);
  11. } else {
  12. // Copy
  13. File resourceFile = new File(workspaceFolder, resource);
  14. FileOutputStream fos = new FileOutputStream(resourceFile);
  15. IOUtils.copyAndClose(inputStream, fos);
  16. }
  17. Runtime.getRuntime().addShutdownHook(new Thread() {
  18. @Override
  19. public void run() {
  20. FileUtils.deleteQuietly(workspaceFolder);
  21. }
  22. });
  23. return workspaceFolder;
  24. }
  25. }

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

  1. public static File prepareDeploymentOpenLResource(String deploymentId, String resource) throws IOException {
  2. RepositoryService repositoryService = Context.getProcessEngineConfiguration().getRepositoryService();
  3. InputStream inputStream = repositoryService.getResourceAsStream(deploymentId, resource);
  4. if (inputStream == null) {
  5. throw new ResourceNotFoundException(String.format("No resource found with name '%s'!", resource));
  6. }
  7. final File workspaceFolder = Files.createTempDirectory("openl").toFile();
  8. if (resource.endsWith(".zip")) {
  9. // Unzip
  10. ZipUtils.extractAll(inputStream, workspaceFolder);
  11. } else {
  12. // Copy
  13. File resourceFile = new File(workspaceFolder, resource);
  14. FileOutputStream fos = new FileOutputStream(resourceFile);
  15. IOUtils.copyAndClose(inputStream, fos);
  16. }
  17. Runtime.getRuntime().addShutdownHook(new Thread() {
  18. @Override
  19. public void run() {
  20. FileUtils.deleteQuietly(workspaceFolder);
  21. }
  22. });
  23. return workspaceFolder;
  24. }
  25. }

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

  1. protected InputStream changeFileIfNeeded(String fileName, InputStream inputStream) throws ProjectException {
  2. if (ProjectDescriptorBasedResolvingStrategy.PROJECT_DESCRIPTOR_FILE_NAME.equals(fileName)) {
  3. // 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.
  4. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  5. try {
  6. IOUtils.copyAndClose(inputStream, outputStream);
  7. } catch (IOException e) {
  8. throw new ProjectException(e.getMessage(), e);
  9. }
  10. ByteArrayInputStream copy = new ByteArrayInputStream(outputStream.toByteArray());
  11. try {
  12. XmlProjectDescriptorSerializer serializer = new XmlProjectDescriptorSerializer(false);
  13. ProjectDescriptor projectDescriptor = serializer.deserialize(copy);
  14. projectDescriptor.setName(getProjectName());
  15. return IOUtils.toInputStream(serializer.serialize(projectDescriptor));
  16. } catch (Exception e) {
  17. log.warn(e.getMessage(), e);
  18. copy.reset();
  19. return copy;
  20. }
  21. } else {
  22. return inputStream;
  23. }
  24. }

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

  1. protected InputStream changeFileIfNeeded(String fileName, InputStream inputStream) throws ProjectException {
  2. if (ProjectDescriptorBasedResolvingStrategy.PROJECT_DESCRIPTOR_FILE_NAME.equals(fileName)) {
  3. // 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.
  4. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  5. try {
  6. IOUtils.copyAndClose(inputStream, outputStream);
  7. } catch (IOException e) {
  8. throw new ProjectException(e.getMessage(), e);
  9. }
  10. ByteArrayInputStream copy = new ByteArrayInputStream(outputStream.toByteArray());
  11. try {
  12. XmlProjectDescriptorSerializer serializer = new XmlProjectDescriptorSerializer(false);
  13. ProjectDescriptor projectDescriptor = serializer.deserialize(copy);
  14. projectDescriptor.setName(getProjectName());
  15. return IOUtils.toInputStream(serializer.serialize(projectDescriptor));
  16. } catch (Exception e) {
  17. log.warn(e.getMessage(), e);
  18. copy.reset();
  19. return copy;
  20. }
  21. } else {
  22. return inputStream;
  23. }
  24. }

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

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

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

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

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

  1. @Override
  2. public InputStream transform(AProjectResource resource) throws ProjectException {
  3. if (isProjectDescriptor(resource)) {
  4. // 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.
  5. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  6. try {
  7. IOUtils.copyAndClose(resource.getContent(), outputStream);
  8. } catch (IOException e) {
  9. throw new ProjectException(e.getMessage(), e);
  10. }
  11. ByteArrayInputStream copy = new ByteArrayInputStream(outputStream.toByteArray());
  12. try {
  13. IProjectDescriptorSerializer serializer = WebStudioUtils.getBean(ProjectDescriptorSerializerFactory.class)
  14. .getSerializer(resource);
  15. ProjectDescriptor projectDescriptor = serializer.deserialize(copy);
  16. projectDescriptor.setName(newProjectName);
  17. return IOUtils.toInputStream(serializer.serialize(projectDescriptor));
  18. } catch (Exception e) {
  19. log.warn(e.getMessage(), e);
  20. copy.reset();
  21. return copy;
  22. }
  23. }
  24. return resource.getContent();
  25. }

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

  1. @Override
  2. public InputStream transform(AProjectResource resource) throws ProjectException {
  3. if (isProjectDescriptor(resource)) {
  4. // 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.
  5. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  6. try {
  7. IOUtils.copyAndClose(resource.getContent(), outputStream);
  8. } catch (IOException e) {
  9. throw new ProjectException(e.getMessage(), e);
  10. }
  11. ByteArrayInputStream copy = new ByteArrayInputStream(outputStream.toByteArray());
  12. try {
  13. IProjectDescriptorSerializer serializer = WebStudioUtils.getBean(ProjectDescriptorSerializerFactory.class)
  14. .getSerializer(resource);
  15. ProjectDescriptor projectDescriptor = serializer.deserialize(copy);
  16. projectDescriptor.setName(newProjectName);
  17. return IOUtils.toInputStream(serializer.serialize(projectDescriptor));
  18. } catch (Exception e) {
  19. log.warn(e.getMessage(), e);
  20. copy.reset();
  21. return copy;
  22. }
  23. }
  24. return resource.getContent();
  25. }

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

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

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

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

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

  1. @Override
  2. public FileData copy(String srcName, FileData destData) throws IOException {
  3. Lock writeLock = repositoryLock.writeLock();
  4. try {
  5. writeLock.lock();
  6. File src = new File(localRepositoryPath, srcName);
  7. File dest = new File(localRepositoryPath, destData.getName());
  8. IOUtils.copyAndClose(new FileInputStream(src), new FileOutputStream(dest));
  9. git.add().addFilepattern(destData.getName()).call();
  10. RevCommit commit = git.commit()
  11. .setMessage(StringUtils.trimToEmpty(destData.getComment()))
  12. .setCommitter(destData.getAuthor(), "")
  13. .call();
  14. addTagToCommit(commit);
  15. push();
  16. } catch (Exception e) {
  17. reset();
  18. throw new IOException(e);
  19. } finally {
  20. writeLock.unlock();
  21. }
  22. return check(destData.getName());
  23. }

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

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

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

  1. @Override
  2. public FileData save(FileData data, InputStream stream) throws IOException {
  3. Lock writeLock = repositoryLock.writeLock();
  4. try {
  5. writeLock.lock();
  6. String fileInRepository = data.getName();
  7. File file = new File(localRepositoryPath, fileInRepository);
  8. createParent(file);
  9. IOUtils.copyAndClose(stream, new FileOutputStream(file));
  10. git.add().addFilepattern(fileInRepository).call();
  11. // TODO: Add possibility to set committer email
  12. RevCommit commit = git.commit()
  13. .setMessage(StringUtils.trimToEmpty(data.getComment()))
  14. .setCommitter(data.getAuthor(), "")
  15. .setOnly(fileInRepository)
  16. .call();
  17. addTagToCommit(commit);
  18. push();
  19. } catch (Exception e) {
  20. reset();
  21. throw new IOException(e);
  22. } finally {
  23. writeLock.unlock();
  24. }
  25. return check(data.getName());
  26. }

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

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

相关文章