Skip to content

Commit

Permalink
Use java.nio.file instead of java.io
Browse files Browse the repository at this point in the history
  • Loading branch information
dwalluck committed Mar 29, 2024
1 parent 890e443 commit 0195b32
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 48 deletions.
10 changes: 8 additions & 2 deletions deb/src/main/java/org/eclipse/packager/deb/Packages.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
package org.eclipse.packager.deb;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
Expand All @@ -36,8 +37,13 @@ public final class Packages {
private Packages() {
}

@Deprecated
public static Map<String, String> parseControlFile(final File packageFile) throws IOException, ParserException {
try (final ArArchiveInputStream in = new ArArchiveInputStream(new FileInputStream(packageFile))) {
return parseControlFile(packageFile.toPath());
}

public static Map<String, String> parseControlFile(final Path packageFile) throws IOException, ParserException {
try (final ArArchiveInputStream in = new ArArchiveInputStream(Files.newInputStream(packageFile))) {
ArchiveEntry ar;
while ((ar = in.getNextEntry()) != null) {
if (!ar.getName().equals("control.tar.gz")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
package org.eclipse.packager.deb.build;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringWriter;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.MessageDigest;
import java.time.Instant;
import java.util.HashMap;
Expand All @@ -46,7 +46,7 @@
import com.google.common.io.ByteStreams;

public class DebianPackageWriter implements AutoCloseable, BinaryPackageBuilder {
public static final Charset CHARSET = Charset.forName("UTF-8");
public static final Charset CHARSET = StandardCharsets.UTF_8;

private static final int AR_ARCHIVE_DEFAULT_MODE = 33188; // see ArArchive

Expand All @@ -56,7 +56,7 @@ public class DebianPackageWriter implements AutoCloseable, BinaryPackageBuilder

private final Supplier<Instant> timestampSupplier;

private final File dataTemp;
private final Path dataTemp;

private final TarArchiveOutputStream dataStream;

Expand Down Expand Up @@ -95,19 +95,37 @@ public DebianPackageWriter(final OutputStream stream, final BinaryPackageControl
this.ar.write(this.binaryHeader);
this.ar.closeArchiveEntry();

this.dataTemp = Files.createTempFile("data", null).toFile();
this.dataTemp = Files.createTempFile("data", null);

this.dataStream = new TarArchiveOutputStream(new GZIPOutputStream(new FileOutputStream(this.dataTemp)));
this.dataStream = new TarArchiveOutputStream(new GZIPOutputStream(Files.newOutputStream(this.dataTemp)));
this.dataStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
}

@Deprecated
public void addFile(final File file, final String fileName, final EntryInformation entryInformation) throws IOException {
addFile(new FileContentProvider(file), fileName, entryInformation, Optional.of((Supplier<Instant>) () -> {
return file == null || !file.canRead() ? null : Instant.ofEpochMilli(file.lastModified());
addFile(file.toPath(), fileName, entryInformation);
}

public void addFile(final Path file, final String fileName, final EntryInformation entryInformation) throws IOException {
addFile(new FileContentProvider(file), fileName, entryInformation, Optional.of(() -> {
if (file == null || !Files.isReadable(file)) {
return null;
}

try {
return Files.getLastModifiedTime(file).toInstant();
} catch (IOException e) {
return null;
}
}));
}

@Deprecated
public void addFile(final File file, final String fileName, final EntryInformation entryInformation, final Optional<Supplier<Instant>> timestampSupplier) throws IOException {
addFile(file.toPath(), fileName, entryInformation, timestampSupplier);
}

public void addFile(final Path file, final String fileName, final EntryInformation entryInformation, final Optional<Supplier<Instant>> timestampSupplier) throws IOException {
addFile(new FileContentProvider(file), fileName, entryInformation, timestampSupplier);
}

Expand Down Expand Up @@ -259,14 +277,14 @@ public void close() throws IOException {
this.ar.close();
}
} finally {
this.dataTemp.delete();
Files.delete(this.dataTemp);
}
}

private void buildAndAddControlFile(final Supplier<Instant> timestampSupplier) throws IOException, FileNotFoundException {
final File controlFile = File.createTempFile("control", null);
private void buildAndAddControlFile(final Supplier<Instant> timestampSupplier) throws IOException {
final Path controlFile = Files.createTempFile("control", null);
try {
try (GZIPOutputStream gout = new GZIPOutputStream(new FileOutputStream(controlFile));
try (GZIPOutputStream gout = new GZIPOutputStream(Files.newOutputStream(controlFile));
TarArchiveOutputStream tout = new TarArchiveOutputStream(gout)) {
tout.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);

Expand All @@ -280,7 +298,7 @@ private void buildAndAddControlFile(final Supplier<Instant> timestampSupplier) t
}
addArFile(controlFile, "control.tar.gz", timestampSupplier);
} finally {
controlFile.delete();
Files.delete(controlFile);
}
}

Expand Down Expand Up @@ -352,11 +370,11 @@ protected ContentProvider createConfFilesContent() throws IOException {
return new StaticContentProvider(sw.toString());
}

private void addArFile(final File file, final String entryName, final Supplier<Instant> timestampSupplier) throws IOException {
final ArArchiveEntry entry = new ArArchiveEntry(entryName, file.length(), 0, 0, AR_ARCHIVE_DEFAULT_MODE, timestampSupplier.get().getEpochSecond());
private void addArFile(final Path file, final String entryName, final Supplier<Instant> timestampSupplier) throws IOException {
final ArArchiveEntry entry = new ArArchiveEntry(entryName, Files.size(file), 0, 0, AR_ARCHIVE_DEFAULT_MODE, timestampSupplier.get().getEpochSecond());
this.ar.putArchiveEntry(entry);

Files.copy(file.toPath(), this.ar);
Files.copy(file, this.ar);

this.ar.closeArchiveEntry();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,31 @@
package org.eclipse.packager.deb.build;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;

public class FileContentProvider implements ContentProvider {

private final File file;
private final Path file;

@Deprecated
public FileContentProvider(final File file) {
this(file.toPath());
}

public FileContentProvider(final Path file) {
this.file = file;
}

@Override
public long getSize() {
return this.file.length();
try {
return Files.size(this.file);
} catch (final IOException e) {
return 0L;
}
}

@Override
Expand All @@ -36,7 +46,7 @@ public InputStream createInputStream() throws IOException {
return null;
}

return new FileInputStream(this.file);
return Files.newInputStream(this.file);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,23 @@

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

import com.google.common.io.Files;
import java.nio.file.Files;
import java.nio.file.Path;

public class TextFileContentProvider implements ContentProvider {
private final byte[] data;

public TextFileContentProvider(final File file) throws FileNotFoundException, IOException {
@Deprecated
public TextFileContentProvider(final File file) throws IOException {
this(file.toPath());
}

public TextFileContentProvider(final Path file) throws IOException {
if (file != null) {
String data = Files.asCharSource(file, StandardCharsets.UTF_8).read();
String data = Files.readString(file);
if (needFix()) {
data = fix(data);
}
Expand Down Expand Up @@ -62,5 +66,4 @@ public InputStream createInputStream() throws IOException {
public boolean hasContent() {
return this.data != null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -136,10 +137,6 @@ private static void hasField(final BinaryPackageControlFile controlFile, final S

public String makeFileName() {
final String name = String.format("%s_%s_%s.deb", getPackage(), getVersion(), getArchitecture());
try {
return URLEncoder.encode(name, "UTF-8");
} catch (final UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
return URLEncoder.encode(name, StandardCharsets.UTF_8);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@
*/
package org.eclipse.packager.deb.tests;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Instant;
import java.util.Optional;
import java.util.function.Supplier;
Expand All @@ -33,40 +31,40 @@ public class BinaryPackageTest {
@SuppressWarnings("deprecation")
@Test
public void test1() throws IOException, InterruptedException {
final File file1 = Files.createTempFile("test-1-", ".deb").toFile();
final File file2 = Files.createTempFile("test-2-", ".deb").toFile();
final Path file1 = Files.createTempFile("test-1-", ".deb");
final Path file2 = Files.createTempFile("test-2-", ".deb");

final Instant now = Instant.now();
final Supplier<Instant> timestampProvider = () -> now;

createDebFile(file1, timestampProvider);
System.out.println("File: " + file1);
Assertions.assertTrue(file1.exists(), "File exists");
Assertions.assertTrue(Files.exists(file1), "File exists");

Thread.sleep(1_001); // sleep for a second to make sure that a timestamp might be changed

createDebFile(file2, timestampProvider);
System.out.println("File: " + file2);
Assertions.assertTrue(file2.exists(), "File exists");
Assertions.assertTrue(Files.exists(file2), "File exists");

final byte[] b1 = Files.readAllBytes(file1.toPath());
final byte[] b1 = Files.readAllBytes(file1);
final String h1 = Hashing.md5().hashBytes(b1).toString();
final byte[] b2 = Files.readAllBytes(file2.toPath());
final byte[] b2 = Files.readAllBytes(file2);
final String h2 = Hashing.md5().hashBytes(b2).toString();
System.out.println(h1);
System.out.println(h2);
Assertions.assertEquals(h1, h2);
}

private void createDebFile(final File file, final Supplier<Instant> timestampProvider) throws IOException, FileNotFoundException {
private void createDebFile(final Path file, final Supplier<Instant> timestampProvider) throws IOException {
final BinaryPackageControlFile packageFile = new BinaryPackageControlFile();
packageFile.setPackage("test");
packageFile.setVersion("0.0.1");
packageFile.setArchitecture("all");
packageFile.setMaintainer("Jens Reimann <[email protected]>");
packageFile.setDescription("Test package\nThis is just a test package\n\nNothing to worry about!");

try (DebianPackageWriter deb = new DebianPackageWriter(new FileOutputStream(file), packageFile, timestampProvider)) {
try (DebianPackageWriter deb = new DebianPackageWriter(Files.newOutputStream(file), packageFile, timestampProvider)) {
deb.addFile("Hello World\n".getBytes(), "/usr/share/foo-test/foo.txt", null, Optional.of(timestampProvider));
deb.addFile("Hello World\n".getBytes(), "/etc/foo.txt", EntryInformation.DEFAULT_FILE_CONF, Optional.of(timestampProvider));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
package org.eclipse.packager.rpm;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

import org.eclipse.packager.rpm.app.Dumper;
import org.eclipse.packager.rpm.parse.RpmInputStream;
Expand All @@ -27,7 +27,7 @@ public class InputStreamTest {

@Test
public void test1() throws IOException {
try (final RpmInputStream in = new RpmInputStream(new BufferedInputStream(new FileInputStream(new File("src/test/resources/data/org.eclipse.scada-0.2.1-1.noarch.rpm"))))) {
try (final RpmInputStream in = new RpmInputStream(new BufferedInputStream(Files.newInputStream(Paths.get("src/test/resources/data/org.eclipse.scada-0.2.1-1.noarch.rpm"))))) {
Dumper.dumpAll(in);

Assertions.assertEquals(280, in.getPayloadHeader().getStart());
Expand Down Expand Up @@ -61,7 +61,7 @@ public void test1() throws IOException {

@Test
public void test2() throws IOException {
try (final RpmInputStream in = new RpmInputStream(new BufferedInputStream(new FileInputStream(new File("src/test/resources/data/org.eclipse.scada-centos6-0.2.1-1.noarch.rpm"))))) {
try (final RpmInputStream in = new RpmInputStream(new BufferedInputStream(Files.newInputStream(Paths.get("src/test/resources/data/org.eclipse.scada-centos6-0.2.1-1.noarch.rpm"))))) {
Dumper.dumpAll(in);
}
}
Expand Down

0 comments on commit 0195b32

Please sign in to comment.