This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 206233
Collapse All | Expand All

(-)a/db.dataview/src/org/netbeans/modules/db/dataview/util/FileBackedClob.java (-44 / +21 lines)
Lines 66-78 Link Here
66
 * Currently the following function are not implemented:
66
 * Currently the following function are not implemented:
67
 * - all position functions
67
 * - all position functions
68
 * - getCharacterStream(long pos, long length)
68
 * - getCharacterStream(long pos, long length)
69
 * - setAsciiStream
70
 * - getAsciiStream
69
 * 
71
 * 
70
 * @author mblaesing
72
 * @author mblaesing
71
 */
73
 */
72
public class FileBackedClob implements Clob {
74
public class FileBackedClob implements Clob {
73
75
74
    boolean freed = false;
76
    private boolean freed = false;
75
    File backingFile;
77
    private File backingFile;
76
78
77
    public FileBackedClob() throws SQLException {
79
    public FileBackedClob() throws SQLException {
78
        try {
80
        try {
Lines 126-140 Link Here
126
    @Override
128
    @Override
127
    public void truncate(long len) throws SQLException {
129
    public void truncate(long len) throws SQLException {
128
        checkFreed();
130
        checkFreed();
129
        FileOutputStream fos = null;
131
        RandomAccessFile raf = null;
130
        try {
132
        try {
131
            fos = new FileOutputStream(backingFile);
133
            raf = new RandomAccessFile(backingFile, "rw");
132
            fos.close();
134
            raf.setLength(len * 4);
133
        } catch (IOException ex) {
135
        } catch (IOException ex) {
134
            throw new SQLException(ex);
136
            throw new SQLException(ex);
135
        } finally {
137
        } finally {
136
            try {
138
            try {
137
                fos.close();
139
                raf.close();
138
            } catch (IOException ex) {
140
            } catch (IOException ex) {
139
                Exceptions.printStackTrace(ex);
141
                Exceptions.printStackTrace(ex);
140
            }
142
            }
Lines 177-182 Link Here
177
            r = new InputStreamReader(new FileInputStream(backingFile), "UTF32");
179
            r = new InputStreamReader(new FileInputStream(backingFile), "UTF32");
178
            r.skip(pos - 1);
180
            r.skip(pos - 1);
179
            CharBuffer c = CharBuffer.allocate(length);
181
            CharBuffer c = CharBuffer.allocate(length);
182
            r.read(c);
183
            c.rewind();
180
            return c.toString();
184
            return c.toString();
181
        } catch (IOException ex) {
185
        } catch (IOException ex) {
182
            throw new SQLException(ex);
186
            throw new SQLException(ex);
Lines 207-219 Link Here
207
    @Override
211
    @Override
208
    public InputStream getAsciiStream() throws SQLException {
212
    public InputStream getAsciiStream() throws SQLException {
209
        throw new UnsupportedOperationException("Not supported yet.");
213
        throw new UnsupportedOperationException("Not supported yet.");
210
//        InputStream fis;
211
//        try {
212
//            fis = new FileInputStream(backingFile);
213
//        } catch (FileNotFoundException ex) {
214
//            throw new SQLException(ex);
215
//        }
216
//        return fis;
217
    }
214
    }
218
215
219
    @Override
216
    @Override
Lines 233-260 Link Here
233
230
234
    @Override
231
    @Override
235
    public int setString(long pos, String str, int offset, int len) throws SQLException {
232
    public int setString(long pos, String str, int offset, int len) throws SQLException {
236
        FileReader fr = null;
233
        checkFreed();
234
        checkPos(pos);
235
        Writer w = null;
237
        try {
236
        try {
238
            checkFreed();
237
            w = setCharacterStream(pos);
239
            checkPos(pos);
238
            w.write(str.substring(offset, Math.min(offset + len, str.length())));
240
            StringBuilder sb = new StringBuilder();
241
            int read;
242
            char[] buffer = new char[1024];
243
            fr = new FileReader(backingFile);
244
            while ((read = fr.read(buffer)) > 0) {
245
                sb.append(buffer, 0, read);
246
            }
247
            fr.close();
248
            sb.replace((int) pos - 1, (int) Math.min(sb.length(), pos + len), str.substring(offset, offset + len));
249
            Writer w = setCharacterStream(1);
250
            w.write(sb.toString());
251
            w.close();
252
            return len;
239
            return len;
253
        } catch (IOException ex) {
240
        } catch (IOException ex) {
254
            throw new SQLException(ex);
241
            throw new SQLException(ex);
255
        } finally {
242
        } finally {
256
            try {
243
            try {
257
                fr.close();
244
                w.close();
258
            } catch (IOException ex) {
245
            } catch (IOException ex) {
259
                Exceptions.printStackTrace(ex);
246
                Exceptions.printStackTrace(ex);
260
            }
247
            }
Lines 266-285 Link Here
266
        checkFreed();
253
        checkFreed();
267
        checkPos(pos);
254
        checkPos(pos);
268
        throw new UnsupportedOperationException("Not supported yet.");
255
        throw new UnsupportedOperationException("Not supported yet.");
269
//
270
//        try {
271
//            final RandomAccessFile raf = new RandomAccessFile(backingFile, "rw");
272
//            try {
273
//                raf.seek(pos - 1);
274
//            } catch (IOException ex) {
275
//                raf.close();
276
//                throw new SQLException(ex);
277
//            }
278
//
279
//            return new RandomAccessOutputStream(raf);
280
//        } catch (IOException ex) {
281
//            throw new SQLException(ex);
282
//        }
283
    }
256
    }
284
257
285
    @Override
258
    @Override
Lines 311-317 Link Here
311
    }
284
    }
312
285
313
    protected void finalize() throws Throwable {
286
    protected void finalize() throws Throwable {
314
        backingFile.delete();
287
        free();
315
        super.finalize();
288
        super.finalize();
316
    }
289
    }
290
291
    File getBackingFile() {
292
        return backingFile;
293
    }
317
}
294
}
(-)a/db.dataview/test/unit/src/org/netbeans/modules/db/dataview/util/FileBackedClobTest.java (+287 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2011 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.modules.db.dataview.util;
43
44
import java.io.CharArrayReader;
45
import java.io.IOException;
46
import org.openide.util.Exceptions;
47
import java.io.Reader;
48
import java.io.Writer;
49
import org.junit.After;
50
import org.junit.AfterClass;
51
import org.junit.Before;
52
import org.junit.BeforeClass;
53
import org.junit.Test;
54
import static org.junit.Assert.*;
55
56
/**
57
 *
58
 * @author mblaesing
59
 */
60
public class FileBackedClobTest {
61
    private static char[] testCase1;
62
    private static char[] testCase2;
63
    private static char[] testCase3;
64
65
    private static void assertCharacterStreamEquals(Reader is1, Reader is2) {
66
        try {
67
            long position = 0;
68
            while (true) {
69
                int input1 = is1.read();
70
                int input2 = is2.read();
71
                if (input1 != input2) {
72
                    throw new AssertionError("Reader differ at position: " + Long.toString(position));
73
                }
74
                if (input1 == -1 || input2 == -1) {
75
                    return;
76
                }
77
                position++;
78
            }
79
        } catch (IOException ex) {
80
            throw new AssertionError(ex);
81
        } finally {
82
            try {
83
                is1.close();
84
            } catch (IOException ex) {
85
                Exceptions.printStackTrace(ex);
86
            }
87
            try {
88
                is2.close();
89
            } catch (IOException ex) {
90
                Exceptions.printStackTrace(ex);
91
            }
92
        }
93
    }
94
95
    @BeforeClass
96
    public static void setUpClass() throws Exception {
97
        char[] testPattern = "Testäöüß".toCharArray();
98
        int testLength = testPattern.length;
99
        testCase1 = new char[10];
100
        testCase2 = new char[1024];
101
        testCase3 = new char[1024 * 1024];
102
        for (int i = 0; i < testCase1.length; i++) {
103
            testCase1[i] = testPattern[ i % testLength];
104
        }
105
        for (int i = 0; i < testCase2.length; i++) {
106
            testCase2[i] = testPattern[ i % testLength];
107
        }
108
        for (int i = 0; i < testCase3.length; i++) {
109
            testCase3[i] = testPattern[ i % testLength];
110
        }
111
    }
112
113
    
114
    public FileBackedClobTest() {
115
    }
116
117
    @AfterClass
118
    public static void tearDownClass() throws Exception {
119
        testCase1 = null;
120
        testCase2 = null;
121
        testCase3 = null;
122
    }
123
    
124
    @Before
125
    public void setUp() {
126
    }
127
    
128
    @After
129
    public void tearDown() {
130
    }
131
132
    @Test
133
    public void testLength() throws Exception {
134
        FileBackedClob c;
135
        c = new FileBackedClob(new CharArrayReader(testCase1));
136
        assertEquals(c.length(), testCase1.length);
137
        assertEquals(c.getBackingFile().length(), testCase1.length * 4);
138
        c.free();
139
        c = new FileBackedClob(new String(testCase2));
140
        assertEquals(c.length(), testCase2.length);
141
        assertEquals(c.getBackingFile().length(), testCase2.length * 4);
142
        c.free();
143
        c = new FileBackedClob(new CharArrayReader(testCase3));
144
        assertEquals(c.length(), testCase3.length);
145
        assertEquals(c.getBackingFile().length(), testCase3.length * 4);
146
        c.free();
147
    }
148
149
    @Test
150
    public void testTruncate() throws Exception {
151
        FileBackedClob c;
152
        c = new FileBackedClob(new CharArrayReader(testCase1));
153
        c.truncate(5);
154
        assertEquals(c.length(), 5);
155
        assertEquals(c.getBackingFile().length(), 5 * 4);
156
        c.free();
157
        c = new FileBackedClob(new String(testCase2));
158
        c.truncate(42);
159
        assertEquals(c.length(), 42);
160
        assertEquals(c.getBackingFile().length(), 42 * 4);
161
        c.free();
162
        c = new FileBackedClob(new CharArrayReader(testCase3));
163
        c.truncate(1024);
164
        assertEquals(c.length(), 1024);
165
        assertEquals(c.getBackingFile().length(), 1024 * 4);
166
        c.free();
167
    }
168
169
    @Test
170
    public void testGetSubString() throws Exception {
171
        FileBackedClob c;
172
        char[] referenceChars = new char[5];
173
        System.arraycopy(testCase1, 5, referenceChars, 0, 5);
174
        String reference = new String(referenceChars);
175
        c = new FileBackedClob(new CharArrayReader(testCase1));
176
        assertEquals(reference, c.getSubString(6, 5));
177
        c.free();
178
        c = new FileBackedClob(new String(testCase2));
179
        assertEquals( reference, c.getSubString(6, 5));
180
        c.free();
181
        c = new FileBackedClob(new CharArrayReader(testCase3));
182
        assertEquals(reference, c.getSubString(6, 5));
183
        c.free();
184
    }
185
186
    @Test
187
    public void testGetCharacterStream_0args() throws Exception {
188
        FileBackedClob c;
189
        c = new FileBackedClob(new CharArrayReader(testCase1));
190
        assertCharacterStreamEquals(c.getCharacterStream(), new CharArrayReader(testCase1));
191
        c.free();
192
        c = new FileBackedClob(new String(testCase2));
193
        assertCharacterStreamEquals(c.getCharacterStream(), new CharArrayReader(testCase2));
194
        c.free();
195
        c = new FileBackedClob(new CharArrayReader(testCase3));
196
        assertCharacterStreamEquals(c.getCharacterStream(), new CharArrayReader(testCase3));
197
        c.free();
198
    }
199
200
    @Test
201
    public void testSetString_long_String() throws Exception {
202
        FileBackedClob b;
203
        char[] test1 = "test".toCharArray();
204
        char[] test2 = "test0123456789".toCharArray();
205
        char[] firstPartReference = new char[testCase2.length - test1.length - 4];
206
        char[] secondPartReference = new char[4];
207
        System.arraycopy(testCase2, 0, firstPartReference, 0, testCase2.length - test1.length - 4);
208
        System.arraycopy(testCase2, testCase2.length - 4 - 1, secondPartReference, 0, 4);
209
        b = new FileBackedClob(new CharArrayReader(testCase2));
210
        b.setString(testCase2.length - test1.length - 4 + 1, new String(test1));
211
        assertEquals(new String(firstPartReference), b.getSubString(1, testCase2.length - test1.length - 4));
212
        assertEquals(new String(secondPartReference), b.getSubString(testCase2.length - 4, 4));
213
        assertEquals(new String(test1), b.getSubString(testCase2.length - 4 - test1.length + 1, test1.length));
214
        assertEquals(b.length(), 1024);
215
        b.setString(testCase2.length - test1.length - 4 + 1, new String(test2));
216
        assertEquals(new String(firstPartReference), b.getSubString(1, testCase2.length - test1.length - 4));
217
        assertEquals(b.length(), 1024 - test1.length - 4 + test2.length);
218
        assertEquals(new String(test2), b.getSubString(b.length() - test2.length + 1, test2.length));
219
        b.free();
220
    }
221
222
    @Test
223
    public void testSetString_4args() throws Exception {
224
        FileBackedClob b;
225
        char[] test1 = "test".toCharArray();
226
        char[] test2 = "01test23456789".toCharArray();
227
        char[] firstPartReference = new char[testCase2.length - test1.length - 4];
228
        char[] secondPartReference = new char[4];
229
        System.arraycopy(testCase2, 0, firstPartReference, 0, testCase2.length - test1.length - 4);
230
        System.arraycopy(testCase2, testCase2.length - 4 - 1, secondPartReference, 0, 4);
231
        b = new FileBackedClob(new CharArrayReader(testCase2));
232
        b.setString(testCase2.length - test1.length - 4 + 1, new String(test2), 2, 4);
233
        assertEquals(new String(firstPartReference), b.getSubString(1, testCase2.length - test1.length - 4));
234
        assertEquals(new String(secondPartReference), b.getSubString(testCase2.length - 4, 4));
235
        assertEquals(new String(test1), b.getSubString(testCase2.length - 4 - test1.length + 1, test1.length));
236
        assertEquals(b.length(), 1024);
237
        b.free();
238
    }
239
240
    @Test
241
    public void testSetCharacterStream() throws Exception {
242
        FileBackedClob b;
243
        char[] test1 = "test".toCharArray();
244
        char[] test2 = "test0123456789".toCharArray();
245
        char[] firstPartReference = new char[testCase2.length - test1.length - 4];
246
        char[] secondPartReference = new char[4];
247
        System.arraycopy(testCase2, 0, firstPartReference, 0, testCase2.length - test1.length - 4);
248
        System.arraycopy(testCase2, testCase2.length - 4 - 1, secondPartReference, 0, 4);
249
        b = new FileBackedClob(new CharArrayReader(testCase2));
250
        Writer os = b.setCharacterStream(testCase2.length - test1.length - 4 + 1);
251
        os.write(test1);
252
        os.close();
253
        assertEquals(new String(firstPartReference), b.getSubString(1, testCase2.length - test1.length - 4));
254
        assertEquals(new String(secondPartReference), b.getSubString(testCase2.length - 4, 4));
255
        assertEquals(new String(test1), b.getSubString(testCase2.length - 4 - test1.length + 1, test1.length));
256
        assertEquals(b.length(), 1024);
257
        os = b.setCharacterStream(testCase2.length - test1.length - 4 + 1);
258
        os.write(test2);
259
        os.close();
260
        assertEquals(new String(firstPartReference), b.getSubString(1, testCase2.length - test1.length - 4));
261
        assertEquals(b.length(), 1024 - test1.length - 4 + test2.length);
262
        assertEquals(new String(test2), b.getSubString(b.length() - test2.length + 1, test2.length));
263
        b.free();
264
    }
265
266
    @Test
267
    public void testFinalize() throws Exception {
268
        FileBackedClob b;
269
        b = new FileBackedClob(new CharArrayReader(testCase2));
270
        assertTrue(b.getBackingFile().exists());
271
        try {
272
            b.finalize();
273
        } catch (Throwable ex) {
274
            Exceptions.printStackTrace(ex);
275
        }
276
        assertFalse(b.getBackingFile().exists());
277
    }
278
    
279
    @Test
280
    public void testFree() throws Exception {
281
        FileBackedClob b;
282
        b = new FileBackedClob(new CharArrayReader(testCase2));
283
        assertTrue(b.getBackingFile().exists());
284
        b.free();
285
        assertFalse(b.getBackingFile().exists());
286
    }
287
}

Return to bug 206233