Java Code Examples for java.nio.channels.FileChannel#size()

The following examples show how to use java.nio.channels.FileChannel#size() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CacheUtils.java    From Android-UtilCode with Apache License 2.0 6 votes vote down vote up
/**
 * 缓存中读取字节数组
 *
 * @param key 键
 * @return 字节数组
 */
public byte[] getBytes(String key) {
    File file = mCacheManager.getFile(key);
    if (!file.exists()) return null;
    FileChannel fc = null;
    try {
        fc = new RandomAccessFile(file, "r").getChannel();
        int size = (int) fc.size();
        MappedByteBuffer byteBuffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, size).load();
        byte[] data = new byte[size];
        byteBuffer.get(data, 0, size);
        if (!CacheHelper.isDue(data)) {
            return CacheHelper.getDataWithoutDueTime(data);
        } else {
            mCacheManager.remove(key);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        CloseUtils.closeIO(fc);
    }
    return null;
}
 
Example 2
Source File: Files.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
private static Stream<String> createFileChannelLinesStream(FileChannel fc, Charset cs) throws IOException {
    try {
        // Obtaining the size from the FileChannel is much faster
        // than obtaining using path.toFile().length()
        long length = fc.size();
        // FileChannel.size() may in certain circumstances return zero
        // for a non-zero length file so disallow this case.
        if (length > 0 && length <= Integer.MAX_VALUE) {
            Spliterator<String> s = new FileChannelLinesSpliterator(fc, cs, 0, (int) length);
            return StreamSupport.stream(s, false)
                    .onClose(Files.asUncheckedRunnable(fc));
        }
    } catch (Error|RuntimeException|IOException e) {
        try {
            fc.close();
        } catch (IOException ex) {
            try {
                e.addSuppressed(ex);
            } catch (Throwable ignore) {
            }
        }
        throw e;
    }
    return null;
}
 
Example 3
Source File: ContentUtils.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
static long copyFileChannel(final FileChannel src, final WritableByteChannel dest, final int bufferSize)
    throws IOException {
    long bytes = 0L;
    long time = -System.currentTimeMillis();
    long size = src.size();
    while (bytes < size) {
        long bytesToTransfer = Math.min(bufferSize, size - bytes);
        long bytesTransfered = src.transferTo(bytes, bytesToTransfer, dest);

        bytes += bytesTransfered;

        if (LOGGER.isDebugEnabled()) {
            long percentage = Math.round(bytes / ((double) size) * 100.0);
            LOGGER.debug("overall bytes transfered: {} progress {}%", bytes, percentage);
        }

    }

    if (LOGGER.isDebugEnabled()) {
        time += System.currentTimeMillis();
        double kBps = (bytes / 1024.0) / (time / 1000.0);
        LOGGER.debug("Transfered: {} bytes in: {} s -> {} kbytes/s", bytes, time / 1000.0, kBps);
    }
    return bytes;
}
 
