Examples of FileChannel


Examples of java.nio.channels.FileChannel

    setContentType(getContentTypeByFileExtension(file));
    setContentLength((int) file.length());
    removeHeader("Transfer-Encoding");
   
    RandomAccessFile raf = new RandomAccessFile(file, "r");
    FileChannel fc = raf.getChannel();

    ByteBuffer buffer = ByteBuffer.allocate(getContentLength());
    fc.read(buffer);
    fc.close();
    raf.close();
    buffer.flip();
   
    getNonBlockingBody().append(true, buffer);
    getNonBlockingBody().setComplete(true);
View Full Code Here

Examples of java.nio.channels.FileChannel

        int contentLength = QAUtil.readContentLength(header);
        File tempFile = QAUtil.createTempfile();
       
        RandomAccessFile raf = new RandomAccessFile(tempFile, "rw");
        FileChannel fc = raf.getChannel();
       
        con.transferTo(fc, contentLength);
       
        fc.close();
        raf.close();
       
        Assert.assertTrue(QAUtil.isEquals(file, tempFile));

        tempFile.delete();
View Full Code Here

Examples of java.nio.channels.FileChannel

    Assert.assertEquals(200, response.getStatus());

    File tempFile = QAUtil.createTempfile();
   
    RandomAccessFile raf = new RandomAccessFile(tempFile, "rw");
    FileChannel fc = raf.getChannel();
   
    BodyDataSource source = response.getBody();
    source.transferTo(fc);
   
    fc.close();
    raf.close();
   
    Assert.assertTrue(QAUtil.isEquals(file, tempFile));

    tempFile.delete();
View Full Code Here

Examples of java.nio.channels.FileChannel

    IHttpResponse response = respHdl.getResponse();
    Assert.assertEquals(200, response.getStatus());

    File tempFile = QAUtil.createTempfile();   
    RandomAccessFile raf = new RandomAccessFile(tempFile, "rw");
    FileChannel fc = raf.getChannel();
   
    BodyDataSource source = response.getBody();
    source.transferTo(fc);
   
    fc.close();
    raf.close();
   
    Assert.assertTrue(QAUtil.isEquals(file, tempFile));

View Full Code Here

Examples of java.nio.channels.FileChannel

 
    int contentLength = QAUtil.readContentLength(header);
    File tempFile = QAUtil.createTempfile();
   
    RandomAccessFile raf = new RandomAccessFile(tempFile, "rw");
    FileChannel fc = raf.getChannel();
   
    con.transferTo(fc, contentLength);
   
    fc.close();
    raf.close();
   
    Assert.assertTrue(QAUtil.isEquals(file, tempFile));
 
    tempFile.delete();
View Full Code Here

Examples of java.nio.channels.FileChannel

      BlockingBodyDataSource dataSource = response.getBlockingBody();
     
      File tempFile = QAUtil.createTempfile();
       
        RandomAccessFile raf = new RandomAccessFile(tempFile, "rw");
        FileChannel fc = raf.getChannel();
       
        dataSource.transferTo(fc);
        fc.close();
        raf.close();
       
        Assert.assertTrue(QAUtil.isEquals(tempFile, file));
        dataSource.close();
     
View Full Code Here

Examples of java.nio.channels.FileChannel

        BlockingBodyDataSource dataSource = response.getBlockingBody();
       
        File tempFile = QAUtil.createTempfile();
       
        RandomAccessFile raf = new RandomAccessFile(tempFile, "rw");
        FileChannel fc = raf.getChannel();
       
        dataSource.transferTo(fc, length);
        fc.close();
        raf.close();
       
        Assert.assertTrue(QAUtil.isEquals(tempFile, file));
        dataSource.close();
       
View Full Code Here

Examples of java.nio.channels.FileChannel

   
    File file = QAUtil.createTempfile();
    System.out.println("write to file " + file.getAbsolutePath());
     
    RandomAccessFile raf = new RandomAccessFile(file, "rw");
    FileChannel fc = raf.getChannel();
    response.getBlockingBody().transferTo(fc);
    fc.close();
    raf.close();

    LineNumberReader lnr = new LineNumberReader(new FileReader(file));
    String line = lnr.readLine();
   
View Full Code Here

Examples of java.nio.channels.FileChannel

    * @param mode the open mode
    * @throws java.io.IOException on error
    */
  MMapRandomAccessFile(String location, String mode ) throws IOException {
    super(location, mode, 1);
    FileChannel channel = file.getChannel();
    source = channel.map( readonly ? FileChannel.MapMode.READ_ONLY : FileChannel.MapMode.READ_WRITE, (long) 0, channel.size());
    channel.close();

    bufferStart = 0;
    dataSize = (int) channel.size();
    dataEnd = channel.size();
    filePosition = 0;
    buffer = null;
    endOfFile = false;
  }
View Full Code Here

Examples of java.nio.channels.FileChannel

    // only write out if something changed after the cache file was last written, or if the file has been deleted
    if (!cacheDirty && cacheFile.exists())
      return;

    FileChannel channel = null;
    try {
      if (!cacheFile.exists()) {
        File dir = cacheFile.getParentFile();
        if (!dir.mkdirs())
          logger.error("Cant make cache directory= "+cacheFile);
      }

      // Get a file channel for the file
      FileOutputStream fos = new FileOutputStream(cacheFile);
      channel = fos.getChannel();

      // Try acquiring the lock without blocking. This method returns
      // null or throws an exception if the file is already locked.
      FileLock lock;
      try {
        lock = channel.tryLock();
      } catch (OverlappingFileLockException e) {
        // File is already locked in this thread or virtual machine
        return; // give up
      }
      if (lock == null) return;

      PrintWriter out = new PrintWriter(fos);
      out.print("<?xml version='1.0' encoding='UTF-8'?>\n");
      out.print("<aggregation xmlns='http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2' version='3' ");
      out.print("type='" + type + "' ");
      if (dimName != null)
        out.print("dimName='" + dimName + "' ");
      if (datasetManager.getRecheck() != null)
        out.print("recheckEvery='" + datasetManager.getRecheck() + "' ");
      out.print(">\n");

      List<Dataset> nestedDatasets = getDatasets();
      for (Dataset dataset : nestedDatasets) {
        DatasetOuterDimension dod = (DatasetOuterDimension) dataset;
        if (dod.getId() == null) logger.warn("id is null");

        out.print("  <netcdf id='" + dod.getId() + "' ");
        out.print("ncoords='" + dod.getNcoords(null) + "' >\n");

        for (CacheVar pv : cacheList) {
          Array data = pv.getData(dod.getId());
          if (data != null) {
            out.print("    <cache varName='" + pv.varName + "' >");
            NCdumpW.printArray(data, out);
            out.print("</cache>\n");
            if (logger.isDebugEnabled())
              logger.debug(" wrote array = " + pv.varName + " nelems= "+data.getSize()+" for "+dataset.getLocation());
          }
        }
        out.print("  </netcdf>\n");
      }

      out.print("</aggregation>\n");
      out.close(); // this also closes the  channel and releases the lock

      cacheFile.setLastModified( datasetManager.getLastScanned());
      cacheDirty = false;

      if (logger.isDebugEnabled())
              logger.debug("Aggregation persisted = " + cacheFile.getPath() + " lastModified= " + new Date(datasetManager.getLastScanned()));

    } finally {
      if (channel != null)
        channel.close();
    }
  }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.