commit
dfee56ad9d
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012-2023 the original author or authors.
|
* Copyright 2012-2024 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -19,7 +19,6 @@ package org.springframework.boot.loader.zip;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A virtual {@link DataBlock} build from a collection of other {@link DataBlock}
|
* A virtual {@link DataBlock} build from a collection of other {@link DataBlock}
|
||||||
|
@ -29,10 +28,14 @@ import java.util.List;
|
||||||
*/
|
*/
|
||||||
class VirtualDataBlock implements DataBlock {
|
class VirtualDataBlock implements DataBlock {
|
||||||
|
|
||||||
private List<DataBlock> parts;
|
private DataBlock[] parts;
|
||||||
|
|
||||||
|
private long[] offsets;
|
||||||
|
|
||||||
private long size;
|
private long size;
|
||||||
|
|
||||||
|
private volatile int lastReadPart = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new {@link VirtualDataBlock} instance. The {@link #setParts(Collection)}
|
* Create a new {@link VirtualDataBlock} instance. The {@link #setParts(Collection)}
|
||||||
* method must be called before the data block can be used.
|
* method must be called before the data block can be used.
|
||||||
|
@ -55,12 +58,16 @@ class VirtualDataBlock implements DataBlock {
|
||||||
* @throws IOException on I/O error
|
* @throws IOException on I/O error
|
||||||
*/
|
*/
|
||||||
protected void setParts(Collection<? extends DataBlock> parts) throws IOException {
|
protected void setParts(Collection<? extends DataBlock> parts) throws IOException {
|
||||||
this.parts = List.copyOf(parts);
|
this.parts = parts.toArray(DataBlock[]::new);
|
||||||
|
this.offsets = new long[parts.size()];
|
||||||
long size = 0;
|
long size = 0;
|
||||||
|
int i = 0;
|
||||||
for (DataBlock part : parts) {
|
for (DataBlock part : parts) {
|
||||||
|
this.offsets[i++] = size;
|
||||||
size += part.size();
|
size += part.size();
|
||||||
}
|
}
|
||||||
this.size = size;
|
this.size = size;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -73,20 +80,30 @@ class VirtualDataBlock implements DataBlock {
|
||||||
if (pos < 0 || pos >= this.size) {
|
if (pos < 0 || pos >= this.size) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
int lastReadPart = this.lastReadPart;
|
||||||
|
int partIndex = 0;
|
||||||
long offset = 0;
|
long offset = 0;
|
||||||
int result = 0;
|
int result = 0;
|
||||||
for (DataBlock part : this.parts) {
|
if (pos >= this.offsets[lastReadPart]) {
|
||||||
|
partIndex = lastReadPart;
|
||||||
|
offset = this.offsets[lastReadPart];
|
||||||
|
}
|
||||||
|
while (partIndex < this.parts.length) {
|
||||||
|
DataBlock part = this.parts[partIndex];
|
||||||
while (pos >= offset && pos < offset + part.size()) {
|
while (pos >= offset && pos < offset + part.size()) {
|
||||||
int count = part.read(dst, pos - offset);
|
int count = part.read(dst, pos - offset);
|
||||||
result += Math.max(count, 0);
|
result += Math.max(count, 0);
|
||||||
if (count <= 0 || !dst.hasRemaining()) {
|
if (count <= 0 || !dst.hasRemaining()) {
|
||||||
|
this.lastReadPart = partIndex;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
pos += count;
|
pos += count;
|
||||||
}
|
}
|
||||||
offset += part.size();
|
offset += part.size();
|
||||||
|
partIndex++;
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012-2023 the original author or authors.
|
* Copyright 2012-2024 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -76,12 +76,13 @@ class VirtualZipDataBlock extends VirtualDataBlock implements CloseableDataBlock
|
||||||
.withOffsetToLocalHeader(offsetToLocalHeader);
|
.withOffsetToLocalHeader(offsetToLocalHeader);
|
||||||
int originalExtraFieldLength = Short.toUnsignedInt(originalRecord.extraFieldLength());
|
int originalExtraFieldLength = Short.toUnsignedInt(originalRecord.extraFieldLength());
|
||||||
int originalFileCommentLength = Short.toUnsignedInt(originalRecord.fileCommentLength());
|
int originalFileCommentLength = Short.toUnsignedInt(originalRecord.fileCommentLength());
|
||||||
DataBlock extraFieldAndComment = new DataPart(
|
int extraFieldAndCommentSize = originalExtraFieldLength + originalFileCommentLength;
|
||||||
originalRecordPos + originalRecord.size() - originalExtraFieldLength - originalFileCommentLength,
|
|
||||||
originalExtraFieldLength + originalFileCommentLength);
|
|
||||||
parts.add(new ByteArrayDataBlock(record.asByteArray()));
|
parts.add(new ByteArrayDataBlock(record.asByteArray()));
|
||||||
parts.add(name);
|
parts.add(name);
|
||||||
parts.add(extraFieldAndComment);
|
if (extraFieldAndCommentSize > 0) {
|
||||||
|
parts.add(new DataPart(originalRecordPos + originalRecord.size() - extraFieldAndCommentSize,
|
||||||
|
extraFieldAndCommentSize));
|
||||||
|
}
|
||||||
return record.size();
|
return record.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -93,7 +94,9 @@ class VirtualZipDataBlock extends VirtualDataBlock implements CloseableDataBlock
|
||||||
int extraFieldLength = Short.toUnsignedInt(originalRecord.extraFieldLength());
|
int extraFieldLength = Short.toUnsignedInt(originalRecord.extraFieldLength());
|
||||||
parts.add(new ByteArrayDataBlock(record.asByteArray()));
|
parts.add(new ByteArrayDataBlock(record.asByteArray()));
|
||||||
parts.add(name);
|
parts.add(name);
|
||||||
parts.add(new DataPart(originalRecordPos + originalRecord.size() - extraFieldLength, extraFieldLength));
|
if (extraFieldLength > 0) {
|
||||||
|
parts.add(new DataPart(originalRecordPos + originalRecord.size() - extraFieldLength, extraFieldLength));
|
||||||
|
}
|
||||||
parts.add(content);
|
parts.add(content);
|
||||||
if (dataDescriptorRecord != null) {
|
if (dataDescriptorRecord != null) {
|
||||||
parts.add(new ByteArrayDataBlock(dataDescriptorRecord.asByteArray()));
|
parts.add(new ByteArrayDataBlock(dataDescriptorRecord.asByteArray()));
|
||||||
|
|
|
@ -0,0 +1,77 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2012-2024 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.boot.loader.zip;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.util.Random;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.zip.ZipEntry;
|
||||||
|
import java.util.zip.ZipInputStream;
|
||||||
|
import java.util.zip.ZipOutputStream;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Disabled;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.io.TempDir;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Performance tests for {@link ZipContent} that creates a {@link VirtualZipDataBlock}.
|
||||||
|
*
|
||||||
|
* @author Phillip Webb
|
||||||
|
*/
|
||||||
|
@Disabled("Only used for manual testing")
|
||||||
|
public class VirtualZipPerformanceTests {
|
||||||
|
|
||||||
|
@TempDir
|
||||||
|
Path temp;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void sequentialReadPerformace() throws IOException {
|
||||||
|
File file = createZipWithLargeEntries();
|
||||||
|
long start = System.nanoTime();
|
||||||
|
try (ZipContent zipContent = ZipContent.open(file.toPath(), "test/")) {
|
||||||
|
try (InputStream in = zipContent.openRawZipData().asInputStream()) {
|
||||||
|
ZipInputStream zip = new ZipInputStream(in);
|
||||||
|
ZipEntry entry = zip.getNextEntry();
|
||||||
|
while (entry != null) {
|
||||||
|
entry = zip.getNextEntry();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.println(TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start));
|
||||||
|
}
|
||||||
|
|
||||||
|
private File createZipWithLargeEntries() throws IOException {
|
||||||
|
byte[] bytes = new byte[1024 * 1024];
|
||||||
|
new Random().nextBytes(bytes);
|
||||||
|
File file = this.temp.resolve("test.zip").toFile();
|
||||||
|
try (ZipOutputStream out = new ZipOutputStream(new FileOutputStream(file))) {
|
||||||
|
out.putNextEntry(new ZipEntry("test/"));
|
||||||
|
out.closeEntry();
|
||||||
|
for (int i = 0; i < 50; i++) {
|
||||||
|
out.putNextEntry(new ZipEntry("test/" + i + ".dat"));
|
||||||
|
out.write(bytes);
|
||||||
|
out.closeEntry();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue