com.blankj.utilcode.constant.MemoryConstants Java Examples

The following examples show how to use com.blankj.utilcode.constant.MemoryConstants. 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: ConvertUtils.java    From Android-UtilCode with Apache License 2.0 6 votes vote down vote up
/**
 * inputStream转outputStream
 *
 * @param is 输入流
 * @return outputStream子类
 */
public static ByteArrayOutputStream input2OutputStream(InputStream is) {
    if (is == null) return null;
    try {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        byte[] b = new byte[MemoryConstants.KB];
        int len;
        while ((len = is.read(b, 0, MemoryConstants.KB)) != -1) {
            os.write(b, 0, len);
        }
        return os;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    } finally {
        CloseUtils.closeIO(is);
    }
}
 
Example #2
Source File: ConvertUtils.java    From AndroidUtilCode with Apache License 2.0 6 votes vote down vote up
/**
 * Size of byte to fit size of memory.
 * <p>to three decimal places</p>
 *
 * @param byteSize  Size of byte.
 * @param precision The precision
 * @return fit size of memory
 */
@SuppressLint("DefaultLocale")
public static String byte2FitMemorySize(final long byteSize, int precision) {
    if (precision < 0) {
        throw new IllegalArgumentException("precision shouldn't be less than zero!");
    }
    if (byteSize < 0) {
        throw new IllegalArgumentException("byteSize shouldn't be less than zero!");
    } else if (byteSize < MemoryConstants.KB) {
        return String.format("%." + precision + "fB", (double) byteSize);
    } else if (byteSize < MemoryConstants.MB) {
        return String.format("%." + precision + "fKB", (double) byteSize / MemoryConstants.KB);
    } else if (byteSize < MemoryConstants.GB) {
        return String.format("%." + precision + "fMB", (double) byteSize / MemoryConstants.MB);
    } else {
        return String.format("%." + precision + "fGB", (double) byteSize / MemoryConstants.GB);
    }
}
 
Example #3
Source File: DokitGlideTransform.java    From DoraemonKit with Apache License 2.0 5 votes vote down vote up
@NonNull
@Override
public Resource<Bitmap> transform(@NonNull Context context, @NonNull Resource<Bitmap> resource, int outWidth, int outHeight) {
    try {
        if (mWrap != null) {
            resource = mWrap.transform(context, resource, outWidth, outHeight);
        }

        if (PerformanceSpInfoConfig.isLargeImgOpen()) {
            String url = "";
            if (mRequestBuilder instanceof RequestBuilder) {
                if (ReflectUtils.reflect(mRequestBuilder).field("model").get() instanceof String) {
                    url = ReflectUtils.reflect(mRequestBuilder).field("model").get();
                } else if (ReflectUtils.reflect(mRequestBuilder).field("model").get() instanceof Integer) {
                    url = "" + ReflectUtils.reflect(mRequestBuilder).field("model").get();
                }
            }
            Bitmap bitmap = resource.get();
            double imgSize = ConvertUtils.byte2MemorySize(bitmap.getByteCount(), MemoryConstants.MB);
            LargePictureManager.getInstance().saveImageInfo(url, imgSize, bitmap.getWidth(), bitmap.getHeight(), "Glide");
        }
    } catch (Exception e) {
        if (mWrap != null) {
            resource = mWrap.transform(context, resource, outWidth, outHeight);
        }
    }

    return resource;
}
 
