001: /**
002: * Copyright (c) 2006, www.pdfbox.org
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * 2. Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: * 3. Neither the name of pdfbox; nor the names of its
014: * contributors may be used to endorse or promote products derived from this
015: * software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: * http://www.pdfbox.org
029: *
030: */package org.pdfbox.io;
031:
032: import java.io.IOException;
033: import java.util.Arrays;
034:
035: /**
036: * An interface to allow PDF files to be stored completely in memory.
037: *
038: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
039: * @version $Revision: 1.2 $
040: */
041: public class RandomAccessBuffer implements RandomAccess {
042:
043: private static final int EXTRA_SPACE = 16384; // 16kb
044: private byte[] buffer;
045: private long pointer;
046: private long size;
047:
048: /**
049: * Default constructor.
050: */
051: public RandomAccessBuffer() {
052: buffer = new byte[EXTRA_SPACE];
053: pointer = 0;
054: size = EXTRA_SPACE;
055: }
056:
057: /**
058: * {@inheritDoc}
059: */
060: public void close() throws IOException {
061: buffer = null;
062: pointer = 0;
063: size = 0;
064: }
065:
066: /**
067: * {@inheritDoc}
068: */
069: public void seek(long position) throws IOException {
070: this .pointer = position;
071: }
072:
073: /**
074: * {@inheritDoc}
075: */
076: public int read() throws IOException {
077: if (pointer >= this .size) {
078: return -1;
079: }
080: int result = buffer[(int) pointer];
081: pointer++;
082: return result;
083: }
084:
085: /**
086: * {@inheritDoc}
087: */
088: public int read(byte[] b, int offset, int length)
089: throws IOException {
090: if (pointer >= this .size) {
091: return 0;
092: }
093: int maxLength = (int) Math.min(length, this .size - pointer);
094: System.arraycopy(buffer, (int) pointer, b, offset, maxLength);
095: pointer += maxLength;
096: return maxLength;
097: }
098:
099: /**
100: * {@inheritDoc}
101: */
102: public long length() throws IOException {
103: return size;
104: }
105:
106: /**
107: * {@inheritDoc}
108: */
109: public void write(int b) throws IOException {
110: write(new byte[] { (byte) b }, 0, 1);
111: }
112:
113: /**
114: * {@inheritDoc}
115: */
116: public void write(byte[] b, int offset, int length)
117: throws IOException {
118: if (pointer + length >= buffer.length) {
119: // expand buffer
120: byte[] temp = new byte[buffer.length + length + EXTRA_SPACE];
121: Arrays.fill(temp, (byte) 0);
122: System.arraycopy(buffer, 0, temp, 0, (int) this .size);
123: buffer = temp;
124: }
125: System.arraycopy(b, offset, buffer, (int) pointer, length);
126: pointer += length;
127: if (pointer > this.size) {
128: this.size = pointer;
129: }
130: }
131: }
|