From 3eaa7615cd7dc67eb78fb0a8f89d4e6662a0db37 Mon Sep 17 00:00:00 2001 From: Brian Burkhalter Date: Tue, 3 Dec 2024 16:19:51 +0000 Subject: [PATCH] 8342086: FileInputStream.available() fails with "Incorrect function" for "nul" path (win) Reviewed-by: alanb --- .../windows/native/libjava/io_util_md.c | 12 +----- .../java/io/FileInputStream/Available.java | 39 ++++++++++++------- 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/src/java.base/windows/native/libjava/io_util_md.c b/src/java.base/windows/native/libjava/io_util_md.c index 4709bbbae24..bae9803a564 100644 --- a/src/java.base/windows/native/libjava/io_util_md.c +++ b/src/java.base/windows/native/libjava/io_util_md.c @@ -342,16 +342,8 @@ handleNonSeekAvailable(FD fd, long *pbytes) { return FALSE; } - if (! PeekNamedPipe(han, NULL, 0, NULL, pbytes, NULL)) { - /* PeekNamedPipe fails when at EOF. In that case we - * simply make *pbytes = 0 which is consistent with the - * behavior we get on Solaris when an fd is at EOF. - * The only alternative is to raise and Exception, - * which isn't really warranted. - */ - if (GetLastError() != ERROR_BROKEN_PIPE) { - return FALSE; - } + if (!PeekNamedPipe(han, NULL, 0, NULL, pbytes, NULL)) { + // If PeekNamedPipe fails, set the number of available bytes to zero. *pbytes = 0; } return TRUE; diff --git a/test/jdk/java/io/FileInputStream/Available.java b/test/jdk/java/io/FileInputStream/Available.java index 8759d8d1d27..e5b5b8825d3 100644 --- a/test/jdk/java/io/FileInputStream/Available.java +++ b/test/jdk/java/io/FileInputStream/Available.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1998, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1998, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -23,25 +23,38 @@ /* * @test - * @bug 4129479 - * @summary Test if available would throw an IOException - * when the stream is closed. + * @bug 4129479 8342086 + * @summary Test that available throws an IOException if the stream is + * closed, and that available works correctly with the NUL + * device on Windows + * @run junit Available */ -import java.io.*; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledOnOs; +import org.junit.jupiter.api.condition.OS; +import static org.junit.jupiter.api.Assertions.*; public class Available { - public static void main(String args[]) throws Exception { + @Test + void throwAfterClose() throws IOException { File file = new File(System.getProperty("test.src", "."), "Available.java"); FileInputStream fis = new FileInputStream(file); fis.close(); - try { - fis.available(); - throw new Exception - ("available should throw an exception after stream is closed"); - } - catch (IOException e) { - } + assertThrows(IOException.class, () -> fis.available()); + } + + @Test + @EnabledOnOs(OS.WINDOWS) + void nulDevice() throws IOException { + File file = new File("nul"); + FileInputStream fis = new FileInputStream(file); + int n = fis.available(); + assertEquals(0, n, "available() returned non-zero value"); } }