Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Use java.nio.file instead of java.io #53

Merged
merged 2 commits into from
Apr 4, 2024
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
8 changes: 4 additions & 4 deletions deb/src/main/java/org/eclipse/packager/deb/Packages.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,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 +36,8 @@ public final class Packages {
private Packages() {
}

public static Map<String, String> parseControlFile(final File packageFile) throws IOException, ParserException {
try (final ArArchiveInputStream in = new ArArchiveInputStream(new FileInputStream(packageFile))) {
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 @@ -12,15 +12,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 +45,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 +55,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 +94,27 @@ 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);
}

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());
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;
}
}));
}

public void addFile(final File file, final String fileName, final EntryInformation entryInformation, final Optional<Supplier<Instant>> timestampSupplier) throws IOException {
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 +266,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 +287,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 +359,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 @@ -12,22 +12,25 @@
*/
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 Path file;

private final File file;

public FileContentProvider(final File file) {
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 +39,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 @@ -13,20 +13,18 @@
package org.eclipse.packager.deb.build;

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 {
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 All @@ -50,7 +48,7 @@ public long getSize() {
}

@Override
public InputStream createInputStream() throws IOException {
public InputStream createInputStream() {
if (this.data != null) {
return new ByteArrayInputStream(this.data);
} else {
Expand All @@ -62,5 +60,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 <ctron@dentrassi.de>");
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
3 changes: 1 addition & 2 deletions rpm/src/main/java/org/eclipse/packager/rpm/app/Dumper.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
Expand Down Expand Up @@ -150,7 +149,7 @@ private static void dumpEntry(final CpioArchiveEntry entry) {

public static void main(final String[] args) throws IOException {
for (final String file : args) {
dump(Paths.get(file));
dump(Path.of(file));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
package org.eclipse.packager.rpm.signature;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -43,10 +43,9 @@

/**
* Sign existing RPM file by calling
* {@link #perform(File, InputStream, String, OutputStream, HashAlgorithm)}
* {@link #perform(Path, InputStream, String, OutputStream, HashAlgorithm)}
*/
public class RpmFileSignatureProcessor {

private RpmFileSignatureProcessor() {
// Hide default constructor because of the static context
}
Expand All @@ -64,7 +63,7 @@ private RpmFileSignatureProcessor() {
* @throws IOException
* @throws PGPException
*/
public static void perform(File rpm, InputStream privateKeyIn, String passphrase, OutputStream out, HashAlgorithm hashAlgorithm)
public static void perform(Path rpm, InputStream privateKeyIn, String passphrase, OutputStream out, HashAlgorithm hashAlgorithm)
throws IOException, PGPException {

final long leadLength = 96;
Expand All @@ -77,15 +76,15 @@ public static void perform(File rpm, InputStream privateKeyIn, String passphrase
long payloadSize = 0L;
byte[] signatureHeader;

if (!rpm.exists()) {
throw new IOException("The file " + rpm.getName() + " does not exist");
if (!Files.exists(rpm)) {
throw new IOException("The file " + rpm.getFileName() + " does not exist");
}

// Extract private key
PGPPrivateKey privateKey = getPrivateKey(privateKeyIn, passphrase);

// Get the information of the RPM
try (RpmInputStream rpmIn = new RpmInputStream(new FileInputStream(rpm))) {
try (RpmInputStream rpmIn = new RpmInputStream(Files.newInputStream(rpm))) {
signatureHeaderStart = rpmIn.getSignatureHeader().getStart();
signatureHeaderLength = rpmIn.getSignatureHeader().getLength();
payloadHeaderStart = rpmIn.getPayloadHeader().getStart();
Expand All @@ -97,12 +96,11 @@ public static void perform(File rpm, InputStream privateKeyIn, String passphrase

if (signatureHeaderStart == 0L || signatureHeaderLength == 0L || payloadHeaderStart == 0L
|| payloadHeaderLength == 0L || payloadStart == 0L || archiveSize == 0L) {
throw new IOException("Unable to read " + rpm.getName() + " informations.");
throw new IOException("Unable to read " + rpm.getFileName() + " informations.");
}

// Build the signature header by digest payload header + payload
try (FileInputStream in = new FileInputStream(rpm)) {
FileChannel channelIn = in.getChannel();
try (FileChannel channelIn = FileChannel.open(rpm)) {
payloadSize = channelIn.size() - payloadStart;
channelIn.position(leadLength + signatureHeaderLength);
ByteBuffer payloadHeaderBuff = ByteBuffer.allocate((int) payloadHeaderLength);
Expand All @@ -113,7 +111,7 @@ public static void perform(File rpm, InputStream privateKeyIn, String passphrase
}

// Write to the OutputStream
try (FileInputStream in = new FileInputStream(rpm)) {
try (InputStream in = Files.newInputStream(rpm)) {
IOUtils.copyLarge(in, out, 0, leadLength);
IOUtils.skip(in, signatureHeaderLength);
out.write(signatureHeader);
Expand Down
Loading