Skip to content

Commit 3eaa761

Browse files
author
Brian Burkhalter
committedDec 3, 2024
8342086: FileInputStream.available() fails with "Incorrect function" for "nul" path (win)
Reviewed-by: alanb
1 parent 60bd73a commit 3eaa761

File tree

2 files changed

+28
-23
lines changed

2 files changed

+28
-23
lines changed
 

‎src/java.base/windows/native/libjava/io_util_md.c

+2-10
Original file line numberDiff line numberDiff line change
@@ -342,16 +342,8 @@ handleNonSeekAvailable(FD fd, long *pbytes) {
342342
return FALSE;
343343
}
344344

345-
if (! PeekNamedPipe(han, NULL, 0, NULL, pbytes, NULL)) {
346-
/* PeekNamedPipe fails when at EOF. In that case we
347-
* simply make *pbytes = 0 which is consistent with the
348-
* behavior we get on Solaris when an fd is at EOF.
349-
* The only alternative is to raise and Exception,
350-
* which isn't really warranted.
351-
*/
352-
if (GetLastError() != ERROR_BROKEN_PIPE) {
353-
return FALSE;
354-
}
345+
if (!PeekNamedPipe(han, NULL, 0, NULL, pbytes, NULL)) {
346+
// If PeekNamedPipe fails, set the number of available bytes to zero.
355347
*pbytes = 0;
356348
}
357349
return TRUE;

‎test/jdk/java/io/FileInputStream/Available.java

+26-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1998, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1998, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,25 +23,38 @@
2323

2424
/*
2525
* @test
26-
* @bug 4129479
27-
* @summary Test if available would throw an IOException
28-
* when the stream is closed.
26+
* @bug 4129479 8342086
27+
* @summary Test that available throws an IOException if the stream is
28+
* closed, and that available works correctly with the NUL
29+
* device on Windows
30+
* @run junit Available
2931
*/
3032

31-
import java.io.*;
33+
import java.io.File;
34+
import java.io.FileInputStream;
35+
import java.io.IOException;
36+
37+
import org.junit.jupiter.api.Test;
38+
import org.junit.jupiter.api.condition.EnabledOnOs;
39+
import org.junit.jupiter.api.condition.OS;
40+
import static org.junit.jupiter.api.Assertions.*;
3241

3342
public class Available {
34-
public static void main(String args[]) throws Exception {
43+
@Test
44+
void throwAfterClose() throws IOException {
3545
File file = new File(System.getProperty("test.src", "."),
3646
"Available.java");
3747
FileInputStream fis = new FileInputStream(file);
3848
fis.close();
39-
try {
40-
fis.available();
41-
throw new Exception
42-
("available should throw an exception after stream is closed");
43-
}
44-
catch (IOException e) {
45-
}
49+
assertThrows(IOException.class, () -> fis.available());
50+
}
51+
52+
@Test
53+
@EnabledOnOs(OS.WINDOWS)
54+
void nulDevice() throws IOException {
55+
File file = new File("nul");
56+
FileInputStream fis = new FileInputStream(file);
57+
int n = fis.available();
58+
assertEquals(0, n, "available() returned non-zero value");
4659
}
4760
}

1 commit comments

Comments
 (1)

openjdk-notifier[bot] commented on Dec 3, 2024

@openjdk-notifier[bot]
Please sign in to comment.