Skip to content

Commit

Permalink
🔨 refactor(1.2): 修改部分的sonar分析的bug
Browse files Browse the repository at this point in the history
refs #6

Signed-off-by: Tony Deng <[email protected]>
  • Loading branch information
tonydeng committed Sep 9, 2019
1 parent 47e58dc commit 149ea30
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 58 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
package com.github.tonydeng.fmj.handler;

import com.github.tonydeng.fmj.runner.BaseCommandOption;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.collect.Lists;
import lombok.Cleanup;
import lombok.extern.slf4j.Slf4j;

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

/**
* Created by tonydeng on 15/4/20.
*/
@Slf4j
public class DefaultCallbackHandler implements ProcessCallbackHandler {
private static final Logger log = LoggerFactory.getLogger(DefaultCallbackHandler.class);


@Override
public String handler(InputStream inputStream) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, BaseCommandOption.UTF8));
StringBuffer sb = new StringBuffer();
@Cleanup BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, BaseCommandOption.UTF8));
List<String> buffer = Lists.newArrayList();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
buffer.add(line);
}
return sb.toString();
return String.join("\n", buffer);
}
}
86 changes: 45 additions & 41 deletions src/main/java/com/github/tonydeng/fmj/runner/BaseCommandOption.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@

import com.github.tonydeng.fmj.model.VideoInfo;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.Collections;
import java.util.List;

