Package com.google.common.io

Examples of com.google.common.io.Closer


      int fileId = tachyonClient.createFile(dstPath);
      if (fileId == -1) {
        return -1;
      }
      TachyonFile tFile = tachyonClient.getFile(fileId);
      Closer closer = Closer.create();
      try {
        OutStream os = closer.register(tFile.getOutStream(UserConf.get().DEFAULT_WRITE_TYPE));
        FileInputStream in = closer.register(new FileInputStream(src));
        FileChannel channel = closer.register(in.getChannel());
        ByteBuffer buf = ByteBuffer.allocate(Constants.KB);
        while (channel.read(buf) != -1) {
          buf.flip();
          os.write(buf.array(), 0, buf.limit());
        }
      } finally {
        closer.close();
      }
      return 0;
    } else {
      tachyonClient.mkdir(dstPath);
      for (String file : src.list()) {
View Full Code Here


    // tachyonClient.getFile() catches FileDoesNotExist exceptions and returns null
    if (tFile == null) {
      throw new IOException(srcPath.toString());
    }

    Closer closer = Closer.create();
    try {
      InStream is = closer.register(tFile.getInStream(ReadType.NO_CACHE));
      FileOutputStream out = closer.register(new FileOutputStream(dst));
      byte[] buf = new byte[512];
      int t = is.read(buf);
      while (t != -1) {
        out.write(buf, 0, t);
        t = is.read(buf);
      }
      System.out.println("Copied " + srcPath + " to " + dstPath);
      return 0;
    } finally {
      closer.close();
    }
  }
View Full Code Here

    if (size == -1) {
      LOG.error("Block file doesn't exist! blockId:" + blockId);
      return false;
    }
    boolean copySuccess = false;
    Closer closer = Closer.create();
    try {
      BlockHandler bhSrc = closer.register(getBlockHandler(blockId));
      BlockHandler bhDst = closer.register(dstDir.getBlockHandler(blockId));
      ByteBuffer srcBuf = bhSrc.read(0, (int) size);
      copySuccess = (bhDst.append(0, srcBuf) == size);
    } finally {
      closer.close();
    }
    if (copySuccess) {
      dstDir.addBlockId(blockId, size);
    }
    return copySuccess;
View Full Code Here

    // Because chmod doesn't have a lot of error or output messages, its safe to process the output
    // after the process is done. As of java 7, you can have the process redirect to System.out
    // and System.err without forking a process.
    // TODO when java 6 support is dropped, switch to
    // http://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html#inheritIO()
    Closer closer = Closer.create();
    try {
      ByteStreams.copy(closer.register(process.getInputStream()), System.out);
      ByteStreams.copy(closer.register(process.getErrorStream()), System.err);
    } catch (Throwable e) {
      throw closer.rethrow(e);
    } finally {
      closer.close();
    }
  }
