Skip to content

Commit

Permalink
Rename FileChannelDataBlock to FileDataBlock
Browse files Browse the repository at this point in the history
Rename the internal `FileChannelDataBlock` to `FileDataBlock` since we
want to fallback to a `RandomAccessFile` when a thread is interrupted.

See gh-40096
  • Loading branch information
philwebb committed Apr 16, 2024
1 parent 778c528 commit 4203e1f
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 66 deletions.
Original file line number Diff line number Diff line change
@@ -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");
* you may not use this file except in compliance with the License.
Expand All @@ -16,6 +16,7 @@

package org.springframework.boot.loader.zip;

import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedByInterruptException;
Expand All @@ -29,14 +30,14 @@
import org.springframework.boot.loader.log.DebugLogger;

/**
* Reference counted {@link DataBlock} implementation backed by a {@link FileChannel} with
* Reference counted {@link DataBlock} implementation backed by a {@link File} with
* support for slicing.
*
* @author Phillip Webb
*/
class FileChannelDataBlock implements CloseableDataBlock {
class FileDataBlock implements CloseableDataBlock {

private static final DebugLogger debug = DebugLogger.get(FileChannelDataBlock.class);
private static final DebugLogger debug = DebugLogger.get(FileDataBlock.class);

static Tracker tracker;

Expand All @@ -46,13 +47,13 @@ class FileChannelDataBlock implements CloseableDataBlock {

private final long size;

FileChannelDataBlock(Path path) throws IOException {
FileDataBlock(Path path) throws IOException {
this.channel = new ManagedFileChannel(path);
this.offset = 0;
this.size = Files.size(path);
}

FileChannelDataBlock(ManagedFileChannel channel, long offset, long size) {
FileDataBlock(ManagedFileChannel channel, long offset, long size) {
this.channel = channel;
this.offset = offset;
this.size = size;
Expand Down Expand Up @@ -115,26 +116,26 @@ <E extends Exception> void ensureOpen(Supplier<E> exceptionSupplier) throws E {
}

/**
* Return a new {@link FileChannelDataBlock} slice providing access to a subset of the
* data. The caller is responsible for calling {@link #open()} and {@link #close()} on
* the returned block.
* Return a new {@link FileDataBlock} slice providing access to a subset of the data.
* The caller is responsible for calling {@link #open()} and {@link #close()} on the
* returned block.
* @param offset the start offset for the slice relative to this block
* @return a new {@link FileChannelDataBlock} instance
* @return a new {@link FileDataBlock} instance
* @throws IOException on I/O error
*/
FileChannelDataBlock slice(long offset) throws IOException {
FileDataBlock slice(long offset) throws IOException {
return slice(offset, this.size - offset);
}

/**
* Return a new {@link FileChannelDataBlock} slice providing access to a subset of the
* data. The caller is responsible for calling {@link #open()} and {@link #close()} on
* the returned block.
* Return a new {@link FileDataBlock} slice providing access to a subset of the data.
* The caller is responsible for calling {@link #open()} and {@link #close()} on the
* returned block.
* @param offset the start offset for the slice relative to this block
* @param size the size of the new slice
* @return a new {@link FileChannelDataBlock} instance
* @return a new {@link FileDataBlock} instance
*/
FileChannelDataBlock slice(long offset, long size) {
FileDataBlock slice(long offset, long size) {
if (offset == 0 && size == this.size) {
return this;
}
Expand All @@ -145,7 +146,7 @@ FileChannelDataBlock slice(long offset, long size) {
throw new IllegalArgumentException("Size must not be negative and must be within bounds");
}
debug.log("Slicing %s at %s with size %s", this.channel, offset, size);
return new FileChannelDataBlock(this.channel, this.offset + offset, size);
return new FileDataBlock(this.channel, this.offset + offset, size);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public final class ZipContent implements Closeable {

private final Kind kind;

private final FileChannelDataBlock data;
private final FileDataBlock data;

private final long centralDirectoryPos;

Expand All @@ -96,7 +96,7 @@ public final class ZipContent implements Closeable {

private SoftReference<Map<Class<?>, Object>> info;

private ZipContent(Source source, Kind kind, FileChannelDataBlock data, long centralDirectoryPos, long commentPos,
private ZipContent(Source source, Kind kind, FileDataBlock data, long centralDirectoryPos, long commentPos,
long commentLength, int[] lookupIndexes, int[] nameHashLookups, int[] relativeCentralDirectoryOffsetLookups,
NameOffsetLookups nameOffsetLookups, boolean hasJarSignatureFile) {
this.source = source;
Expand Down Expand Up @@ -449,7 +449,7 @@ private static final class Loader {

private final Source source;

private final FileChannelDataBlock data;
private final FileDataBlock data;

private final long centralDirectoryPos;

Expand All @@ -463,8 +463,7 @@ private static final class Loader {

private int cursor;

private Loader(Source source, Entry directoryEntry, FileChannelDataBlock data, long centralDirectoryPos,
int maxSize) {
private Loader(Source source, Entry directoryEntry, FileDataBlock data, long centralDirectoryPos, int maxSize) {
this.source = source;
this.data = data;
this.centralDirectoryPos = centralDirectoryPos;
Expand Down Expand Up @@ -561,7 +560,7 @@ static ZipContent load(Source source) throws IOException {

private static ZipContent loadNonNested(Source source) throws IOException {
debug.log("Loading non-nested zip '%s'", source.path());
return openAndLoad(source, Kind.ZIP, new FileChannelDataBlock(source.path()));
return openAndLoad(source, Kind.ZIP, new FileDataBlock(source.path()));
}

private static ZipContent loadNestedZip(Source source, Entry entry) throws IOException {
Expand All @@ -573,7 +572,7 @@ private static ZipContent loadNestedZip(Source source, Entry entry) throws IOExc
return openAndLoad(source, Kind.NESTED_ZIP, entry.getContent());
}

private static ZipContent openAndLoad(Source source, Kind kind, FileChannelDataBlock data) throws IOException {
private static ZipContent openAndLoad(Source source, Kind kind, FileDataBlock data) throws IOException {
try {
data.open();
return loadContent(source, kind, data);
Expand All @@ -584,7 +583,7 @@ private static ZipContent openAndLoad(Source source, Kind kind, FileChannelDataB
}
}

private static ZipContent loadContent(Source source, Kind kind, FileChannelDataBlock data) throws IOException {
private static ZipContent loadContent(Source source, Kind kind, FileDataBlock data) throws IOException {
ZipEndOfCentralDirectoryRecord.Located locatedEocd = ZipEndOfCentralDirectoryRecord.load(data);
ZipEndOfCentralDirectoryRecord eocd = locatedEocd.endOfCentralDirectoryRecord();
long eocdPos = locatedEocd.pos();
Expand Down Expand Up @@ -634,7 +633,7 @@ private static ZipContent loadContent(Source source, Kind kind, FileChannelDataB
* @return the offset within the data where the archive begins
* @throws IOException on I/O error
*/
private static long getStartOfZipContent(FileChannelDataBlock data, ZipEndOfCentralDirectoryRecord eocd,
private static long getStartOfZipContent(FileDataBlock data, ZipEndOfCentralDirectoryRecord eocd,
Zip64EndOfCentralDirectoryRecord zip64Eocd) throws IOException {
long specifiedOffsetToStartOfCentralDirectory = (zip64Eocd != null)
? zip64Eocd.offsetToStartOfCentralDirectory() : eocd.offsetToStartOfCentralDirectory();
Expand Down Expand Up @@ -699,7 +698,7 @@ public class Entry {

private volatile String name;

private volatile FileChannelDataBlock content;
private volatile FileDataBlock content;

/**
* Create a new {@link Entry} instance.
Expand Down Expand Up @@ -789,13 +788,13 @@ public int getUncompressedSize() {
* @throws IOException on I/O error
*/
public CloseableDataBlock openContent() throws IOException {
FileChannelDataBlock content = getContent();
FileDataBlock content = getContent();
content.open();
return content;
}

private FileChannelDataBlock getContent() throws IOException {
FileChannelDataBlock content = this.content;
private FileDataBlock getContent() throws IOException {
FileDataBlock content = this.content;
if (content == null) {
int pos = this.centralRecord.offsetToLocalHeader();
checkNotZip64Extended(pos);
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
* you may not use this file except in compliance with the License.
Expand All @@ -25,8 +25,8 @@
import org.junit.jupiter.api.extension.ExtendWith;

/**
* Annotation that can be added to tests to assert that {@link FileChannelDataBlock} files
* are not left open.
* Annotation that can be added to tests to assert that {@link FileDataBlock} files are
* not left open.
*
* @author Phillip Webb
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import org.junit.jupiter.api.extension.ExtensionContext;

import org.springframework.boot.loader.ref.DefaultCleanerTracking;
import org.springframework.boot.loader.zip.FileChannelDataBlock.Tracker;
import org.springframework.boot.loader.zip.FileDataBlock.Tracker;

import static org.assertj.core.api.Assertions.assertThat;

Expand All @@ -45,14 +45,14 @@ class AssertFileChannelDataBlocksClosedExtension implements BeforeEachCallback,
@Override
public void beforeEach(ExtensionContext context) throws Exception {
tracker.clear();
FileChannelDataBlock.tracker = tracker;
FileDataBlock.tracker = tracker;
DefaultCleanerTracking.set(tracker::addedCleanable);
}

@Override
public void afterEach(ExtensionContext context) throws Exception {
tracker.assertAllClosed();
FileChannelDataBlock.tracker = null;
FileDataBlock.tracker = null;
}

private static final class OpenFilesTracker implements Tracker {
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
* you may not use this file except in compliance with the License.
Expand All @@ -16,7 +16,7 @@

package org.springframework.boot.loader.zip;

import org.springframework.boot.loader.zip.FileChannelDataBlock.ManagedFileChannel;
import org.springframework.boot.loader.zip.FileDataBlock.ManagedFileChannel;

/**
* Test access to {@link ManagedFileChannel} details.
Expand All @@ -28,6 +28,6 @@ public final class FileChannelDataBlockManagedFileChannel {
private FileChannelDataBlockManagedFileChannel() {
}

public static int BUFFER_SIZE = FileChannelDataBlock.ManagedFileChannel.BUFFER_SIZE;
public static int BUFFER_SIZE = FileDataBlock.ManagedFileChannel.BUFFER_SIZE;

}
Loading

0 comments on commit 4203e1f

Please sign in to comment.