|
1 | 1 | /* |
2 | | - * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2010, 2025, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
|
21 | 21 | * questions. |
22 | 22 | */ |
23 | 23 |
|
24 | | -import java.io.*; |
25 | | -import java.security.cert.*; |
| 24 | +/* |
| 25 | + * @test |
| 26 | + * @bug 6813340 |
| 27 | + * @summary X509Factory should not depend on is.available()==0 |
| 28 | + */ |
26 | 29 |
|
27 | | -class SlowStreamReader { |
| 30 | +import java.io.File; |
| 31 | +import java.io.FileInputStream; |
| 32 | +import java.io.PipedInputStream; |
| 33 | +import java.io.PipedOutputStream; |
| 34 | +import java.security.cert.CertificateFactory; |
| 35 | +import java.util.concurrent.atomic.AtomicBoolean; |
28 | 36 |
|
| 37 | +public class SlowStream { |
29 | 38 | public static void main(String[] args) throws Exception { |
30 | | - CertificateFactory factory = CertificateFactory.getInstance("X.509"); |
31 | | - if (factory.generateCertificates(System.in).size() != 5) { |
32 | | - throw new Exception("Not all certs read"); |
33 | | - } |
34 | | - } |
35 | | -} |
| 39 | + final var outputStream = new PipedOutputStream(); |
| 40 | + final var inputStream = new PipedInputStream(outputStream); |
36 | 41 |
|
37 | | -class SlowStreamWriter { |
38 | | - public static void main(String[] args) throws Exception { |
39 | | - for (int i=0; i<5; i++) { |
40 | | - FileInputStream fin = new FileInputStream(new File(new File( |
41 | | - System.getProperty("test.src", "."), "openssl"), "pem")); |
42 | | - byte[] buffer = new byte[4096]; |
43 | | - while (true) { |
44 | | - int len = fin.read(buffer); |
45 | | - if (len < 0) break; |
46 | | - System.out.write(buffer, 0, len); |
| 42 | + final var failed = new AtomicBoolean(false); |
| 43 | + |
| 44 | + final var writer = new Thread(() -> { |
| 45 | + try { |
| 46 | + for (int i = 0; i < 5; i++) { |
| 47 | + try (final var fin = new FileInputStream(new File( |
| 48 | + new File(System.getProperty("test.src", "."), |
| 49 | + "openssl"), |
| 50 | + "pem"))) { |
| 51 | + |
| 52 | + fin.transferTo(outputStream); |
| 53 | + |
| 54 | + Thread.sleep(2000); |
| 55 | + } |
| 56 | + } |
| 57 | + outputStream.close(); |
| 58 | + } catch (final Exception e) { |
| 59 | + System.out.println("Writer Thread Error: "); |
| 60 | + e.printStackTrace(System.out); |
| 61 | + failed.set(true); |
| 62 | + } |
| 63 | + }); |
| 64 | + |
| 65 | + final var reader = new Thread(() -> { |
| 66 | + try { |
| 67 | + final var factory = CertificateFactory.getInstance("X.509"); |
| 68 | + final var numOfCerts = factory.generateCertificates(inputStream).size(); |
| 69 | + if (numOfCerts != 5) { |
| 70 | + throw new Exception( |
| 71 | + String.format("Not all certs read. %d found 5 expected", numOfCerts) |
| 72 | + ); |
| 73 | + } |
| 74 | + inputStream.close(); |
| 75 | + } catch (final Exception e) { |
| 76 | + System.out.println("Reader Thread Error: "); |
| 77 | + e.printStackTrace(System.out); |
| 78 | + failed.set(true); |
47 | 79 | } |
48 | | - Thread.sleep(2000); |
| 80 | + }); |
| 81 | + |
| 82 | + writer.start(); |
| 83 | + reader.start(); |
| 84 | + |
| 85 | + writer.join(); |
| 86 | + reader.join(); |
| 87 | + |
| 88 | + if (failed.get()) { |
| 89 | + throw new RuntimeException( |
| 90 | + "The test failed, please check the reader and writer threads output" |
| 91 | + ); |
49 | 92 | } |
50 | 93 | } |
51 | 94 | } |
0 commit comments