Skip to content

Commit

Permalink
Removed hacks for unknown size of the input stream
Browse files Browse the repository at this point in the history
Signed-off-by: David Matějček <david.matejcek@omnifish.ee>
  • Loading branch information
dmatej committed Feb 20, 2024
1 parent 6595491 commit e426d26
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ public static void captureLogAndCloseClient(final TestInfo testInfo) throws Exce
ensureWritableDir(reportDir);

File reportFile = new File(reportDir, testInfo.getTestClass().orElseThrow().getName() + "-server.log");
FileUtils.copy(response.readEntity(InputStream.class), reportFile, Long.MAX_VALUE);
try (InputStream readEntity = response.readEntity(InputStream.class)) {
FileUtils.copy(readEntity, reportFile);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.Collection;
import java.util.Locale;
import java.util.Set;
Expand Down Expand Up @@ -609,8 +610,10 @@ private static File relativize(File parent, File child) {
* retry count is reached.
*
* @param work the RetriableWork implementation to be run
* @return the number of retries performed; 0 indicates the work succeeded without having to retry
* @deprecated The situation usually means there's an IO leak.
* @return the number of retries performed; 0 indicates the work succeeded without having to
* retry
* @deprecated The situation usually means there's an IO leak. It can be used also on Windows OS
* when many threads are trying to access the same file.
*/
@Deprecated
private static int doWithRetry(RetriableWork work) {
Expand Down Expand Up @@ -740,18 +743,22 @@ static boolean isValidString(String s) {


/**
* FIXME: Document and write test with large zip file to see if it wouldn't be better to use
* BufferedInputStream and BufferedOutputStream.
* FIXME: Usually used with JarFile and it's entry. Add API.
* Fast method using NIO to copy data from the input to the output file, when you already do
* know the size of the input.
* <p>
* WARNING: Don't use it when you don't know the byteCount value.
*
* @param in It will be closed after processing.
* @param out Target output file.
* @param byteCount count of bytes to be transferred.
* @throws IOException
* @throws IOException if the operation failed.
* @throws IllegalArgumentException if the byte count is less then 0 or equal to
* {@link Long#MAX_VALUE} (obvious hacks)
*/
public static void copy(InputStream in, File out, long byteCount) throws IOException {
public static void copy(InputStream in, File out, long byteCount) throws IOException, IllegalArgumentException {
if (byteCount < 0 || byteCount >= Long.MAX_VALUE) {
throw new IllegalArgumentException("If you don't know the byte count, don't use this method!");
}
try (
ReadableByteChannel inputChannel = Channels.newChannel(in);
FileOutputStream output = new FileOutputStream(out)) {
Expand All @@ -760,6 +767,19 @@ public static void copy(InputStream in, File out, long byteCount) throws IOExcep
}


/**
* This method should be used instead of {@link #copy(InputStream, File, long)} if you don't
* know the size of the input stream.
*
* @param in It will NOT be closed after processing. That is caller's responsibility.
* @param out Target output file. If the file already exists, it will be overwritten!
* @throws IOException
*/
public static void copy(InputStream in, File out) throws IOException {
Files.copy(in, out.toPath(), StandardCopyOption.REPLACE_EXISTING);
}


/**
* Copies stream with internal 8K buffer.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ private File extractFile(final Payload.Part part, final String outputName) throw

if (!extractedFile.isDirectory()) {
try (InputStream is = part.getInputStream()) {
FileUtils.copy(is, extractedFile, Long.MAX_VALUE);
FileUtils.copy(is, extractedFile);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,21 @@

package org.glassfish.admin.payload;

import com.sun.enterprise.util.io.FileUtils;

import java.io.*;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.Properties;

import org.glassfish.api.admin.Payload;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2022 Contributors to the Eclipse Foundation
* Copyright (c) 2021, 2023 Contributors to the Eclipse Foundation
* Copyright (c) 2011, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -22,26 +22,27 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import org.junit.jupiter.api.BeforeAll;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.DynamicTest.stream;

/**
* @author wnevins
* @author David Matejcek
*/
public class FileUtilsTest {

@TempDir
private static File tempDir;

@BeforeAll
public static void init() throws Exception {
tempDir = Files.createTempDirectory(FileUtilsTest.class.getSimpleName()).toFile();
}

@Test
public void testMkdirsMaybe() {
Expand Down Expand Up @@ -81,34 +82,59 @@ public void testCopyDirectoriesFiles() throws Exception {
}


@Test
public void testCopyStreamWithKnownSizeToFile() throws Exception {
File outputFile = new File(tempDir, "outputFile");
File testFile = new File(FileUtilsTest.class.getResource("/adminport.xml").toURI());
long length = testFile.length();
try (FileInputStream stream = new FileInputStream(testFile)) {
FileUtils.copy(stream, outputFile, length);
assertEquals(testFile.length(), outputFile.length());
assertThrows(IOException.class, () -> stream.available());
}
// do that once again to verify that the file was not appended or the operation blocked.
try (FileInputStream stream = new FileInputStream(testFile)) {
FileUtils.copy(stream, outputFile, length);
assertEquals(testFile.length(), outputFile.length());
assertThrows(IOException.class, () -> stream.available());
}
}


@Test
public void testCopyStreamToFile() throws Exception {
File outputFile = new File(tempDir, "outputFile");
File testFile = new File(FileUtilsTest.class.getResource("/adminport.xml").toURI());
FileInputStream stream = new FileInputStream(testFile);
FileUtils.copy(stream, outputFile, Long.MAX_VALUE);
assertEquals(testFile.length(), outputFile.length());
try (FileInputStream stream = new FileInputStream(testFile)) {
FileUtils.copy(stream, outputFile);
assertEquals(testFile.length(), outputFile.length());
assertEquals(0, stream.available(), "available bytes");
}
}


@Test
public void testCopyFileStreamToFileStream() throws Exception {
File outputFile = new File(tempDir, "outputFile");
FileOutputStream output = new FileOutputStream(outputFile);
File testFile = new File(FileUtilsTest.class.getResource("/adminport.xml").toURI());
FileInputStream inputStream = new FileInputStream(testFile);
FileUtils.copy(inputStream, output);
assertEquals(testFile.length(), outputFile.length());
try (FileOutputStream output = new FileOutputStream(outputFile);
FileInputStream inputStream = new FileInputStream(testFile)) {
FileUtils.copy(inputStream, output);
assertEquals(testFile.length(), outputFile.length());
assertEquals(0, inputStream.available(), "available bytes");
}
}


@Test
public void testCopyCLStreamToStream() throws Exception {
File outputFile = new File(tempDir, "outputFile");
BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(outputFile));
File testFile = new File(FileUtilsTest.class.getResource("/adminport.xml").toURI());
InputStream inputStream = new BufferedInputStream(FileUtilsTest.class.getResourceAsStream("/adminport.xml"));
FileUtils.copy(inputStream, output);
assertEquals(testFile.length(), outputFile.length());
try (BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(outputFile));
InputStream inputStream = new BufferedInputStream(FileUtilsTest.class.getResourceAsStream("/adminport.xml"))) {
FileUtils.copy(inputStream, output);
assertEquals(testFile.length(), outputFile.length());
assertEquals(0, inputStream.available(), "available bytes");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ private void loadOutbound(Outbound outbound, File outboundFile) throws IOExcepti
Part part = parts.next();
File sourceFile = File.createTempFile("source", "", topDir);
try (InputStream inputStream = part.getInputStream()) {
FileUtils.copy(inputStream, sourceFile, Long.MAX_VALUE);
FileUtils.copy(inputStream, sourceFile);
}
outbound.addPart(part.getContentType(), part.getName(), part.getProperties(), new FileInputStream(sourceFile));
}
Expand Down

0 comments on commit e426d26

Please sign in to comment.