1/*
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 6856817
27 * @summary optimize the Writer.append(CharSequence) method
28 */
29import java.io.File;
30import java.io.FileOutputStream;
31import java.io.FileReader;
32import java.io.IOException;
33import java.io.OutputStream;
34import java.io.OutputStreamWriter;
35import java.nio.ByteBuffer;
36import java.nio.CharBuffer;
37
38/**
39 *
40 * @author vyom.tewari@oacle.com
41 */
42public class Bug6856817 {
43
44    private static final String str = "This is just a test string that i am using it to test the CharBuffer.append(CharSequence csq)  for little bit performance improvement.";
45    private static final int BUF_SIZE = 1024;
46
47    /**
48     *
49     * @param args
50     */
51    public static void main(String args[]) {
52        CharBuffer charBuffer = CharBuffer.allocate(BUF_SIZE);
53        File file = new File("temp.txt");
54        file.deleteOnExit();
55
56        charBuffer.put(str);
57        charBuffer.flip();
58        checkFileContent(charBuffer, file, str);
59        charBuffer.position(10);
60        checkFileContent(charBuffer, file, str.substring(10));
61        charBuffer.position(charBuffer.limit());
62        checkFileContent(charBuffer, file, str.substring(charBuffer.limit()));
63
64        char arr[] = new char[BUF_SIZE];
65        charBuffer = CharBuffer.wrap(arr);
66        charBuffer.put(str);
67        charBuffer.flip();
68        checkFileContent(charBuffer, file, str);
69        charBuffer.position(10);
70        checkFileContent(charBuffer, file, str.substring(10));
71        charBuffer.position(charBuffer.limit());
72        checkFileContent(charBuffer, file, str.substring(charBuffer.limit()));
73
74        char secArr[] = new char[BUF_SIZE];
75        charBuffer = CharBuffer.wrap(secArr);
76        charBuffer.put(str);
77        charBuffer.position(5);
78        charBuffer.limit(str.length() - 7);
79        charBuffer = charBuffer.slice();
80        checkFileContent(charBuffer, file, str.substring(5, (str.length() - 7)));
81        charBuffer.position(10);
82        checkFileContent(charBuffer, file, str.substring(15, (str.length() - 7)));
83        charBuffer.position(charBuffer.limit());
84        checkFileContent(charBuffer, file, str.substring(charBuffer.limit()));
85
86        charBuffer = ByteBuffer.allocate(BUF_SIZE).asCharBuffer();
87        charBuffer.put(str);
88        charBuffer.flip();
89        checkFileContent(charBuffer, file, str);
90        charBuffer.position(10);
91        checkFileContent(charBuffer, file, str.substring(10));
92        charBuffer.position(charBuffer.limit());
93        checkFileContent(charBuffer, file, str.substring(charBuffer.limit()));
94
95        charBuffer = ByteBuffer.allocateDirect(1024).asCharBuffer();
96        charBuffer.put(str);
97        charBuffer.flip();
98        checkFileContent(charBuffer, file, str);
99        charBuffer.position(10);
100        checkFileContent(charBuffer, file, str.substring(10));
101        charBuffer.position(charBuffer.limit());
102        checkFileContent(charBuffer, file, str.substring(charBuffer.limit()));
103    }
104
105    private static void checkFileContent(CharBuffer charBuffer, File file, String expectedValue) {
106        OutputStreamWriter writer = null;
107        FileReader reader = null;
108        int position, limit;
109        position = charBuffer.position();
110        limit = charBuffer.limit();
111        try {
112            OutputStream outputStream = new FileOutputStream(file);
113            writer = new OutputStreamWriter(outputStream);
114            writer.append(charBuffer);
115            writer.close();
116            if (!isEqual(position, charBuffer.position())) {
117                System.out.println(": failed");
118                throw new RuntimeException("buffer position before write: " + position + " and position after write: " + charBuffer.position());
119            }
120            if (!isEqual(limit, charBuffer.limit())) {
121                System.out.println(": failed");
122                throw new RuntimeException("buffer limit before write: " + limit + " and limit after write: " + charBuffer.limit());
123            }
124            reader = new FileReader(file);
125            char arr[] = new char[BUF_SIZE];
126            int byteRead = reader.read(arr);
127            if (byteRead != -1) {
128                String stringRead = new String(arr, 0, byteRead);
129                if (expectedValue.equals(stringRead)) {
130                    System.out.println(": passed");
131                } else {
132                    System.out.println(": failed");
133                    throw new RuntimeException("expected :" + expectedValue + " and got:" + stringRead);
134                }
135            }
136        } catch (IOException ex) {
137            ex.printStackTrace();
138            throw new RuntimeException(ex);
139        } finally {
140            try {
141                if (writer != null) {
142                    writer.close();
143                }
144                if (reader != null) {
145                    reader.close();
146                }
147            } catch (IOException ex) {
148                throw new RuntimeException(ex);
149            }
150        }
151    }
152
153    private static boolean isEqual(final int first, final int second) {
154        return (first == second);
155    }
156}
157