Repository: netty/netty
File: handler/src/main/java/io/netty/handler/ssl/util/KeytoolSelfSignedCertGenerator.java (Java)

Code before:
                            "-storetype", KEY_STORE_TYPE
                    )
                    .redirectErrorStream(true)
                    .start();
            try {
                if (!process.waitFor(60, TimeUnit.SECONDS)) {
                    process.destroyForcibly();
                    throw new IOException("keytool timeout");
                }
            } catch (InterruptedException e) {
                process.destroyForcibly();
                Thread.currentThread().interrupt();
                throw new InterruptedIOException();
            }

            if (process.exitValue() != 0) {
                ByteBuf buffer = Unpooled.buffer();
                try (InputStream stream = process.getInputStream()) {
                    while (true) {
                        if (buffer.writeBytes(stream, 4096) == -1) {
                            break;
                        }
                    }
                }
                String log = buffer.toString(StandardCharsets.UTF_8);
                throw new IOException("Keytool exited with status " + process.exitValue() + ": " + log);
            }

            KeyStore ks = KeyStore.getInstance(KEY_STORE_TYPE);
            try (InputStream is = Files.newInputStream(keyStore)) {
                ks.load(is, PASSWORD.toCharArray());
            }
            KeyStore.PrivateKeyEntry entry = (KeyStore.PrivateKeyEntry) ks.getEntry(
                    ALIAS, new KeyStore.PasswordProtection(PASSWORD.toCharArray()));
            builder.paths = SelfSignedCertificate.newSelfSignedCertificate(
                    builder.fqdn, entry.getPrivateKey(), (X509Certificate) entry.getCertificate());
            builder.privateKey = entry.getPrivateKey();
        } finally {
            Files.deleteIfExists(keyStore);
            Files.delete(directory);
        }
    }
}


Review comment (suggestion):
```suggestion
                ByteBuf buffer = Unpooled.buffer();
                try {
                    try (InputStream stream = process.getInputStream()) {
                        while (true) {
                            if (buffer.writeBytes(stream, 4096) == -1) {
                                break;
                            }
                        }
                    }
                    String log = buffer.toString(StandardCharsets.UTF_8);
                    throw new IOException("Keytool exited with status " + process.exitValue() + ": " + log);
                } finally {
                    buffer.release();
                }
```

Code after:
                            "-storetype", KEY_STORE_TYPE
                    )
                    .redirectErrorStream(true)
                    .start();
            try {
                if (!process.waitFor(60, TimeUnit.SECONDS)) {
                    process.destroyForcibly();
                    throw new IOException("keytool timeout");
                }
            } catch (InterruptedException e) {
                process.destroyForcibly();
                Thread.currentThread().interrupt();
                throw new InterruptedIOException();
            }

            if (process.exitValue() != 0) {
                ByteBuf buffer = Unpooled.buffer();
                try {
                    try (InputStream stream = process.getInputStream()) {
                        while (true) {
                            if (buffer.writeBytes(stream, 4096) == -1) {
                                break;
                            }
                        }
                    }
                    String log = buffer.toString(StandardCharsets.UTF_8);
                    throw new IOException("Keytool exited with status " + process.exitValue() + ": " + log);
                } finally {
                    buffer.release();
                }
            }

            KeyStore ks = KeyStore.getInstance(KEY_STORE_TYPE);
            try (InputStream is = Files.newInputStream(keyStore)) {
                ks.load(is, PASSWORD.toCharArray());
            }
            KeyStore.PrivateKeyEntry entry = (KeyStore.PrivateKeyEntry) ks.getEntry(
                    ALIAS, new KeyStore.PasswordProtection(PASSWORD.toCharArray()));
            builder.paths = SelfSignedCertificate.newSelfSignedCertificate(
                    builder.fqdn, entry.getPrivateKey(), (X509Certificate) entry.getCertificate());
            builder.privateKey = entry.getPrivateKey();
        } finally {
            Files.deleteIfExists(keyStore);
            Files.delete(directory);
        }
    }
}