View Full Code Here

            continue;
          }
          for (int k = 0; k < fileInfo.blockIds.size(); k ++) {
            lockBlock(fileInfo.blockIds.get(k), Users.CHECKPOINT_USER_ID);
          }
          Closer closer = Closer.create();
          long fileSizeByte = 0;
          try {
            OutputStream os =
                closer.register(mCheckpointUfs.create(midPath, (int) fileInfo.getBlockSizeByte()));
            for (int k = 0; k < fileInfo.blockIds.size(); k ++) {
              File tempFile =
                  new File(CommonUtils.concat(mLocalDataFolder.toString(),
                      fileInfo.blockIds.get(k)));
              fileSizeByte += tempFile.length();
              InputStream is = closer.register(new FileInputStream(tempFile));
              byte[] buf = new byte[16 * Constants.KB];
              int got = is.read(buf);
              while (got != -1) {
                os.write(buf, 0, got);
                got = is.read(buf);
              }
            }
          } finally {
            closer.close();
            for (int k = 0; k < fileInfo.blockIds.size(); k ++) {
              unlockBlock(fileInfo.blockIds.get(k), Users.CHECKPOINT_USER_ID);
            }
          }
          if (!mCheckpointUfs.rename(midPath, dstPath)) {
View Full Code Here

    int blockLockId = mTachyonFS.getBlockLockId();
    if (!mTachyonFS.lockBlock(blockId, blockLockId)) {
      return null;
    }
    String localFileName = getLocalFilename(blockIndex);
    Closer closer = Closer.create();
    if (localFileName != null) {
      try {
        RandomAccessFile localFile = closer.register(new RandomAccessFile(localFileName, "r"));

        long fileLength = localFile.length();
        String error = null;
        if (offset > fileLength) {
          error = String.format("Offset(%d) is larger than file length(%d)", offset, fileLength);
        }
        if (error == null && len != -1 && offset + len > fileLength) {
          error =
              String.format("Offset(%d) plus length(%d) is larger than file length(%d)", offset,
                  len, fileLength);
        }
        if (error != null) {
          throw new IOException(error);
        }

        if (len == -1) {
          len = fileLength - offset;
        }

        FileChannel localFileChannel = closer.register(localFile.getChannel());
        final ByteBuffer buf = localFileChannel.map(FileChannel.MapMode.READ_ONLY, offset, len);
        mTachyonFS.accessLocalBlock(blockId);
        return new TachyonByteBuffer(mTachyonFS, buf, blockId, blockLockId);
      } catch (FileNotFoundException e) {
        LOG.info(localFileName + " is not on local disk.");
      } catch (IOException e) {
        LOG.warn("Failed to read local file " + localFileName + " because:", e);
      } finally {
        closer.close();
      }
    }

    mTachyonFS.unlockBlock(blockId, blockLockId);
    return null;
View Full Code Here

        this.baseCommand = constructBaseCommand();
    }

    private File extractYSlow() throws IOException {
        File yslow = File.createTempFile("yslow", ".js");
        Closer closer = Closer.create();
        try {

            InputStream in = this.getClass().getClassLoader()
                    .getResourceAsStream(YSLOW);
            OutputStream out = new FileOutputStream(yslow);
            closer.register(in);
            closer.register(out);
            IOUtils.copy(in, out);
        } catch (Throwable t) {
            closer.rethrow(t);
        } finally {
            closer.close();
        }
        yslow.deleteOnExit();
        return yslow;
    }
View Full Code Here

  /**
   * Create a temporary ANT file for executing JUnit4 ANT task.
   */
  private File createTemporaryAntFile(Document doc) throws IOException {
    Closer closer = Closer.create();
    try {
      File antFile = File.createTempFile("junit4-ant-", ".xml", dir);
      OutputStream os = closer.register(new FileOutputStream(antFile));
      XMLWriter xmlWriter = new XMLWriter(os, OutputFormat.createPrettyPrint());
      xmlWriter.write(doc);
      return antFile;
    } catch (Throwable t) {
      throw closer.rethrow(t);
    } finally {
      closer.close();
    }
  }
View Full Code Here

            targetWork.mkdirs();
        }

        getLog().debug("Resolved target artifact to " + artifactFile.toString());

        Closer closer = Closer.create();

        Container container = new Container(artifactFile.getName());

        try {
            ZipInputStream zis = closer.register(new ZipInputStream(new BufferedInputStream(new FileInputStream(artifactFile))));


            getLog().info("Extracting " + artifactFile + " to " + targetWork);
            ZipFileIteratorAndExtractor iterator = new ZipFileIteratorAndExtractor(container, zis, targetWork);
            iterator.extract();

            getLog().info("Retrieving Properties");
            Properties properties = getProperties();

            getLog().info("Processing Files");
            TemplateProcessor processor = new TemplateProcessor(properties, tokenStart, tokenEnd, getLog());
            FileTemplating.processFiles(targetWork, processor);

            getLog().info("Compressing to " + finalFile);
            ZipOutputStream zos = closer.register(new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(finalFile))));
            ZipFileCompressor compressor = new ZipFileCompressor(container, zos, targetWork);
            compressor.compress();

            getLog().info("Done.");
        } catch (IOException e) {
            getLog().error(e);
            throw new MojoExecutionException(e.getMessage(), e);
        } finally {
            try {
                closer.close();
            } catch (IOException e) {
                throw new MojoExecutionException(e.getMessage(), e);
            }
        }
    }
View Full Code Here

        properties.putAll(System.getProperties());
        properties.putAll(project.getProperties());

        if (propertySources != null) {

            Closer closer = Closer.create();
            try {
                for (String propertySource : propertySources) {
                    loadPropertySource(properties, closer, propertySource);
                }
            } finally {
                closer.close();
            }
        }
        return properties;
    }
View Full Code Here

TOP

Related Classes of com.google.common.io.Closer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.