Example 4
Source File: FileLoader.java    From twister2 with Apache License 2.0 6 votes vote down vote up
public static List<Object> readFile(String fileName, MessageType dataType,
                                    KryoSerializer deserializer) {
  String outFileName = Paths.get(fileName).toString();
  FileChannel rwChannel;
  try {
    rwChannel = new RandomAccessFile(outFileName, "rw").getChannel();
    ByteBuffer os = rwChannel.map(FileChannel.MapMode.READ_ONLY, 0, rwChannel.size());

    List<Object> values = new ArrayList<>();
    // lets read the key values
    long totalRead = 0;
    while (totalRead < rwChannel.size()) {
      Object value;

      int dataSize = os.getInt();
      value = dataType.getDataPacker().unpackFromBuffer(os, dataSize);
      values.add(value);
      totalRead += Integer.BYTES + dataSize;
    }
    rwChannel.force(true);
    rwChannel.close();
    return values;
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example 5
Source File: MappedReadBuffer.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
static ReadBuffer create(RandomAccessFile file) throws IOException {
    FileChannel ch = file.getChannel();
    long size = ch.size();
    // if file size is more than 2 GB and when file mapping is
    // configured (default), use mapped file reader
    if (canUseFileMap() && (size <= Integer.MAX_VALUE)) {
        MappedByteBuffer buf;
        try {
            buf = ch.map(FileChannel.MapMode.READ_ONLY, 0, size);
            ch.close();
            return new MappedReadBuffer(buf);
        } catch (IOException exp) {
            exp.printStackTrace();
            System.err.println("File mapping failed, will use direct read");
            // fall through
        }
    } // else fall through
    return new FileReadBuffer(file);
}
 
Example 6
Source File: LogPageHandler.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public Page handle(URL url) {
      long size = 0;
String content = "";
String modified = "Not exist";
if (file != null && file.exists()) {
	try {
		FileInputStream fis = new FileInputStream(file);
		FileChannel channel = fis.getChannel();
		size = channel.size();
		ByteBuffer bb;
		if (size <= SHOW_LOG_LENGTH) {
			bb = ByteBuffer.allocate((int) size);
			channel.read(bb, 0);
		} else {
			int pos = (int) (size - SHOW_LOG_LENGTH);
			bb = ByteBuffer.allocate(SHOW_LOG_LENGTH);
			channel.read(bb, pos);
		}
		bb.flip();
		content = new String(bb.array()).replace("<", "&lt;")
				.replace(">", "&gt;").replace("\n", "<br/><br/>");
		modified = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
				.format(new Date(file.lastModified()));
	} catch (IOException e) {
	}
}
Level level = LogManager.getRootLogger().getLevel();
      List<List<String>> rows = new ArrayList<List<String>>();
      List<String> row = new ArrayList<String>();
      row.add(content);
      rows.add(row);
      return new Page("Log", "Log",  new String[] {(file == null ? "" : file.getName()) + ", " + size + " bytes, " + modified + ", " + level}, rows);
  }
 
Example 7
Source File: AsyncSenderImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public boolean run(boolean complete) {
    try {
        FileChannel source = fileChannel;
        long pos = source.position();
        long size = source.size();

        StreamSinkChannel dest = channel;
        if (dest == null) {
            if (callback == IoCallback.END_EXCHANGE) {
                if (exchange.getResponseContentLength() == -1 && !exchange.getResponseHeaders().contains(Headers.TRANSFER_ENCODING)) {
                    exchange.setResponseContentLength(size);
                }
            }
            channel = dest = exchange.getResponseChannel();
            if (dest == null) {
                throw UndertowMessages.MESSAGES.responseChannelAlreadyProvided();
            }
        }

        while (size - pos > 0) {
            long ret = dest.transferFrom(source, pos, size - pos);
            pos += ret;
            if (ret == 0) {
                source.position(pos);
                dest.getWriteSetter().set(this);
                dest.resumeWrites();
                return false;
            }
        }

        if (complete) {
            invokeOnComplete();
        }
    } catch (IOException e) {
        invokeOnException(callback, e);
    }

    return true;
}
 
Example 8
Source File: SocketOutputStream.java    From RDFS with Apache License 2.0 5 votes vote down vote up
/**
 * Transfers data from FileChannel using 
 * {@link FileChannel#transferTo(long, long, WritableByteChannel)}. 
 * 
 * Similar to readFully(), this waits till requested amount of 
 * data is transfered.
 * 
 * @param fileCh FileChannel to transfer data from.
 * @param position position within the channel where the transfer begins
 * @param count number of bytes to transfer.
 * 
 * @throws EOFException 
 *         If end of input file is reached before requested number of 
 *         bytes are transfered.
 *
 * @throws SocketTimeoutException 
 *         If this channel blocks transfer longer than timeout for 
 *         this stream.
 *          
 * @throws IOException Includes any exception thrown by 
 *         {@link FileChannel#transferTo(long, long, WritableByteChannel)}. 
 */
public void transferToFully(FileChannel fileCh, long position, int count) 
                            throws IOException {
  
  while (count > 0) {
    /* 
     * Ideally we should wait after transferTo returns 0. But because of
     * a bug in JRE on Linux (http://bugs.sun.com/view_bug.do?bug_id=5103988),
     * which throws an exception instead of returning 0, we wait for the
     * channel to be writable before writing to it. If you ever see 
     * IOException with message "Resource temporarily unavailable" 
     * thrown here, please let us know.
     * 
     * Once we move to JAVA SE 7, wait should be moved to correct place.
     */
    waitForWritable();
    int nTransfered = (int) fileCh.transferTo(position, count, getChannel());
    
    if (nTransfered == 0) {
      //check if end of file is reached.
      if (position >= fileCh.size()) {
        throw new EOFException("EOF Reached. file size is " + fileCh.size() + 
                               " and " + count + " more bytes left to be " +
                               "transfered.");
      }
      //otherwise assume the socket is full.
      //waitForWritable(); // see comment above.
    } else if (nTransfered < 0) {
      throw new IOException("Unexpected return of " + nTransfered + 
                            " from transferTo()");
    } else {
      position += nTransfered;
      count -= nTransfered;
    }
  }
}
 
Example 9
Source File: Benchmark.java    From joshua with Apache License 2.0 5 votes vote down vote up
public Benchmark(String dir) throws IOException {
  File file = new File(dir + "/slice_00000.source");
  this.fin = new FileInputStream(file);
  FileChannel source_channel = this.fin.getChannel();
  int byte_size = (int) source_channel.size();
  int int_size = byte_size / 4;

  byteBuffer = source_channel.map(MapMode.READ_ONLY, 0, byte_size);
  intBuffer = byteBuffer.asIntBuffer();

  intArray = new int[int_size];
  intBuffer.get(intArray);
}
 
Example 10
Source File: WindowsUserDefinedFileAttributeView.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int read(String name, ByteBuffer dst) throws IOException {
    if (System.getSecurityManager() != null)
        checkAccess(file.getPathForPermissionCheck(), true, false);

    // wrap with channel
    FileChannel fc = null;
    try {
        Set<OpenOption> opts = new HashSet<>();
        opts.add(READ);
        if (!followLinks)
            opts.add(WindowsChannelFactory.OPEN_REPARSE_POINT);
        fc = WindowsChannelFactory
            .newFileChannel(join(file, name), null, opts, 0L);
    } catch (WindowsException x) {
        x.rethrowAsIOException(join(file.getPathForPermissionCheck(), name));
    }

    // read to EOF (nothing we can do if I/O error occurs)
    try {
        if (fc.size() > dst.remaining())
            throw new IOException("Stream too large");
        int total = 0;
        while (dst.hasRemaining()) {
            int n = fc.read(dst);
            if (n < 0)
                break;
            total += n;
        }
        return total;
    } finally {
        fc.close();
    }
}
 
Example 11
Source File: BlockingWriterSenderImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void performTransfer(FileChannel source, IoCallback callback) {

        ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE);
        try {
            long pos = source.position();
            long size = source.size();
            while (size - pos > 0) {
                int ret = source.read(buffer);
                if (ret <= 0) {
                    break;
                }
                pos += ret;
                buffer.flip();
                if (!writeBuffer(buffer, callback)) {
                    return;
                }
                buffer.clear();
            }

            if (pos != size) {
                throw new EOFException("Unexpected EOF reading file");
            }

        } catch (IOException e) {
            callback.onException(exchange, this, e);
        }
        invokeOnComplete(callback);
    }
 
Example 12
Source File: WindowsUserDefinedFileAttributeView.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int read(String name, ByteBuffer dst) throws IOException {
    if (System.getSecurityManager() != null)
        checkAccess(file.getPathForPermissionCheck(), true, false);

    // wrap with channel
    FileChannel fc = null;
    try {
        Set<OpenOption> opts = new HashSet<>();
        opts.add(READ);
        if (!followLinks)
            opts.add(WindowsChannelFactory.OPEN_REPARSE_POINT);
        fc = WindowsChannelFactory
            .newFileChannel(join(file, name), null, opts, 0L);
    } catch (WindowsException x) {
        x.rethrowAsIOException(join(file.getPathForPermissionCheck(), name));
    }

    // read to EOF (nothing we can do if I/O error occurs)
    try {
        if (fc.size() > dst.remaining())
            throw new IOException("Stream too large");
        int total = 0;
        while (dst.hasRemaining()) {
            int n = fc.read(dst);
            if (n < 0)
                break;
            total += n;
        }
        return total;
    } finally {
        fc.close();
    }
}
 
Example 13
Source File: WindowsUserDefinedFileAttributeView.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int read(String name, ByteBuffer dst) throws IOException {
    if (System.getSecurityManager() != null)
        checkAccess(file.getPathForPermissionCheck(), true, false);

    // wrap with channel
    FileChannel fc = null;
    try {
        Set<OpenOption> opts = new HashSet<>();
        opts.add(READ);
        if (!followLinks)
            opts.add(WindowsChannelFactory.OPEN_REPARSE_POINT);
        fc = WindowsChannelFactory
            .newFileChannel(join(file, name), null, opts, 0L);
    } catch (WindowsException x) {
        x.rethrowAsIOException(join(file.getPathForPermissionCheck(), name));
    }

    // read to EOF (nothing we can do if I/O error occurs)
    try {
        if (fc.size() > dst.remaining())
            throw new IOException("Stream too large");
        int total = 0;
        while (dst.hasRemaining()) {
            int n = fc.read(dst);
            if (n < 0)
                break;
            total += n;
        }
        return total;
    } finally {
        fc.close();
    }
}
 
Example 14
Source File: MappedFile.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
public static MappedFile of(File file, int position, int capacity) throws IOException {
   final MappedByteBuffer buffer;
   final int length;
   final FileChannel channel = FileChannel.open(file.toPath(), StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.READ);
   length = (int) channel.size();
   if (length != capacity && length != 0) {
      channel.close();
      throw new IllegalStateException("the file is not " + capacity + " bytes long!");
   }
   buffer = channel.map(FileChannel.MapMode.READ_WRITE, position, capacity);
   return new MappedFile(channel, buffer, 0, length);
}
 
Example 15
Source File: WindowsUserDefinedFileAttributeView.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int read(String name, ByteBuffer dst) throws IOException {
    if (System.getSecurityManager() != null)
        checkAccess(file.getPathForPermissionCheck(), true, false);

    // wrap with channel
    FileChannel fc = null;
    try {
        Set<OpenOption> opts = new HashSet<>();
        opts.add(READ);
        if (!followLinks)
            opts.add(WindowsChannelFactory.OPEN_REPARSE_POINT);
        fc = WindowsChannelFactory
            .newFileChannel(join(file, name), null, opts, 0L);
    } catch (WindowsException x) {
        x.rethrowAsIOException(join(file.getPathForPermissionCheck(), name));
    }

    // read to EOF (nothing we can do if I/O error occurs)
    try {
        if (fc.size() > dst.remaining())
            throw new IOException("Stream too large");
        int total = 0;
        while (dst.hasRemaining()) {
            int n = fc.read(dst);
            if (n < 0)
                break;
            total += n;
        }
        return total;
    } finally {
        fc.close();
    }
}
 
Example 16
Source File: MCRFileChannel.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
private void updateMetadata() throws IOException {
    if (!write || !modified) {
        if (create) {
            MCRPathEventHelper.fireFileCreateEvent(path, file.getBasicFileAttributes());
        }
        return;
    }
    MessageDigest md5Digest = MCRMD5InputStream.buildMD5Digest();
    FileChannel md5Channel = (FileChannel) Files.newByteChannel(file.getLocalPath(), StandardOpenOption.READ);
    try {
        long position = 0, size = md5Channel.size();
        while (position < size) {
            long remainingSize = size - position;
            final ByteBuffer byteBuffer = md5Channel.map(MapMode.READ_ONLY, position,
                Math.min(remainingSize, Integer.MAX_VALUE));
            while (byteBuffer.hasRemaining()) {
                md5Digest.update(byteBuffer);
            }
            position += byteBuffer.limit();
        }
    } finally {
        if (md5Channel != baseChannel) {
            md5Channel.close();
        }
    }
    String md5 = MCRContentInputStream.getMD5String(md5Digest.digest());
    file.setMD5(md5);
    final MCRFileAttributes<String> basicFileAttributes = file.getBasicFileAttributes();
    if (create) {
        MCRPathEventHelper.fireFileCreateEvent(path, basicFileAttributes);
    } else {
        MCRPathEventHelper.fireFileUpdateEvent(path, basicFileAttributes);
    }
}
 
Example 17
Source File: LogTelnetHandler.java    From dubbox-hystrix with Apache License 2.0 4 votes vote down vote up
public String telnet(Channel channel, String message) {
    long size = 0 ;
    File file = LoggerFactory.getFile();
    StringBuffer buf = new StringBuffer();
    if (message == null || message.trim().length() == 0) {
        buf.append("EXAMPLE: log error / log 100");
    }else {
        String str[] = message.split(" ");
        if (! StringUtils.isInteger(str[0])){
            LoggerFactory.setLevel(Level.valueOf(message.toUpperCase()));
        } else {
            int SHOW_LOG_LENGTH = Integer.parseInt(str[0]);
            
            if (file != null && file.exists()) {
                try{
                    FileInputStream fis = new FileInputStream(file);
                    FileChannel filechannel = fis.getChannel();
                    size = filechannel.size();
                    ByteBuffer bb;
                    if (size <= SHOW_LOG_LENGTH) {
                        bb = ByteBuffer.allocate((int) size);
                        filechannel.read(bb, 0);
                    } else {
                        int pos = (int) (size - SHOW_LOG_LENGTH);
                        bb = ByteBuffer.allocate(SHOW_LOG_LENGTH);
                        filechannel.read(bb, pos);
                    }
                    bb.flip();
                    String content = new String(bb.array()).replace("<", "&lt;")
                    .replace(">", "&gt;").replace("\n", "<br/><br/>");
                    buf.append("\r\ncontent:"+content);
                    
                    buf.append("\r\nmodified:"+(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                    .format(new Date(file.lastModified()))));
                    buf.append("\r\nsize:"+size +"\r\n");
                }catch (Exception e) {
                    buf.append(e.getMessage());
                }
            }else {
                size = 0;
                buf.append("\r\nMESSAGE: log file not exists or log appender is console .");
            }
        }
    }
    buf.append("\r\nCURRENT LOG LEVEL:"+ LoggerFactory.getLevel())
    .append("\r\nCURRENT LOG APPENDER:"+ (file == null ? "console" : file.getAbsolutePath()));
    return buf.toString();
}
 
Example 18
Source File: AbstractFileIOChannel.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public long getSize() throws IOException {
	FileChannel channel = fileChannel;
	return channel == null ? 0 : channel.size();
}
 
Example 19
Source File: LocalFileSystemOperations.java    From ats-framework with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings( "resource" )
@Override
public void copyFile(
        String sourceFile,
        String destinationFile,
        boolean failOnError ) {

    File inputFile = new File(sourceFile);
    checkFileExistence(inputFile);

    FileChannel srcChannel = null;
    FileChannel dstChannel = null;

    try {
        // Create channel on the source
        srcChannel = new FileInputStream(sourceFile).getChannel();

        // Create channel on the destination
        dstChannel = new FileOutputStream(destinationFile).getChannel();

        // Copy file contents from source to destination
        dstChannel.truncate(0);

        if (log.isDebugEnabled()) {
            log.debug("Copying file '" + sourceFile + "' of " + srcChannel.size() + "bytes to '"
                      + destinationFile + "'");
        }

        /* Copy the file in chunks.
         * If we provide the whole file at once, the copy process does not start or does not
         * copy the whole file on some systems when the file is a very large one - usually
         * bigger than 2 GBs
         */
        final long CHUNK = 16 * 1024 * 1024; // 16 MB chunks
        for (long pos = 0; pos < srcChannel.size(); ) {
            pos += dstChannel.transferFrom(srcChannel, pos, CHUNK);
        }

        if (srcChannel.size() != dstChannel.size()) {
            throw new FileSystemOperationException("Size of the destination file \"" + destinationFile
                                                   + "\" and the source file \"" + sourceFile
                                                   + "\" mismatch!");
        }

        if (osType.isUnix()) {
            // set the original file permission to the new file
            setFilePermissions(destinationFile, getFilePermissions(sourceFile));
        }

    } catch (IOException e) {
        throw new FileSystemOperationException("Unable to copy file '" + sourceFile + "' to '"
                                               + destinationFile + "'", e);
    } finally {
        // Close the channels
        IoUtils.closeStream(srcChannel,
                            "Unable to close input channel while copying file '" + sourceFile + "' to '"
                            + destinationFile + "'");
        IoUtils.closeStream(dstChannel,
                            "Unable to close destination channel while copying file '" + sourceFile
                            + "' to '" + destinationFile + "'");
    }
}
 
Example 20
Source File: FileLoader.java    From twister2 with Apache License 2.0 4 votes vote down vote up
public static Triple<List<Tuple>, Long, Long> openFilePart(String fileName, long startOffSet,
                                                           int maxSize, MessageType keyType,
                                                           MessageType dataType,
                                                           KryoSerializer deserializer) {
  List<Tuple> keyValues = new ArrayList<>();
  String outFileName = Paths.get(fileName).toString();
  FileChannel rwChannel;
  try {
    rwChannel = new RandomAccessFile(outFileName, "rw").getChannel();
    long size = maxSize < rwChannel.size() - startOffSet
        ? maxSize : rwChannel.size() - startOffSet;
    ByteBuffer os = rwChannel.map(FileChannel.MapMode.READ_ONLY, startOffSet, size);

    long totalRead = 0;
    while (totalRead < size) {
      Object key;
      Object value;

      int keySize = os.getInt();
      key = keyType.getDataPacker().unpackFromBuffer(os, keySize);

      // we cannot read further
      if (totalRead + keySize > size) {
        break;
      }

      int dataSize = os.getInt();
      value = dataType.getDataPacker().unpackFromBuffer(os, dataSize);

      // we cannot read further
      if (totalRead + keySize + dataSize > size) {
        break;
      }

      keyValues.add(new Tuple(key, value));
      totalRead += 8 + keySize + dataSize;
    }
    rwChannel.close();
    return new ImmutableTriple<>(keyValues, totalRead + startOffSet, rwChannel.size());
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}