8342086: FileInputStream.available() fails with "Incorrect function" for "nul" path (win)

Reviewed-by: alanb
This commit is contained in:
Brian Burkhalter 2024-12-03 16:19:51 +00:00
parent 60bd73a595
commit 3eaa7615cd
2 changed files with 28 additions and 23 deletions

View File

@ -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;

View File

@ -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");
}
}