1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.pdfbox.io; 18 19 import java.io.IOException; 20 import java.util.Arrays; 21 22 /** 23 * An interface to allow PDF files to be stored completely in memory. 24 * 25 * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a> 26 * @version $Revision: 1.2 $ 27 */ 28 public class RandomAccessBuffer implements RandomAccess 29 { 30 31 private static final int EXTRA_SPACE = 16384; // 16kb 32 private byte[] buffer; 33 private long pointer; 34 private long size; 35 36 /** 37 * Default constructor. 38 */ 39 public RandomAccessBuffer() 40 { 41 buffer = new byte[EXTRA_SPACE]; 42 pointer = 0; 43 size = EXTRA_SPACE; 44 } 45 46 /** 47 * {@inheritDoc} 48 */ 49 public void close() throws IOException 50 { 51 buffer = null; 52 pointer = 0; 53 size = 0; 54 } 55 56 /** 57 * {@inheritDoc} 58 */ 59 public void seek(long position) throws IOException 60 { 61 this.pointer = position; 62 } 63 64 /** 65 * {@inheritDoc} 66 */ 67 public int read() throws IOException 68 { 69 if (pointer >= this.size) 70 { 71 return -1; 72 } 73 int result = buffer[(int)pointer]; 74 pointer++; 75 return result; 76 } 77 78 /** 79 * {@inheritDoc} 80 */ 81 public int read(byte[] b, int offset, int length) throws IOException 82 { 83 if (pointer >= this.size) 84 { 85 return 0; 86 } 87 int maxLength = (int) Math.min(length, this.size-pointer); 88 System.arraycopy(buffer, (int) pointer, b, offset, maxLength); 89 pointer += maxLength; 90 return maxLength; 91 } 92 93 /** 94 * {@inheritDoc} 95 */ 96 public long length() throws IOException 97 { 98 return size; 99 } 100 101 /** 102 * {@inheritDoc} 103 */ 104 public void write(int b) throws IOException 105 { 106 write(new byte[] {(byte) b}, 0, 1); 107 } 108 109 /** 110 * {@inheritDoc} 111 */ 112 public void write(byte[] b, int offset, int length) throws IOException 113 { 114 if (pointer+length >= buffer.length) 115 { 116 // expand buffer 117 byte[] temp = new byte[buffer.length+length+EXTRA_SPACE]; 118 Arrays.fill(temp, (byte)0); 119 System.arraycopy(buffer, 0, temp, 0, (int) this.size); 120 buffer = temp; 121 } 122 System.arraycopy(b, offset, buffer, (int)pointer, length); 123 pointer += length; 124 if (pointer > this.size) 125 { 126 this.size = pointer; 127 } 128 } 129 }