Example #4
Source File: DokitFrescoPostprocessor.java    From DoraemonKit with Apache License 2.0 5 votes vote down vote up
@Override
public CloseableReference<Bitmap> process(Bitmap sourceBitmap, PlatformBitmapFactory bitmapFactory) {
    try {
        if (PerformanceSpInfoConfig.isLargeImgOpen()) {
            double imgSize = ConvertUtils.byte2MemorySize(sourceBitmap.getByteCount(), MemoryConstants.MB);
            LargePictureManager.getInstance().saveImageInfo(mUri.toString(), imgSize, sourceBitmap.getWidth(), sourceBitmap.getHeight(), "Fresco");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    if (mOriginalPostprocessor != null) {
        return mOriginalPostprocessor.process(sourceBitmap, bitmapFactory);
    }

    final Bitmap.Config sourceBitmapConfig = sourceBitmap.getConfig();
    CloseableReference<Bitmap> destBitmapRef =
            bitmapFactory.createBitmapInternal(
                    sourceBitmap.getWidth(),
                    sourceBitmap.getHeight(),
                    sourceBitmapConfig != null ? sourceBitmapConfig : FALLBACK_BITMAP_CONFIGURATION);
    try {
        process(destBitmapRef.get(), sourceBitmap);
        return CloseableReference.cloneOrNull(destBitmapRef);
    } finally {
        CloseableReference.closeSafely(destBitmapRef);
    }

}
 
Example #5
Source File: DokitImageLoadingListener.java    From DoraemonKit with Apache License 2.0 5 votes vote down vote up
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
    try {
        if (PerformanceSpInfoConfig.isLargeImgOpen()) {
            double imgSize = ConvertUtils.byte2MemorySize(loadedImage.getByteCount(), MemoryConstants.MB);
            LargePictureManager.getInstance().saveImageInfo(imageUri, imgSize, loadedImage.getWidth(), loadedImage.getHeight(), "ImageLoader");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (mOriginalImageLoadingListener != null) {
        mOriginalImageLoadingListener.onLoadingComplete(imageUri, view, loadedImage);
    }
}
 
Example #6
Source File: FileUtils.java    From Android-UtilCode with Apache License 2.0 5 votes vote down vote up
/**
 * 字节数转合适内存大小
 * <p>保留3位小数</p>
 *
 * @param byteNum 字节数
 * @return 合适内存大小
 */
@SuppressLint("DefaultLocale")
private static String byte2FitMemorySize(long byteNum) {
    if (byteNum < 0) {
        return "shouldn't be less than zero!";
    } else if (byteNum < MemoryConstants.KB) {
        return String.format("%.3fB", (double) byteNum + 0.0005);
    } else if (byteNum < MemoryConstants.MB) {
        return String.format("%.3fKB", (double) byteNum / MemoryConstants.KB + 0.0005);
    } else if (byteNum < MemoryConstants.GB) {
        return String.format("%.3fMB", (double) byteNum / MemoryConstants.MB + 0.0005);
    } else {
        return String.format("%.3fGB", (double) byteNum / MemoryConstants.GB + 0.0005);
    }
}
 
Example #7
Source File: ConvertUtils.java    From Android-UtilCode with Apache License 2.0 5 votes vote down vote up
/**
 * 字节数转合适内存大小
 * <p>保留3位小数</p>
 *
 * @param byteNum 字节数
 * @return 合适内存大小
 */
@SuppressLint("DefaultLocale")
public static String byte2FitMemorySize(long byteNum) {
    if (byteNum < 0) {
        return "shouldn't be less than zero!";
    } else if (byteNum < MemoryConstants.KB) {
        return String.format("%.3fB", (double) byteNum + 0.0005);
    } else if (byteNum < MemoryConstants.MB) {
        return String.format("%.3fKB", (double) byteNum / MemoryConstants.KB + 0.0005);
    } else if (byteNum < MemoryConstants.GB) {
        return String.format("%.3fMB", (double) byteNum / MemoryConstants.MB + 0.0005);
    } else {
        return String.format("%.3fGB", (double) byteNum / MemoryConstants.GB + 0.0005);
    }
}
 
Example #8
Source File: ConvertUtilsTest.java    From AndroidUtilCode with Apache License 2.0 5 votes vote down vote up
@Test
public void byte2MemorySize() {
    assertEquals(
            1024,
            ConvertUtils.byte2MemorySize(MemoryConstants.GB, MemoryConstants.MB),
            0.001
    );
}
 
Example #9
Source File: ConvertUtilsTest.java    From Android-UtilCode with Apache License 2.0 4 votes vote down vote up
@Test
public void testByte2Unit() throws Exception {
    assertThat(byte2MemorySize(MemoryConstants.GB, MemoryConstants.MB) - 1024).isWithin(0.001);
}
 
Example #10
Source File: ConvertUtils.java    From Android-UtilCode with Apache License 2.0 2 votes vote down vote up
/**
 * 以unit为单位的内存大小转字节数
 *
 * @param memorySize 大小
 * @param unit       单位类型
 *                   <ul>
 *                   <li>{@link MemoryConstants#BYTE}: 字节</li>
 *                   <li>{@link MemoryConstants#KB}  : 千字节</li>
 *                   <li>{@link MemoryConstants#MB}  : 兆</li>
 *                   <li>{@link MemoryConstants#GB}  : GB</li>
 *                   </ul>
 * @return 字节数
 */
public static long memorySize2Byte(long memorySize, @MemoryConstants.Unit int unit) {
    if (memorySize < 0) return -1;
    return memorySize * unit;
}
 
Example #11
Source File: ConvertUtils.java    From Android-UtilCode with Apache License 2.0 2 votes vote down vote up
/**
 * 字节数转以unit为单位的内存大小
 *
 * @param byteNum 字节数
 * @param unit    单位类型
 *                <ul>
 *                <li>{@link MemoryConstants#BYTE}: 字节</li>
 *                <li>{@link MemoryConstants#KB}  : 千字节</li>
 *                <li>{@link MemoryConstants#MB}  : 兆</li>
 *                <li>{@link MemoryConstants#GB}  : GB</li>
 *                </ul>
 * @return 以unit为单位的size
 */
public static double byte2MemorySize(long byteNum, @MemoryConstants.Unit int unit) {
    if (byteNum < 0) return -1;
    return (double) byteNum / unit;
}
 
Example #12
Source File: ConvertUtils.java    From AndroidUtilCode with Apache License 2.0 2 votes vote down vote up
/**
 * Size of memory in unit to size of byte.
 *
 * @param memorySize Size of memory.
 * @param unit       The unit of memory size.
 *                   <ul>
 *                   <li>{@link MemoryConstants#BYTE}</li>
 *                   <li>{@link MemoryConstants#KB}</li>
 *                   <li>{@link MemoryConstants#MB}</li>
 *                   <li>{@link MemoryConstants#GB}</li>
 *                   </ul>
 * @return size of byte
 */
public static long memorySize2Byte(final long memorySize,
                                   @MemoryConstants.Unit final int unit) {
    if (memorySize < 0) return -1;
    return memorySize * unit;
}
 
Example #13
Source File: ConvertUtils.java    From AndroidUtilCode with Apache License 2.0 2 votes vote down vote up
/**
 * Size of byte to size of memory in unit.
 *
 * @param byteSize Size of byte.
 * @param unit     The unit of memory size.
 *                 <ul>
 *                 <li>{@link MemoryConstants#BYTE}</li>
 *                 <li>{@link MemoryConstants#KB}</li>
 *                 <li>{@link MemoryConstants#MB}</li>
 *                 <li>{@link MemoryConstants#GB}</li>
 *                 </ul>
 * @return size of memory in unit
 */
public static double byte2MemorySize(final long byteSize,
                                     @MemoryConstants.Unit final int unit) {
    if (byteSize < 0) return -1;
    return (double) byteSize / unit;
}