TempBuffer.java revision 2362
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk/*
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * Copyright (c) 2002, 2003, Oracle and/or its affiliates. All rights reserved.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk *
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * This code is free software; you can redistribute it and/or modify it
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * under the terms of the GNU General Public License version 2 only, as
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * published by the Free Software Foundation.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk *
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * This code is distributed in the hope that it will be useful, but WITHOUT
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * version 2 for more details (a copy is included in the LICENSE file that
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * accompanied this code).
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk *
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * You should have received a copy of the GNU General Public License version
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * 2 along with this work; if not, write to the Free Software Foundation,
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk *
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * or visit www.oracle.com if you need additional information or have any
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * questions.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk/* @test
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * @bug 4738257 4853295
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * @summary Test Util.getTemporaryBuffer
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenkimport java.io.*;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenkimport java.nio.channels.*;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenkimport java.nio.*;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenkpublic class TempBuffer {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk private static final int SIZE = 4000;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk private static final int ITERATIONS = 10;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk public static void main(String[] args) throws Exception {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk for (int i=0; i<ITERATIONS; i++)
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk testTempBufs();
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk private static void testTempBufs() throws Exception {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk Pipe pipe = Pipe.open();
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk Pipe.SourceChannel sourceChannel = pipe.source();
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk final Pipe.SinkChannel sinkChannel = pipe.sink();
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk Thread writerThread = new Thread() {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk public void run() {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk try {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk OutputStream out = Channels.newOutputStream(sinkChannel);
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk File blah = File.createTempFile("blah1", null);
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk blah.deleteOnExit();
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk TempBuffer.initTestFile(blah);
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk RandomAccessFile raf = new RandomAccessFile(blah, "rw");
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk FileChannel fs = raf.getChannel();
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk fs.transferTo(0, SIZE, Channels.newChannel(out));
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk out.flush();
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk } catch (IOException ioe) {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk throw new RuntimeException(ioe);
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk };
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk writerThread.start();
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk InputStream in = Channels.newInputStream(sourceChannel);
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk File blah = File.createTempFile("blah2", null);
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk blah.deleteOnExit();
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk RandomAccessFile raf = new RandomAccessFile(blah, "rw");
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk FileChannel fs = raf.getChannel();
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk raf.setLength(SIZE);
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk fs.transferFrom(Channels.newChannel(in), 0, SIZE);
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk fs.close();
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk private static void initTestFile(File blah) throws IOException {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk FileOutputStream fos = new FileOutputStream(blah);
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk BufferedWriter awriter
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk = new BufferedWriter(new OutputStreamWriter(fos, "8859_1"));
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk for(int i=0; i<4000; i++) {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk String number = new Integer(i).toString();
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk for (int h=0; h<4-number.length(); h++)
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk awriter.write("0");
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk awriter.write(""+i);
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk awriter.newLine();
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk awriter.flush();
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk awriter.close();
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk}
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk