栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

2021SC@SDUSC-multimedia-utils-一款java后端的图片、视频处理工具jar包

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

2021SC@SDUSC-multimedia-utils-一款java后端的图片、视频处理工具jar包

2021SC@SDUSC 

目录

项目名称:multimedia-utils

RuntimeUtils


项目名称:multimedia-utils

博客十一

在上篇博客我们介绍的是json文件。主要讲的在这个项目里,为了我们更方便的储存临时数据和对数据进行提取,我们用json文件对相应的的数据进行提取。在下面我们将介绍runtimeutils。

RuntimeUtils

在每一个java进程之中都会存在有一个Runtime类的对象。由于此类的对象是由Java进程自己维护,所以在整个Runtime类设计的过程之中,只为用户提供了唯一的一个实例化对象。所以这个类所使用的就是单例设计模式,构造方法被私有化了。所以其类的内部一定会提供有一个static方法取得本类的实例化对象。

取得Runtime类对象:public static Runtime getRuntime()
那么取得对象之后可以做什么呢?在Runtime类中定义有如下可以取得内存大小的方法。

    最大可用内存大小:public long maxMemory()
    总共可用内存大小:public long totalMemory()
    空闲内存大小:public long freeMemory()

下面我们来给出相关代码段。

package com.whty.zdxt.multimedia.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;


class RuntimeUtils {
    private static final Log log = LogFactory.getLog(RuntimeUtils.class);

    
    public static void execSF(String command) {
        log.debug("执行命令:" + command);
        Process exec = null;
        try {
            exec = Runtime.getRuntime().exec(command);
            exec.waitFor();
            RuntimeUtils.successful(exec);
            String failure = RuntimeUtils.failure(exec);
            int i = exec.exitValue();
            if (i != 0) {
                throw new RuntimeException(failure);
            }
        } catch (IOException | InterruptedException e) {
            throw new RuntimeException("执行命令失败", e);
        } finally {
            if (exec != null) {
                exec.destroy();
            }
        }
    }

    
    public static void execFailure(String command) {
        log.debug("执行命令:" + command);
        Process exec = null;
        try {
            exec = Runtime.getRuntime().exec(command);
            exec.waitFor();
            String failure = RuntimeUtils.failure(exec);
            int i = exec.exitValue();
            if (i != 0) {
                throw new RuntimeException(failure);
            }
        } catch (IOException | InterruptedException e) {
            throw new RuntimeException("执行命令失败", e);
        } finally {
            if (exec != null) {
                exec.destroy();
            }
        }
    }

    
    public static String execSuccessful(String command) {
        log.debug("执行命令:" + command);
        Process exec = null;
        String response = null;
        try {
            exec = Runtime.getRuntime().exec(command);
            response = RuntimeUtils.successful(exec);
        } catch (IOException e) {
            throw new RuntimeException("执行命令失败", e);
        } finally {
            if (exec != null) {
                exec.destroy();
            }
        }
        return response;
    }

    
    private static String successful(Process exec) throws IOException {
        InputStream is = exec.getInputStream();
        return response(is);
    }

    
    private static String failure(Process exec) throws IOException {
        InputStream is = exec.getErrorStream();
        return response(is);
    }

    
    private static String response(InputStream is) throws IOException {
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String line = null;
        String r = "";
        while ((line = br.readLine()) != null) {
//            System.out.println(line);
            r = r + line;
        }
        return r;
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/658723.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号