KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > output2 > HeapStorage


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.core.output2;
20
21 import java.io.IOException JavaDoc;
22 import java.nio.ByteBuffer JavaDoc;
23
24 /**
25  * Heap based implementation of the Storage interface, over a byte array.
26  *
27  */

28 class HeapStorage implements Storage {
29     private boolean closed = true;
30     private byte[] bytes = new byte[2048];
31     private int size = 0;
32
33     public Storage toFileMapStorage() throws IOException JavaDoc {
34         FileMapStorage result = new FileMapStorage();
35         result.write(getReadBuffer(0, size), false);
36         return result;
37     }
38
39     public ByteBuffer JavaDoc getReadBuffer(int start, int length) throws IOException JavaDoc {
40         return ByteBuffer.wrap(bytes, start, Math.min(length, bytes.length - start));
41     }
42
43     public ByteBuffer JavaDoc getWriteBuffer(int length) throws IOException JavaDoc {
44         return ByteBuffer.allocate(length);
45     }
46
47     public synchronized int write(ByteBuffer JavaDoc buf, boolean addNewLine) throws IOException JavaDoc {
48         closed = false;
49         int oldSize = size;
50         size += buf.limit() + ( addNewLine ? OutWriter.lineSepBytes.length : 0);
51         if (size > bytes.length) {
52             byte[] oldBytes = bytes;
53             bytes = new byte[Math.max (oldSize * 2, (buf.limit() * 2) + oldSize)];
54             System.arraycopy (oldBytes, 0, bytes, 0, oldSize);
55         }
56         buf.flip();
57         buf.get(bytes, oldSize, buf.limit());
58         if (addNewLine) {
59             System.arraycopy (OutWriter.lineSepBytes, 0, bytes, size - OutWriter.lineSepBytes.length, OutWriter.lineSepBytes.length);
60         }
61         return oldSize;
62     }
63
64     public synchronized void dispose() {
65         bytes = new byte[0];
66         size = 0;
67     }
68
69     public synchronized int size() {
70         return size;
71     }
72
73     public void flush() throws IOException JavaDoc {
74         //N/A
75
}
76
77     public void close() throws IOException JavaDoc {
78         closed = true;
79     }
80
81     public boolean isClosed() {
82         return closed; //To change body of implemented methods use File | Settings | File Templates.
83
}
84 }
85
Popular Tags