/**
* Created by tonydeng on 15/4/15.
*/
@Slf4j
public class BaseCommandOption {
private static final Logger log = LoggerFactory.getLogger(BaseCommandOption.class);
private static boolean isWin = false;
private static boolean isLinux = false;
private static Integer DEFAULT_IO_THREADS = 1;
private static Integer defaultIoThreads = 1;

private static List<String> FFMPEG_BINARY;
private static List<String> FFPROBE_BINARY;
private static List<String> ffmpegBinary;
private static List<String> ffprobeBinary;

public static final String WINCMD = "cmd";
public static final String WINCMDOP = "/c";
Expand Down Expand Up @@ -56,57 +56,63 @@ public class BaseCommandOption {

static {
String env = System.getProperty("os.name");
if (log.isDebugEnabled())
log.debug("current operate system :{}", env);
log.debug("current operate system :{}", env);

if (null != env) {
if (StringUtils.isNotEmpty(env)) {
String os = env.toLowerCase();
if (os.indexOf("win") >= 0) {
if (os.contains("win")) {
isWin = true;
} else if (os.indexOf("linux") >= 0 || os.indexOf("mac") >= 0) {
} else if (os.contains("linux") || os.contains("mac")) {
isLinux = true;
}
}
//获得当前机器的CPU核数
DEFAULT_IO_THREADS = Runtime.getRuntime().availableProcessors();
defaultIoThreads = Runtime.getRuntime().availableProcessors();
if (log.isDebugEnabled()) {
log.debug("isWindows : '{}' or isLinux:'{}' DEFAULT_IO_THREADS:'{}'",
isWin, isLinux, DEFAULT_IO_THREADS);
log.debug("isWindows : '{}' or isLinux:'{}' defaultIoThreads:'{}'",
isWin, isLinux, defaultIoThreads);
}
}

private BaseCommandOption() {

}

/**
* 得到ffmpeg命令参数
*
* @return
*/
public static List<String> getFFmpegBinary() {
if (FFMPEG_BINARY == null) {
if (ffmpegBinary == null) {
if (isWin) {
FFMPEG_BINARY = Lists.newArrayList(WINCMD, WINCMDOP, FFMPEG);
ffmpegBinary = Lists.newArrayList(WINCMD, WINCMDOP, FFMPEG);
} else if (isLinux) {
FFMPEG_BINARY = Lists.newArrayList(LINUXCMD, FFMPEG);
ffmpegBinary = Lists.newArrayList(LINUXCMD, FFMPEG);
}
}
return FFMPEG_BINARY;
return ffmpegBinary;
}

/**
* 得到ffprobe命令
*
* @return
*/
public static List<String> getFFprobeBinary() {
if (null == FFPROBE_BINARY) {
if (null == ffprobeBinary) {
if (isWin) {
FFPROBE_BINARY = Lists.newArrayList(WINCMD, WINCMDOP, FFPROBE);
ffprobeBinary = Lists.newArrayList(WINCMD, WINCMDOP, FFPROBE);
} else if (isLinux) {
FFPROBE_BINARY = Lists.newArrayList(LINUXCMD, FFPROBE);
ffprobeBinary = Lists.newArrayList(LINUXCMD, FFPROBE);
}
}
return FFPROBE_BINARY;
return ffprobeBinary;
}

/**
* 视频输入的命令参数
*
* @param input
* @return
*/
Expand All @@ -118,6 +124,7 @@ public static List<String> toInputCommonsCmdArrays(String input) {

/**
* 截图的命令参数
*
* @param input
* @param output
* @param shotSecond
Expand All @@ -127,7 +134,7 @@ public static List<String> toInputCommonsCmdArrays(String input) {
public static List<String> toScreenshotCmdArrays(String input, String output, int shotSecond, VideoInfo vi) {
if (vi != null && vi.getSize() > 0) {
List<String> commands = Lists.newArrayList();
if (vi.getDuration() < Long.valueOf(shotSecond).longValue()) {
if (vi.getDuration() < shotSecond) {
shotSecond = 1;
}
commands.add(SS);
Expand All @@ -147,11 +154,12 @@ public static List<String> toScreenshotCmdArrays(String input, String output, in
commands.add(output);
return commands;
}
return Collections.EMPTY_LIST;
return Collections.emptyList();
}

/**
* 转成HLS的命令参数
*
* @param input
* @param m3u8Output
* @param cutSecond
Expand All @@ -168,7 +176,7 @@ public static List<String> toHLSCmdArrays(String input, String m3u8Output, int c
CA, FORMAT_ACC,
STRICT, "-2",
F, FORMAT_HLS,
THREADS,DEFAULT_IO_THREADS.toString(),
THREADS, defaultIoThreads.toString(),
HLS_TIME, String.valueOf(cutSecond),
HLS_LIST_SIZE, "0",
HLS_WRAP, "0",
Expand All @@ -178,11 +186,12 @@ public static List<String> toHLSCmdArrays(String input, String m3u8Output, int c

return commands;
}
return Collections.EMPTY_LIST;
return Collections.emptyList();
}

/**
* 转码到mp4的命令参数
*
* @param input
* @param output
* @param vi
Expand All @@ -196,33 +205,28 @@ public static List<String> toMP4CmdArrays(String input, String output, VideoInfo
CV, FORMAT_LIB264,
CA, FORMAT_ACC,
STRICT, "-2",
THREADS, DEFAULT_IO_THREADS.toString(),
THREADS, defaultIoThreads.toString(),
output
));
return commands;
}
return Collections.EMPTY_LIST;
return Collections.emptyList();
}

/**
* 获取视频转向的参数
*
* @param vi
* @return
*/
public static List<String> getRoateCmdArrays(VideoInfo vi){
if (vi != null && vi.getSize() > 0) {
if (vi.getRotate() > 0) {
//-vf "transpose=1" -c:a copy
return Lists.newArrayList(
VF,"transpose=1"
public static List<String> getRoateCmdArrays(VideoInfo vi) {
if (vi != null && vi.getSize() > 0 && vi.getRotate() > 0) {
//-vf "transpose=1" -c:a copy
return Lists.newArrayList(
VF, "transpose=1"
// CA,COPY
);
// commands.add(VF);
// commands.add("transpose=1");
// commands.add(CA);
// commands.add(COPY);
}
);
}
return Collections.EMPTY_LIST;
return Collections.emptyList();
}
}
29 changes: 20 additions & 9 deletions src/main/java/com/github/tonydeng/fmj/utils/EncriptUtils.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package com.github.tonydeng.fmj.utils;

import lombok.extern.slf4j.Slf4j;

import java.security.MessageDigest;

/**
* Created by tonydeng on 15/4/17.
*/
@Slf4j
public class EncriptUtils {
//十六进制下数字到字符的映射数组
private final static String[] hexDigits = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"};
private static final String[] HEX_DIGITS = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"};

private EncriptUtils() {

}

/**
* 把inputString加密
Expand All @@ -27,10 +34,9 @@ private static String encodeByMD5(String originString) {
//使用指定的字节数组对摘要进行最后更新,然后完成摘要计算
byte[] results = md5.digest(originString.getBytes());
//将得到的字节数组变成字符串返回
String result = byteArrayToHexString(results);
return result;
return byteArrayToHexString(results);
} catch (Exception e) {
e.printStackTrace();
log.error("encode md5 error", e);
}
}
return null;
Expand All @@ -43,20 +49,25 @@ private static String encodeByMD5(String originString) {
* @return 十六进制字符串
*/
private static String byteArrayToHexString(byte[] b) {
StringBuffer resultSb = new StringBuffer();
StringBuilder builder = new StringBuilder();
for (int i = 0; i < b.length; i++) {
resultSb.append(byteToHexString(b[i]));
builder.append(byteToHexString(b[i]));
}
return resultSb.toString();
return builder.toString();
}

//将一个字节转化成十六进制形式的字符串
/**
* 将一个字节转化成十六进制形式的字符串
*
* @param b
* @return
*/
private static String byteToHexString(byte b) {
int n = b;
if (n < 0)
n = 256 + n;
int d1 = n / 16;
int d2 = n % 16;
return hexDigits[d1] + hexDigits[d2];
return HEX_DIGITS[d1] + HEX_DIGITS[d2];
}
}
22 changes: 22 additions & 0 deletions src/test/java/com/github/tonydeng/fmj/utils/EncriptUtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.github.tonydeng.fmj.utils;

import com.github.tonydeng.fmj.BaseTest;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.Test;

/**
* @author Tony Deng
* @version V1.0
* @date 2019-09-10 00:28
**/
@Slf4j
public class EncriptUtilsTest extends BaseTest {

private static final String INPUT = "12345";

@Test
public void testMd5() {
Assert.assertEquals("827CCB0EEA8A706C4C34A16891F84E7B",EncriptUtils.md5(INPUT));
}
}

0 comments on commit 149ea30

Please sign in to comment.