Path: blob/master/test/jdk/sun/nio/ch/TempBuffer.java
41152 views
/*1* Copyright (c) 2002, 2010, 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*/2223/* @test24* @bug 4738257 485329525* @summary Test Util.getTemporaryBuffer26*/2728import java.io.*;29import java.nio.channels.*;30import java.nio.*;3132public class TempBuffer {3334private static final int SIZE = 4000;3536private static final int ITERATIONS = 10;3738public static void main(String[] args) throws Exception {39for (int i=0; i<ITERATIONS; i++)40testTempBufs();41}4243private static void testTempBufs() throws Exception {44Pipe pipe = Pipe.open();45Pipe.SourceChannel sourceChannel = pipe.source();46final Pipe.SinkChannel sinkChannel = pipe.sink();4748Thread writerThread = new Thread() {49public void run() {50try {51OutputStream out = Channels.newOutputStream(sinkChannel);52File blah = File.createTempFile("blah1", null);53blah.deleteOnExit();54TempBuffer.initTestFile(blah);55RandomAccessFile raf = new RandomAccessFile(blah, "rw");56FileChannel fc = raf.getChannel();57try {58fc.transferTo(0, SIZE, Channels.newChannel(out));59} finally {60fc.close();61}62out.flush();63} catch (IOException ioe) {64throw new RuntimeException(ioe);65}66}67};6869writerThread.start();7071InputStream in = Channels.newInputStream(sourceChannel);72File blah = File.createTempFile("blah2", null);73blah.deleteOnExit();74RandomAccessFile raf = new RandomAccessFile(blah, "rw");75FileChannel fc = raf.getChannel();76try {77raf.setLength(SIZE);78fc.transferFrom(Channels.newChannel(in), 0, SIZE);79} finally {80fc.close();81}8283sourceChannel.close();84sinkChannel.close();85blah.delete();86}8788private static void initTestFile(File blah) throws IOException {89FileOutputStream fos = new FileOutputStream(blah);90BufferedWriter awriter91= new BufferedWriter(new OutputStreamWriter(fos, "8859_1"));9293for(int i=0; i<4000; i++) {94String number = new Integer(i).toString();95for (int h=0; h<4-number.length(); h++)96awriter.write("0");97awriter.write(""+i);98awriter.newLine();99}100awriter.flush();101awriter.close();102}103}104105106