1 19 package org.netbeans.core.output2; 20 21 import java.io.IOException ; 22 import java.nio.ByteBuffer ; 23 24 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 { 34 FileMapStorage result = new FileMapStorage(); 35 result.write(getReadBuffer(0, size), false); 36 return result; 37 } 38 39 public ByteBuffer getReadBuffer(int start, int length) throws IOException { 40 return ByteBuffer.wrap(bytes, start, Math.min(length, bytes.length - start)); 41 } 42 43 public ByteBuffer getWriteBuffer(int length) throws IOException { 44 return ByteBuffer.allocate(length); 45 } 46 47 public synchronized int write(ByteBuffer buf, boolean addNewLine) throws IOException { 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 { 74 } 76 77 public void close() throws IOException { 78 closed = true; 79 } 80 81 public boolean isClosed() { 82 return closed; } 84 } 85 | Popular Tags |