Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Code cleanup #113

Merged
merged 1 commit into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* limitations under the License.
*/

@SuppressWarnings("OctalInteger")
public final class AttributeConstants {

public static final int OCTAL_OWNER_READ = 0400;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
/**
* @author Kristian Rosenvold
*/
@SuppressWarnings("OctalInteger")
public class AttributeUtils {
/*
Reads last-modified with proper failure handling if something goes wrong.
Expand Down Expand Up @@ -101,7 +102,6 @@ public static BasicFileAttributes getFileAttributes(@Nonnull File file) throws I

public static BasicFileAttributes getFileAttributes(Path path) throws IOException {
if (isUnix(path)) {

try {
return Files.readAttributes(path, PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
} catch (UnsupportedOperationException ignore) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,10 @@ public FileAttributes(@Nonnull File file, boolean followLinks) throws IOExceptio
this.octalMode = attrs.containsKey("mode")
? (Integer) attrs.get("mode") & 0xfff
: PlexusIoResourceAttributes.UNKNOWN_OCTAL_MODE;
//noinspection unchecked
this.permissions = attrs.containsKey("permissions")
? (Set<PosixFilePermission>) attrs.get("permissions")
: Collections.<PosixFilePermission>emptySet();
: Collections.emptySet();
this.size = (Long) attrs.get("size");
this.lastModifiedTime = (FileTime) attrs.get("lastModifiedTime");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@ public interface PlexusIoResourceAttributes {
*/
int getOctalMode();

// @Nonnull
// String getOctalModeString();

/**
* Indicates if this is a symbolic link element.
* For file-based resource attributes this value may be always "false" for versions prior to java7.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,13 @@ public class SymlinkUtils {
* @throws java.io.IOException
*/
public static @Nonnull File readSymbolicLink(@Nonnull File symlink) throws IOException {
final java.nio.file.Path path = java.nio.file.Files.readSymbolicLink(symlink.toPath());
return path.toFile();
return Files.readSymbolicLink(symlink.toPath()).toFile();
}

public static @Nonnull File createSymbolicLink(@Nonnull File symlink, File target) throws IOException {
Path link = symlink.toPath();
if (!Files.exists(link, LinkOption.NOFOLLOW_LINKS)) {
link = java.nio.file.Files.createSymbolicLink(link, target.toPath());
link = Files.createSymbolicLink(link, target.toPath());
}
return link.toFile();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

public class PlexusIoProxyResourceAttributes implements PlexusIoResourceAttributes {

PlexusIoResourceAttributes target;
final PlexusIoResourceAttributes target;

public PlexusIoProxyResourceAttributes(PlexusIoResourceAttributes thisAttr) {
this.target = thisAttr;
Expand Down Expand Up @@ -45,11 +45,6 @@ public Integer getUserId() {
return target.getUserId();
}

/* public String getOctalModeString()
{
return target.getOctalModeString();
}
*/
public boolean isOwnerWritable() {
return target.isOwnerWritable();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public abstract class AbstractFileMapper implements FileMapper {
* Checks the input and returns it without modifications.
*/
public @Nonnull String getMappedFileName(@Nonnull String pName) {
if (pName == null || pName.length() == 0) {
if (pName == null || pName.isEmpty()) {
throw new IllegalArgumentException("The source name must not be null.");
}
return pName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void setTargetExtension(String pTargetExtension) {
if (pTargetExtension == null) {
throw new IllegalArgumentException("The target extension is null.");
}
if (pTargetExtension.length() == 0) {
if (pTargetExtension.isEmpty()) {
throw new IllegalArgumentException("The target extension is empty.");
}
if (pTargetExtension.charAt(0) == '.') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class IdentityMapper extends AbstractFileMapper {

@Nonnull
public String getMappedFileName(@Nonnull String pName) {
if (pName == null || pName.length() == 0) {
if (pName == null || pName.isEmpty()) {
throw new IllegalArgumentException("The source name must not be null.");
}
return pName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void setTargetName(String pName) {
if (pName == null) {
throw new IllegalArgumentException("The target name is null.");
}
if (pName.length() == 0) {
if (pName.isEmpty()) {
throw new IllegalArgumentException("The target name is empty.");
}
targetName = pName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void setPrefix(String prefix) {
* Performs the mapping of a file name by adding a prefix.
*/
public static String getMappedFileName(String prefix, String name) {
if (prefix == null || prefix.length() == 0) {
if (prefix == null || prefix.isEmpty()) {
return name;
}
return prefix + name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
public interface InputStreamTransformer {
/**
* Transform the supplied input stream into another input stream.
*
* <p>
* The close method will be delegated through the entire call chain
*
* @param resource The p-io resource the stream is for
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import java.io.IOException;
import java.util.Iterator;

import org.codehaus.plexus.components.io.functions.PlexusIoResourceConsumer;

/**
* Default implementation of {@link PlexusIoFileResourceCollection} for
* zip files, tar files, etc.
Expand Down Expand Up @@ -106,20 +104,17 @@ public void close() throws IOException {
}

public Stream stream() {
return new Stream() {
public void forEach(PlexusIoResourceConsumer resourceConsumer) throws IOException {

final Iterator<PlexusIoResource> it = getEntries();
while (it.hasNext()) {
final PlexusIoResource res = it.next();
if (isSelected(res)) {
resourceConsumer.accept(res);
}
}
if (it instanceof Closeable) {
((Closeable) it).close();
return resourceConsumer -> {
Iterator<PlexusIoResource> it = getEntries();
while (it.hasNext()) {
final PlexusIoResource res = it.next();
if (isSelected(res)) {
resourceConsumer.accept(res);
}
}
if (it instanceof Closeable) {
((Closeable) it).close();
}
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,8 @@ public PlexusIoResource resolve(final PlexusIoResource resource) throws IOExcept

public long getLastModified() throws IOException {
long lastModified = PlexusIoResource.UNKNOWN_MODIFICATION_DATE;
for (final Iterator iter = getResources(); iter.hasNext(); ) {
final PlexusIoResource res = (PlexusIoResource) iter.next();
long l = res.getLastModified();
for (Iterator<PlexusIoResource> iter = getResources(); iter.hasNext(); ) {
long l = iter.next().getLastModified();
if (l == PlexusIoResource.UNKNOWN_MODIFICATION_DATE) {
return PlexusIoResource.UNKNOWN_MODIFICATION_DATE;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ public Deferred(final PlexusIoResource resource, PlexusIoResourceCollection owne
throws IOException {
this.resource = resource;
this.owner = owner;
dfos = hasTransformer ? new DeferredFileOutputStream(5000000, "p-archiver", null, null) : null;
dfos = hasTransformer
? DeferredFileOutputStream.builder()
.setThreshold(5000000)
.setPrefix("p-archiver")
.get()
: null;
if (dfos != null) {
InputStream inputStream = owner.getInputStream(resource);
IOUtils.copy(inputStream, dfos);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public InputStream getContents() throws IOException {
protected String getName(File file) throws IOException {
final String name = file.getPath();
final String ext = getDefaultExtension();
if (ext != null && ext.length() > 0 && name.endsWith(ext)) {
if (ext != null && !ext.isEmpty() && name.endsWith(ext)) {
return name.substring(0, name.length() - ext.length());
}
return name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.attribute.FileTime;

import org.apache.commons.io.IOUtils;
Expand Down Expand Up @@ -103,7 +104,10 @@ protected PlexusIoFileResource(@Nonnull File file, @Nonnull String name, @Nonnul
private static DeferredFileOutputStream asDeferredStream(
@Nonnull ContentSupplier supplier, @Nonnull InputStreamTransformer transToUse, PlexusIoResource resource)
throws IOException {
DeferredFileOutputStream dfos = new DeferredFileOutputStream(5000000, "p-archiver", null, null);
DeferredFileOutputStream dfos = DeferredFileOutputStream.builder()
.setThreshold(5000000)
.setPrefix("p-archiver")
.get();
InputStream inputStream = supplier.getContents();
InputStream transformed = transToUse.transform(resource, inputStream);
IOUtils.copy(transformed, dfos);
Expand All @@ -113,11 +117,7 @@ private static DeferredFileOutputStream asDeferredStream(
}

private static ContentSupplier getRootContentSupplier(final File file) {
return new ContentSupplier() {
public InputStream getContents() throws IOException {
return new FileInputStream(file);
}
};
return () -> Files.newInputStream(file.toPath());
}

public static String getName(File file) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public interface PlexusIoResource extends FileInfo, SizeSupplier, ContentSupplie
* Creates an {@link java.io.InputStream}, which may be used to read
* the files contents. This is useful, if the file selector
* comes to a decision based on the files contents.
*
* <p>
* Please note that this InputStream is unbuffered. Clients should wrap this in a
* BufferedInputStream or attempt reading reasonably large chunks (8K+).
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,4 @@ public boolean isFile() {
public long getLastModified() {
return targetResource.getLastModified();
}

@Nonnull
@Override
public PlexusIoResourceAttributes getAttributes() {
return super.getAttributes();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
*/
public class PlexusIoProxyResourceCollection extends AbstractPlexusIoResourceCollectionWithAttributes
implements EncodingSupported {
private PlexusIoResourceCollection src;
private final PlexusIoResourceCollection src;

public PlexusIoProxyResourceCollection(@Nonnull PlexusIoResourceCollection src) {
this.src = src;
Expand Down Expand Up @@ -98,14 +98,14 @@ protected FileSelector getDefaultFileSelector() {

private String getNonEmptyPrfix() {
String prefix = getPrefix();
if (prefix != null && prefix.length() == 0) {
if (prefix != null && prefix.isEmpty()) {
return null;
}
return prefix;
}

class FwdIterator extends ForwardingIterator {
Iterator<PlexusIoResource> iter;
final Iterator<PlexusIoResource> iter;

private final FileSelector fileSelector = getDefaultFileSelector();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.codehaus.plexus.components.io.resources.PlexusIoResource;

class ResourceInvocationHandler implements InvocationHandler {
private PlexusIoResource testImpl;
private final PlexusIoResource testImpl;

private final ContentSupplier contentSupplier;
private final NameSupplier nameSupplier;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
import java.nio.file.attribute.PosixFilePermission;
import java.util.Set;

import org.codehaus.plexus.util.Os;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.OS;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
Expand Down Expand Up @@ -34,22 +35,22 @@ void testMorePatterns() {
}

@Test
void testEvenMorePatterns() throws Exception {
void testEvenMorePatterns() {
final Set<PosixFilePermission> permissions = AttributeUtils.getPermissions(0412);
assertTrue(permissions.contains(PosixFilePermission.OWNER_READ));
assertTrue(permissions.contains(PosixFilePermission.GROUP_EXECUTE));
assertTrue(permissions.contains(PosixFilePermission.OTHERS_WRITE));
}

@Test
void test777() throws Exception {
void test777() {
final Set<PosixFilePermission> permissions = AttributeUtils.getPermissions(0777);
assertEquals(9, permissions.size());
}

@Test
@DisabledOnOs(OS.WINDOWS)
void testChmodBackAndForth() throws IOException {
if (Os.isFamily(Os.FAMILY_WINDOWS)) return;
final File bxx = File.createTempFile("bxx", "ff");
AttributeUtils.chmod(bxx, 0422);
PlexusIoResourceAttributes firstAttrs = new FileAttributes(bxx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@

import java.io.File;

import org.codehaus.plexus.util.Os;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.OS;

import static org.junit.jupiter.api.Assertions.assertNotNull;

Expand All @@ -28,14 +29,9 @@
*/
public class FileAttributesTest {
@Test
@DisabledOnOs(OS.WINDOWS)
void testGetPosixFileAttributes() throws Exception {

if (Os.isFamily(Os.FAMILY_WINDOWS)) {
return;
}

File file = new File(".");

PlexusIoResourceAttributes fa = new FileAttributes(file);
assertNotNull(fa);
}
Expand Down
Loading
Loading