Compare commits

...

9 Commits

Author SHA1 Message Date
829790af29 Merge pull request 'v0.0.2' (#1) from dev into master
Reviewed-on: #1
2026-01-19 09:46:56 +00:00
82a48bd1d2 add CI workflow
All checks were successful
CI/CD / build-deploy (pull_request) Successful in 40s
2026-01-19 17:46:27 +08:00
d0d4e0e633 add toOutputStream(InputStream is, OutputStream os, long start, long end) 2026-01-05 01:51:58 +08:00
77979be5fe add IOSize.parse(String) 2025-10-24 11:58:46 +08:00
cd9eee61f3 add IO.md5(InputStream) 2025-10-24 11:58:28 +08:00
c0b4f41ea4 support deploy nexus 2025-10-13 10:51:11 +08:00
96ad0e2912 ignored CopilotChatHistory.xml 2025-10-13 10:51:03 +08:00
f9d7221e0f close stream for IO.resourceExist 2025-07-23 11:13:23 +08:00
3effab4def add IO.toStringLines 2025-07-23 11:11:18 +08:00
5 changed files with 269 additions and 28 deletions

111
.gitea/workflows/ci.yml Normal file
View File

@ -0,0 +1,111 @@
name: CI/CD
on:
pull_request:
branches:
- master
types:
- closed
jobs:
build-deploy:
runs-on: act_runner_java
if: ${{ github.event.pull_request.merged == true }}
env:
JAVA_HOME: /usr/lib/jvm/java-21-openjdk
steps:
- name: Checkout code
run: |
git clone ${{ github.server_url }}/${{ github.repository }}.git .
git checkout ${{ github.sha }}
- name: Set up environment
run: |
echo "PR #${{ github.event.number }} merged into master"
echo "Source branch: ${{ github.event.pull_request.head.ref }}"
echo "Target branch: ${{ github.event.pull_request.base.ref }}"
- name: Run tests
run: |
echo "Running test suite..."
- name: Build project
run: |
mvn -B -DskipTests clean package source:jar javadoc:jar
- name: Deploy to Nexus
if: success()
run: |
if [ -z "${{ secrets.NEXUS_USERNAME }}" ] || [ -z "${{ secrets.NEXUS_PASSWORD }}" ]; then
echo "Missing secrets.NEXUS_USERNAME or secrets.NEXUS_PASSWORD"
exit 1
fi
mkdir -p ~/.m2
cat > ~/.m2/settings.xml <<EOF
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<servers>
<server>
<id>timi-nexus</id>
<username>${{ secrets.NEXUS_USERNAME }}</username>
<password>${{ secrets.NEXUS_PASSWORD }}</password>
</server>
</servers>
</settings>
EOF
version=$(mvn -q -DforceStdout help:evaluate -Dexpression=project.version)
artifact_id=$(mvn -q -DforceStdout help:evaluate -Dexpression=project.artifactId)
main_jar="target/${artifact_id}-${version}.jar"
sources_jar="target/${artifact_id}-${version}-sources.jar"
javadoc_jar="target/${artifact_id}-${version}-javadoc.jar"
if [ ! -f "$main_jar" ] || [ ! -f "$sources_jar" ] || [ ! -f "$javadoc_jar" ]; then
echo "Missing build artifacts in target"
exit 1
fi
mvn -B deploy:deploy-file \
-Dfile="$main_jar" \
-Dsources="$sources_jar" \
-Djavadoc="$javadoc_jar" \
-DpomFile="./pom.xml" \
-Durl="https://nexus.imyeyu.com/repository/maven-releases/" \
-DrepositoryId="timi-nexus" \
-Dhttps.protocols=TLSv1.2 \
-Djdk.tls.client.protocols=TLSv1.2
- name: Create release
if: ${{ success() && startsWith(github.event.pull_request.title, 'v') }}
env:
GITEA_TOKEN: ${{ secrets.RUNNER_TOKEN }}
GITEA_SERVER_URL: ${{ github.server_url }}
GITEA_REPOSITORY: ${{ github.repository }}
RELEASE_TAG: ${{ github.event.pull_request.title }}
RELEASE_TARGET: ${{ github.sha }}
run: |
if [ -z "$GITEA_TOKEN" ]; then
echo "Missing secrets.RUNNER_TOKEN"
exit 1
fi
api_url="$GITEA_SERVER_URL/api/v1/repos/$GITEA_REPOSITORY/releases"
payload=$(cat <<EOF
{
"tag_name": "$RELEASE_TAG",
"name": "$RELEASE_TAG",
"target_commitish": "$RELEASE_TARGET",
"draft": false,
"prerelease": false
}
EOF
)
response=$(curl -sS -X POST "$api_url" \
-H "Authorization: token $GITEA_TOKEN" \
-H "Content-Type: application/json" \
-d "$payload")
release_id=$(echo "$response" | grep -o '"id":[0-9]*' | head -n 1 | grep -o '[0-9]*')
if [ -z "$release_id" ] || echo "$response" | grep -q '"message"'; then
echo "Create release failed: $response"
exit 1
fi
echo "Release created: id=$release_id"
for asset_path in target/*.jar; do
asset_name=$(basename "$asset_path")
curl -sS -X POST "$api_url/$release_id/assets?name=$asset_name" \
-H "Authorization: token $GITEA_TOKEN" \
-H "Content-Type: application/octet-stream" \
--data-binary @"$asset_path"
done

1
.gitignore vendored
View File

@ -8,6 +8,7 @@ target/
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
.idea/CopilotChatHistory.xml
*.iws
*.iml
*.ipr

43
pom.xml
View File

@ -6,7 +6,7 @@
<groupId>com.imyeyu.io</groupId>
<artifactId>timi-io</artifactId>
<version>0.0.1</version>
<version>0.0.2</version>
<packaging>jar</packaging>
<properties>
@ -20,22 +20,47 @@
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>21</source>
<target>21</target>
<encoding>UTF-8</encoding>
</configuration>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.1.3</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.3.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.11.2</version>
</plugin>
</plugins>
</build>
<distributionManagement>
<repository>
<id>timi_nexus</id>
<url>https://nexus.imyeyu.com/repository/maven-releases/</url>
</repository>
</distributionManagement>
<repositories>
<repository>
<id>timi_nexus</id>
<url>https://nexus.imyeyu.com/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.imyeyu.utils</groupId>
<artifactId>timi-utils</artifactId>
<version>0.0.1</version>
<version>0.0.2</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>

View File

@ -11,6 +11,7 @@ import com.imyeyu.utils.Text;
import javax.naming.NoPermissionException;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
@ -352,6 +353,19 @@ public class IO implements OS.FileSystem {
}
}
public static String[] toStringLines(File file) throws IOException {
return toString(file).split("\r\n|[\r\n]");
}
public static void toStringLines(File file, CallbackArg<String> lineCallback) throws IOException {
try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8))) {
String line;
while ((line = br.readLine()) != null) {
lineCallback.handler(line);
}
}
}
/**
* 读取文件为字符串UTF-8
*
@ -629,6 +643,24 @@ public class IO implements OS.FileSystem {
return !interrupt;
}
public static void toOutputStream(InputStream is, OutputStream os, long start, long end) throws IOException {
long skip = is.skip(start);
if (skip != start) {
throw new TimiException(TimiCode.ERROR, "skip fail");
}
byte[] buffer = new byte[8192];
long remaining = end - start + 1;
while (0 < remaining) {
int toRead = (int) Math.min(buffer.length, remaining);
int read = is.read(buffer, 0, toRead);
if (read == -1) {
break;
}
os.write(buffer, 0, read);
remaining -= read;
}
}
/**
* 写入字节到文件随机访问对象,此操作不会关闭流
*
@ -895,8 +927,13 @@ public class IO implements OS.FileSystem {
* @param path jar 内文件路径,不需要 / 开始,如 config/TimiJava.ini
* @return 数据流
*/
public static boolean resourceExist(Class<?> clazz, String path) {
return resourceToInputStream(clazz, path) != null;
public static boolean resourceExist(Class<?> clazz, String path) throws IOException {
InputStream stream = resourceToInputStream(clazz, path);
if (stream != null) {
stream.close();
return true;
}
return false;
}
/**
@ -1010,15 +1047,23 @@ public class IO implements OS.FileSystem {
*
* @param file 文件
* @return MD5
* @throws NoSuchAlgorithmException JDK 不支持此算法
*/
public static String md5(File file) throws NoSuchAlgorithmException {
try {
public static String md5(File file) throws FileNotFoundException {
return md5(getInputStream(file));
}
/**
* 计算输入流 MD5
*
* @param stream 输入流
* @return MD5
*/
public static String md5(InputStream stream) {
try (stream) {
MessageDigest md = MessageDigest.getInstance("MD5");
InputStream is = getInputStream(file);
byte[] buffer = new byte[8192];
int l;
while ((l = is.read(buffer)) != -1) {
while ((l = stream.read(buffer)) != -1) {
md.update(buffer, 0, l);
}
byte[] bytes = md.digest();
@ -1027,9 +1072,8 @@ public class IO implements OS.FileSystem {
chars[j++] = Text.HEX_DIGITS_LOWER[bytes[i] >>> 4 & 0xF];
chars[j++] = Text.HEX_DIGITS_LOWER[bytes[i] & 0xF];
}
is.close();
return new String(chars);
} catch (IOException e) {
} catch (IOException | NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}

View File

@ -1,5 +1,10 @@
package com.imyeyu.io;
import com.imyeyu.java.TimiJava;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 字节大小工具
*
@ -38,20 +43,13 @@ public class IOSize {
EB;
/**
* 转换指定单位字节量
* 转换字节量
*
* @param unit 单位
* @param value 值
* @return 该单位值字节量
*/
public static long value(Unit unit, double value) {
Unit[] values = values();
for (int i = 0; i < values.length; i++) {
if (values[i] == unit) {
return (long) (value * Math.pow(1024, i));
}
}
return (long) value;
public long toBytes(double value) {
return (long) (value * Math.pow(1024, ordinal()));
}
}
@ -165,4 +163,66 @@ public class IOSize {
}
return "0";
}
/**
* 将格式化的储存量字符串解析为字节量
* <p>支持格式10GB, 10 GB, 1TB, 1.24 KB 等(单位不区分大小写)</p>
* <pre>
* // 返回 102400
* IOSize.parse("100 KB");
* // 返回 1073741824
* IOSize.parse("1GB");
* </pre>
*
* @param sizeStr 格式化后的储存量字符串
* @return 字节量
* @throws IllegalArgumentException 格式无效
*/
public static long parse(String sizeStr) {
if (TimiJava.isEmpty(sizeStr)) {
throw new IllegalArgumentException("not found sizeStr");
}
// 正则匹配:可选符号 + 数字(含小数点)+ 可选空格 + 单位
Pattern pattern = Pattern.compile("([-+]?\\d+(?:\\.\\d+)?)\\s*([a-zA-Z]+)");
Matcher matcher = pattern.matcher(sizeStr.trim());
if (matcher.find()) {
double value;
try {
value = Double.parseDouble(matcher.group(1));
} catch (NumberFormatException e) {
throw new IllegalArgumentException("invalid number format: " + matcher.group(1), e);
}
if (value < 0) {
throw new IllegalArgumentException("size cannot be negative: " + value);
}
String unitStr = matcher.group(2).toUpperCase();
Unit unit;
try {
unit = Unit.valueOf(unitStr);
} catch (IllegalArgumentException e) {
// 处理单字母单位缩写K/M/G/T/P/E
unit = switch (unitStr.charAt(0)) {
case 'K' -> Unit.KB;
case 'M' -> Unit.MB;
case 'G' -> Unit.GB;
case 'T' -> Unit.TB;
case 'P' -> Unit.PB;
case 'E' -> Unit.EB;
case 'B' -> Unit.B;
default -> throw new IllegalArgumentException("Unknown unit: " + unitStr);
};
}
return unit.toBytes(value);
}
// 尝试解析纯数字(无单位,默认字节)
try {
long bytes = Long.parseLong(sizeStr.trim());
if (bytes < 0) {
throw new IllegalArgumentException("size cannot be negative: " + bytes);
}
return bytes;
} catch (NumberFormatException e) {
throw new IllegalArgumentException("invalid size format: " + sizeStr, e);
}
}
}