[Add] PixivUgoiraBuilder 增加方法'buildUgoira(OutputStream, boolean)'以提供输出流给Builder输出已构建的动图数据;

[Add] PixivUgoiraBuilder 增加方法'getUgoiraMeta():JsonObject'方法, 可通过该方法获取动图元数据;
This commit is contained in:
2020-06-08 15:30:22 +08:00
parent d1aeda012e
commit d4d3432c76
2 changed files with 28 additions and 2 deletions

View File

@ -82,7 +82,33 @@ public final class PixivUgoiraBuilder {
log.debug("IllustId: {}, UgoiraMeta: {}", this.illustId, this.ugoiraMeta); log.debug("IllustId: {}, UgoiraMeta: {}", this.illustId, this.ugoiraMeta);
} }
/**
* 获取动图元数据
* @return 动图元数据, 返回的对象不影响Builder中的meta对象
*/
public JsonObject getUgoiraMeta() {
return this.ugoiraMeta.deepCopy();
}
/**
* 构建动图
* @param original 是否为原图画质
* @return 返回动图数据输入流
* @throws IOException 当获取数据发生异常时抛出
*/
public InputStream buildUgoira(boolean original) throws IOException { public InputStream buildUgoira(boolean original) throws IOException {
ByteArrayOutputStream bufferOutput = new ByteArrayOutputStream();
buildUgoira(bufferOutput, original);
return new ByteArrayInputStream(bufferOutput.toByteArray());
}
/**
* 构建动图
* @param outputStream 动图输出流
* @param original 是否为原图画质
* @throws IOException 当获取数据发生异常时抛出
*/
public void buildUgoira(OutputStream outputStream, boolean original) throws IOException {
getUgoiraImageSize(); getUgoiraImageSize();
log.debug("动图尺寸信息: Height: {}, Width: {}", height, width); log.debug("动图尺寸信息: Height: {}, Width: {}", height, width);
@ -95,7 +121,6 @@ public final class PixivUgoiraBuilder {
HttpResponse response = httpClient.execute(request); HttpResponse response = httpClient.execute(request);
log.trace("请求已发送, 正在处理响应..."); log.trace("请求已发送, 正在处理响应...");
ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(response.getEntity().getContent(), 64 * 1024)); ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(response.getEntity().getContent(), 64 * 1024));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ZipEntry entry; ZipEntry entry;
ByteArrayOutputStream cacheOutputStream = new ByteArrayOutputStream(512); ByteArrayOutputStream cacheOutputStream = new ByteArrayOutputStream(512);
HashMap<String, InputStream> frameMap = new HashMap<>(frames.size()); HashMap<String, InputStream> frameMap = new HashMap<>(frames.size());
@ -140,7 +165,6 @@ public final class PixivUgoiraBuilder {
} }
}); });
encoder.finishEncoding(); encoder.finishEncoding();
return new ByteArrayInputStream(outputStream.toByteArray());
} }
/** /**

View File

@ -6,6 +6,7 @@ import org.apache.http.impl.client.HttpClientBuilder;
import org.bouncycastle.util.io.Streams; import org.bouncycastle.util.io.Streams;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.slf4j.LoggerFactory;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -20,6 +21,7 @@ public class PixivUgoiraBuilderTest {
File outputFile = new File("./output2.gif"); File outputFile = new File("./output2.gif");
CloseableHttpClient httpClient = HttpClientBuilder.create().setProxy(new HttpHost("127.0.0.1", 1001)).build(); CloseableHttpClient httpClient = HttpClientBuilder.create().setProxy(new HttpHost("127.0.0.1", 1001)).build();
PixivUgoiraBuilder builder = new PixivUgoiraBuilder(httpClient, 80766493); PixivUgoiraBuilder builder = new PixivUgoiraBuilder(httpClient, 80766493);
LoggerFactory.getLogger(PixivUgoiraBuilderTest.class).info("UgoiraMeta: {}", builder.getUgoiraMeta());
InputStream inputStream = builder.buildUgoira(true); InputStream inputStream = builder.buildUgoira(true);
Files.write(outputFile.toPath(), Streams.readAll(inputStream)); Files.write(outputFile.toPath(), Streams.readAll(inputStream));
} }