Question about the Sonar rule java:S2093

Question about the Sonar rule java:S2093

  • Operating system: Windows 10
  • SonarQube for IntelliJ plugin version: 10.16.1.80464
  • IntelliJ version: 2024.3.3
  • Programming language you’re coding in: Java 8
  • Is connected mode used: No

And a thorough description of the problem / question:
How can I change these code lines below to obey the rule, when an OutputStream object can be null?


package cn.rapcao.test;

import cn.hutool.core.io.IoUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.function.Supplier;

/**
 * Question about the Sonar rule java:S2093
 *
 * @author RapCao
 */
@Slf4j
public class MainTest {

	public static void main(String[] args) throws IOException {
		final Supplier<InputStream> supplier = new InputStreamSupplier();

		InputStream inputStream = null;
		ByteArrayOutputStream outputStream = null;
		final byte[] result;

		// QUESTION POINT: How can I change these codes below to obey the rule?
		try {
			// 1. Get an InputStream object from a supplier object, can be null.
			inputStream = supplier.get();

			if (inputStream == null) {
				// 2. Return an empty array if the InputStream object is null.
				result = new byte[0];
			} else {
				// 3. Otherwise, copy the content from the InputStream object to a byte Array.
				outputStream = new ByteArrayOutputStream();

				IoUtil.copy(inputStream, outputStream);
				outputStream.flush();

				result = outputStream.toByteArray();
			}
		} catch (IOException e) {
			log.error(e.getMessage(), e);
			throw e;
		} finally {
			if (inputStream != null) {
				try {
					inputStream.close();
				} catch (IOException e) {
					log.warn(e.getMessage(), e);
				}
			}
			if (outputStream != null) {
				try {
					outputStream.close();
				} catch (IOException e) {
					log.warn(e.getMessage(), e);
				}
			}
		}

		log.info(Arrays.toString(result));
	}

	public static class InputStreamSupplier implements Supplier<InputStream> {

		@Override
		public InputStream get() {
			try {
				return Files.newInputStream(Paths.get("/path/to/file"));
			} catch (IOException e) {
				return null;
			}
		}
	}
}

Hello @cxz1023447732 thanks for your feedback.

Have you tried with this approach ?

    public static void main(String[] args) throws IOException {
        final Supplier<InputStream> supplier = new InputStreamSupplier();

       final byte[] result;

        // QUESTION POINT: How can I change these codes below to obey the rule?
        try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
             InputStream inputStream = supplier.get()) {
           if (inputStream == null) {
                // 2. Return an empty array if the InputStream object is null.
                result = new byte[0];
            } else {
                IoUtil.copy(inputStream, outputStream);
                outputStream.flush();

                result = outputStream.toByteArray();
            }
        } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw e;
        } 

        log.info(Arrays.toString(result));
    }