Path: blob/master/test/jdk/java/nio/file/Files/NulDevice.java
41153 views
/*1* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.IOException;24import java.io.InputStream;25import java.io.OutputStream;26import java.nio.ByteBuffer;27import java.nio.channels.AsynchronousFileChannel;28import java.nio.channels.FileChannel;29import java.nio.file.Files;30import java.nio.file.Path;31import static java.nio.file.StandardOpenOption.*;32import java.util.concurrent.ExecutionException;3334/* @test35* @bug 826389836* @summary Verify stream and channel behavior with NUL device37* @requires os.family == "windows"38* @run main NulDevice39*/40public class NulDevice {41public static void main(String[] args)42throws ExecutionException, InterruptedException, IOException {43Path path = Path.of("nul");4445try (OutputStream os = Files.newOutputStream(path)) {46os.write(0x02);47try (InputStream is = Files.newInputStream(path);) {48if (is.available() != 0) {49throw new RuntimeException("No bytes should be available");50}51int aByte = is.read();52if (aByte != -1) {53throw new RuntimeException("Should only read -1 from NUL");54}55}56}5758try (OutputStream os = Files.newOutputStream(path, WRITE)) {59os.write(0x02);60}6162try (FileChannel ch = FileChannel.open(path, CREATE, TRUNCATE_EXISTING, WRITE)) {63byte[] bytes = "Whatever".getBytes();64ByteBuffer buf = ByteBuffer.allocate(2*bytes.length);65buf.put(bytes);66int nw = ch.write(buf);67if (nw != bytes.length) {68throw new RuntimeException("Should write " + bytes.length +69" to NUL");70}71}7273try (FileChannel ch = FileChannel.open(path, READ)) {74if (ch.size() != 0) {75throw new RuntimeException("Size should be zero");76}77ByteBuffer buf = ByteBuffer.allocate(10);78int nr = ch.read(buf);79if (nr != -1) {80throw new RuntimeException("Read returns " + nr + " not -1");81}82}8384try (AsynchronousFileChannel ch = AsynchronousFileChannel.open(path, READ, WRITE)) {85if (ch.size() != 0) {86throw new RuntimeException("Size should be zero");87}88int nw = ch.write(ByteBuffer.wrap(new byte[] {(byte)0x02}), 0L).get();89if (nw != 1) {90throw new RuntimeException("Wrote " + nw + " bytes, not one");91}92ByteBuffer buf = ByteBuffer.allocate(10);93int nr = ch.read(buf, 0L).get();94if (nr != -1) {95throw new RuntimeException("Read returns " + nr + " not -1");96}97}98}99}100101102