Package java.nio.channels

Examples of java.nio.channels.FileChannel


      }
    }
  }
 
  public static void copyFile(File in, File outDir) throws IOException {
       FileChannel sourceChannel = null;
       FileChannel destinationChannel=null;
       try{
         sourceChannel= new
              FileInputStream(in).getChannel();
        
         File out=new File(outDir,in.getName());
         destinationChannel = new
              FileOutputStream(out).getChannel();
         sourceChannel.transferTo(0, sourceChannel.size(), destinationChannel);
       }
       finally{
         try{
           if (sourceChannel!=null){
             sourceChannel.close();
           }
         }
         finally{
           if (destinationChannel!=null){
             destinationChannel.close();
           }
         }
       }
  }
View Full Code Here


    if (locks.get(lockFileName) != null) {
        // We already own this lock, for a different FileHandler
        // object.  Try again.
        continue;
          }
    FileChannel fc;
    try {
        lockStream = new FileOutputStream(lockFileName);
        fc = lockStream.getChannel();
    } catch (IOException ix) {
        // We got an IOException while trying to open the file.
        // Try the next file.
        continue;
    }
    try {
        FileLock fl = fc.tryLock();
        if (fl == null) {
            // We failed to get the lock.  Try next file.
      continue;
        }
        // We got the lock OK.
View Full Code Here

  private void copyFile(File originFile, File destinationFile) {
    FileInputStream inputStream = null;
    FileOutputStream outputStream = null;
    try {
      inputStream = new FileInputStream(originFile);
      FileChannel srcChannel = inputStream.getChannel();

      if (!destinationFile.exists())
        destinationFile.createNewFile();

      outputStream = new FileOutputStream(destinationFile);
      FileChannel dstChannel = outputStream.getChannel();

      dstChannel.transferFrom(srcChannel, 0, srcChannel.size());

    } catch (IOException e) {
      throw new PersistenceException(e);
    } finally {
      closeCloseable(inputStream);
View Full Code Here

     *
     * @param source
     * @param dest
     */
    public static void copyFile(File source, File dest) {
        FileChannel in = null;
        FileChannel out = null;
        try {
            in = new FileInputStream(source).getChannel();
            out = new FileOutputStream(dest).getChannel();

            long size = in.size();
            MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, size);

            out.write(buf);

            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }

        } catch (Exception e) {
            LOG.error("Unable to copy " + source + " to " + dest);
        }
View Full Code Here

  public static final void copyFileNIO(File originFile, File destinationFile) {
    FileInputStream inputStream = null;
    FileOutputStream outputStream = null;
    try {
      inputStream = new FileInputStream(originFile);
      FileChannel srcChannel = inputStream.getChannel();

      if (!destinationFile.exists())
        destinationFile.createNewFile();

      outputStream = new FileOutputStream(destinationFile);
      FileChannel dstChannel = outputStream.getChannel();

      long bytesToTransfer = srcChannel.size();
      long position = 0;
      while (bytesToTransfer > 0) {
        long bytesTransferred = dstChannel.transferFrom(srcChannel, position, bytesToTransfer);
        position += bytesTransferred;
        bytesToTransfer -= bytesTransferred;
      }

    } catch (IOException e) {
View Full Code Here

               + "deployment-test" + delim + module;
         String target = serverHome + delim + "deploy" + delim + module;
         log.info("source = " + source);
         log.info("target = " + target);

         FileChannel srcChannel = new FileInputStream(source).getChannel();
         FileChannel destChannel = new FileOutputStream(target).getChannel();
         srcChannel.transferTo(0, srcChannel.size(), destChannel);
         srcChannel.close();
         destChannel.close();

         // See if we can get connection to the data sources:
         boolean connected = connectToDataSource(jndiName1);
         assertTrue("Data source " + jndiName1 + " connected successful : "
               + connected, connected);
View Full Code Here

        response.setContentLength((int) f.length());
        String filename = (item.getDescription());
        response.setHeader("Content-Disposition", "attachment; filename=" + filename);

        FileInputStream fis = new FileInputStream(f);
        FileChannel in = fis.getChannel();
        WritableByteChannel out = Channels.newChannel(response.getOutputStream());
        try {
          in.transferTo(0, in.size(), out);
          service.removeFile(id);
        } catch (Exception e) {
          throw e;
        } finally {
          in.close();
          // make sure servlet doesn't append anything unnecessary:
          out.close();
        }
      }
    } catch (FileNotFoundException e) {
View Full Code Here

   */
  public static void copy(File source, File target) {
    if (source != null && source.exists()) {
      try {
        target.createNewFile();
        FileChannel sourceChannel = new FileInputStream(source)
            .getChannel();
        FileChannel targetChannel = new FileOutputStream(target)
            .getChannel();
        targetChannel.transferFrom(sourceChannel, 0,
            sourceChannel.size());
        sourceChannel.close();
        targetChannel.close();
      } catch (IOException e) {
        throw new RuntimeException(String.format(
            "Copy file from '%s' to '%s' failed (%s)",
            source.getAbsolutePath(), target.getAbsolutePath(),
            e.getMessage()));
View Full Code Here

    }
  }

  private static MappedByteBuffer map(RandomAccessFile raf, MapMode mode,
      long size) throws IOException {
    FileChannel channel = raf.getChannel();

    boolean threw = true;
    try {
      MappedByteBuffer mbb = channel.map(mode, 0, size);
      threw = false;
      return mbb;
    } finally {
      Closeables.close(channel, threw);
    }
View Full Code Here

         log.fine("Replacing legacy jboss header in: "+javaFile);
      RandomAccessFile raf = new RandomAccessFile(javaFile, "rw");
      File bakFile = new File(javaFile.getAbsolutePath()+".bak");
      FileOutputStream fos = new FileOutputStream(bakFile);
      fos.write(DEFAULT_HEADER.getBytes());
      FileChannel fc = raf.getChannel();
      long count = raf.length() - endOfHeader;
      fc.transferTo(endOfHeader, count, fos.getChannel());
      fc.close();
      fos.close();
      raf.close();
      if( javaFile.delete() == false )
         log.severe("Failed to delete java file: "+javaFile);
      if( bakFile.renameTo(javaFile) == false )
View Full Code Here

TOP

Related Classes of java.nio.channels.FileChannel

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.