Skip to content

Commit

Permalink
Merge pull request wildfly#5586 from ehsavoie/WFCORE-6431
Browse files Browse the repository at this point in the history
[WFCORE-6431]: Use InputStream.nullInputStream.
  • Loading branch information
yersan authored Jul 20, 2023
2 parents 6c3db89 + e1f54df commit 3e12aaa
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,17 @@ public byte[] addContent(InputStream stream) throws IOException {
byte[] sha1Bytes;
Path tmp = File.createTempFile(CONTENT, ".tmp", repoRoot).toPath();
if (stream != null) {
try (OutputStream fos = Files.newOutputStream(tmp); MessageDigestHandle digestHandle = new MessageDigestHandle()) {
MessageDigest messageDigest = digestHandle.getMessageDigest();
DigestOutputStream dos = new DigestOutputStream(fos, messageDigest);
try (OutputStream fos = Files.newOutputStream(tmp);
MessageDigestHandle digestHandle = new MessageDigestHandle()) {
DigestOutputStream dos = new DigestOutputStream(fos, digestHandle.getMessageDigest());
BufferedInputStream bis = new BufferedInputStream(stream);
byte[] bytes = new byte[8192];
int read;
while ((read = bis.read(bytes)) > -1) {
dos.write(bytes, 0, read);
}
fos.flush();
sha1Bytes = messageDigest.digest();
sha1Bytes = dos.getMessageDigest().digest();
}
} else {//create a directory instead
Files.delete(tmp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package org.jboss.as.repository;

import static org.jboss.as.repository.HashUtil.emptyStream;

import java.io.InputStream;

/**
Expand All @@ -34,7 +32,7 @@ public ExplodedContent(String relativePath, InputStream content) {

public ExplodedContent(String relativePath) {
this.relativePath = relativePath;
this.content = emptyStream();
this.content = InputStream.nullInputStream();
}

public String getRelativePath() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@
import java.nio.file.Path;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Vector;
import java.util.stream.Collectors;
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;
import org.jboss.as.repository.logging.DeploymentRepositoryLogger;

Expand Down Expand Up @@ -85,8 +86,9 @@ public static boolean isEachHexHashInTable(String s) {
// WFLY-6018, check each char is in table, otherwise, there will be StringIndexOutOfBoundsException due to illegal char
char[] array = s.toCharArray();
for (char c : array) {
if (Arrays.binarySearch(table, c) < 0)
if (Arrays.binarySearch(table, c) < 0) {
return false;
}
}
return true;
}
Expand Down Expand Up @@ -123,29 +125,16 @@ private static InputStream getRecursiveContentStream(Path path) {
}
} else if (Files.isDirectory(path)) {
try {
Vector<InputStream> v = new Vector<>();
final List<InputStream> v = new ArrayList<>();
v.add(new ByteArrayInputStream(path.getFileName().toString().getBytes(StandardCharsets.UTF_8)));
try(Stream<Path> paths = Files.list(path)) {
v.addAll(paths.sorted((Path path1, Path path2) -> path1.compareTo(path2)).map(p -> getRecursiveContentStream(p)).collect(Collectors.toList()));
paths.sorted((Path path1, Path path2) -> path1.compareTo(path2)).map(p -> getRecursiveContentStream(p)).forEach(p -> v.add(p));
}
return new SequenceInputStream(v.elements());
return new SequenceInputStream(Collections.enumeration(v));
} catch (IOException ex) {
throw DeploymentRepositoryLogger.ROOT_LOGGER.hashingError(ex, path);
}
}
return emptyStream();
}

/**
* Create an empty non-null) stream.
* @return an empty non-null stream.
*/
public static InputStream emptyStream() {
return new InputStream() {
@Override
public int read() throws IOException {
return -1;
}
};
return InputStream.nullInputStream();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.jboss.as.repository.HashUtil.emptyStream;
import static org.jboss.as.repository.PathUtil.deleteRecursively;
import static org.junit.Assert.assertFalse;
import static org.hamcrest.MatcherAssert.assertThat;
Expand Down Expand Up @@ -255,7 +254,7 @@ private byte[] createContentArchive() throws IOException {
entry = new ZipEntry("test/empty-file.txt");
entry.setLastModifiedTime(time);
out.putNextEntry(entry);
try (InputStream in = HashUtil.emptyStream()) {
try (InputStream in = InputStream.nullInputStream()) {
StreamUtils.copyStream(in, out);
}
out.closeEntry();
Expand Down Expand Up @@ -344,7 +343,7 @@ public void testListContents() throws Exception {
}
assertThat(contents.size(), is(2));
assertThat(contents, CoreMatchers.hasItems("test.jsp", "testfile.xhtml"));
hash = repository.addContentToExploded(hash, Collections.singletonList(new ExplodedContent("test/empty-file.txt", emptyStream())), true);
hash = repository.addContentToExploded(hash, Collections.singletonList(new ExplodedContent("test/empty-file.txt", InputStream.nullInputStream())), true);
hash = repository.addContentToExploded(hash, Collections.singletonList(new ExplodedContent("empty-dir", null)), true);
List<String> result = new ArrayList<>();
for (ContentRepositoryElement repositoryElement : repository.listContent(hash, "", ContentFilter.Factory.createContentFilter(- 1, false))) {
Expand Down Expand Up @@ -382,7 +381,7 @@ public void testListArchiveContents() throws Exception {
}
assertThat(contents.size(), is(5));
assertThat(contents, CoreMatchers.hasItems("test.jsp", "testfile.xhtml", "test/empty-file.txt", "test/", "empty-dir/"));
hash = repository.addContentToExploded(hash, Collections.singletonList(new ExplodedContent("test/empty-file.txt", emptyStream())), true);
hash = repository.addContentToExploded(hash, Collections.singletonList(new ExplodedContent("test/empty-file.txt", InputStream.nullInputStream())), true);
hash = repository.addContentToExploded(hash, Collections.singletonList(new ExplodedContent("empty-dir", null)), true);
List<String> result = new ArrayList<>();
for (ContentRepositoryElement repositoryElement : repository.listContent(hash, "", ContentFilter.Factory.createContentFilter(1, false))) {
Expand Down

0 comments on commit 3e12aaa

Please sign in to comment.