Java Code Examples for com.helger.commons.CGlobal#BYTES_PER_MEGABYTE

The following examples show how to use com.helger.commons.CGlobal#BYTES_PER_MEGABYTE . 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: FileChannelHelper.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Nullable
public static InputStream getInputStream (@Nonnull final File aFile)
{
  ValueEnforcer.notNull (aFile, "File");

  final FileInputStream aFIS = FileHelper.getInputStream (aFile);
  if (aFIS != null)
  {
    // Check if using a memory mapped file makes sense (file size > 1MB)
    final FileChannel aChannel = aFIS.getChannel ();
    if (getFileSize (aChannel) > CGlobal.BYTES_PER_MEGABYTE)
    {
      // Check if mapping is possible
      final InputStream aIS = _getMappedInputStream (aChannel, aFile);
      if (aIS != null)
        return aIS;

      // Mapping failed - fall through
    }
  }
  return aFIS;
}
 
Example 2
Source File: SizeHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
public String getAsMatching (final long nSize)
{
  if (nSize >= CGlobal.BYTES_PER_PETABYTE)
    return getAsPB (nSize);
  if (nSize >= CGlobal.BYTES_PER_TERABYTE)
    return getAsTB (nSize);
  if (nSize >= CGlobal.BYTES_PER_GIGABYTE)
    return getAsGB (nSize);
  if (nSize >= CGlobal.BYTES_PER_MEGABYTE)
    return getAsMB (nSize);
  if (nSize >= CGlobal.BYTES_PER_KILOBYTE)
    return getAsKB (nSize);
  return _format (nSize) + B_SUFFIX;
}
 
Example 3
Source File: SizeHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
public String getAsMatching (final long nSize, @Nonnegative final int nDecimals)
{
  if (nSize >= CGlobal.BYTES_PER_PETABYTE)
    return getAsPB (nSize, nDecimals);
  if (nSize >= CGlobal.BYTES_PER_TERABYTE)
    return getAsTB (nSize, nDecimals);
  if (nSize >= CGlobal.BYTES_PER_GIGABYTE)
    return getAsGB (nSize, nDecimals);
  if (nSize >= CGlobal.BYTES_PER_MEGABYTE)
    return getAsMB (nSize, nDecimals);
  if (nSize >= CGlobal.BYTES_PER_KILOBYTE)
    return getAsKB (nSize, nDecimals);
  return _format (nSize, nDecimals) + B_SUFFIX;
}
 
Example 4
Source File: FileOperations.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
/**
 * Copy the content of the source file to the destination file using
 * {@link FileChannel}. This version seems to fail with UNC paths.
 *
 * @param aSrcFile
 *        Source file. May not be <code>null</code>.
 * @param aDestFile
 *        Destination file. May not be <code>null</code>.
 * @return {@link ESuccess}
 */
@Nonnull
private static ESuccess _copyFileViaChannel (@Nonnull final File aSrcFile, @Nonnull final File aDestFile)
{
  final FileChannel aSrcChannel = FileChannelHelper.getFileReadChannel (aSrcFile);
  if (aSrcChannel == null)
    return ESuccess.FAILURE;

  try
  {
    final FileChannel aDstChannel = FileChannelHelper.getFileWriteChannel (aDestFile, EAppend.TRUNCATE);
    if (aDstChannel == null)
      return ESuccess.FAILURE;

    try
    {
      FileLock aSrcLock = null;
      FileLock aDestLock = null;
      try
      {
        final long nBytesToRead = aSrcChannel.size ();

        // Shared read lock and exclusive write lock
        aSrcLock = aSrcChannel.lock (0, nBytesToRead, true);
        aDestLock = aDstChannel.lock ();

        // Main copying - the loop version is much quicker than then
        // transferTo with full size!
        long nBytesWritten = 0;
        final long nChunkSize = 1L * CGlobal.BYTES_PER_MEGABYTE;
        while (nBytesWritten < nBytesToRead)
          nBytesWritten += aSrcChannel.transferTo (nBytesWritten, nChunkSize, aDstChannel);

        if (nBytesToRead != nBytesWritten)
        {
          if (LOGGER.isErrorEnabled ())
            LOGGER.error ("Failed to copy file. Meant to read " + nBytesToRead + " bytes but wrote " + nBytesWritten);
          return ESuccess.FAILURE;
        }
        return ESuccess.SUCCESS;
      }
      catch (final IOException ex)
      {
        throw new IllegalStateException ("Failed to copy from " + aSrcFile + " to " + aDestFile, ex);
      }
      finally
      {
        // Unlock
        ChannelHelper.release (aDestLock);
        ChannelHelper.release (aSrcLock);
      }
    }
    finally
    {
      ChannelHelper.close (aDstChannel);
    }
  }
  finally
  {
    ChannelHelper.close (aSrcChannel);
  }
}