KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > io > RandomAccessBuffer


1 /**
2  * Copyright (c) 2006, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31 package org.pdfbox.io;
32
33 import java.io.IOException JavaDoc;
34 import java.util.Arrays JavaDoc;
35
36 /**
37  * An interface to allow PDF files to be stored completely in memory.
38  *
39  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
40  * @version $Revision: 1.2 $
41  */

42 public class RandomAccessBuffer implements RandomAccess
43 {
44
45     private static final int EXTRA_SPACE = 16384; // 16kb
46
private byte[] buffer;
47     private long pointer;
48     private long size;
49     
50     /**
51      * Default constructor.
52      */

53     public RandomAccessBuffer()
54     {
55         buffer = new byte[EXTRA_SPACE];
56         pointer = 0;
57         size = EXTRA_SPACE;
58     }
59     
60     /**
61      * {@inheritDoc}
62      */

63     public void close() throws IOException JavaDoc
64     {
65         buffer = null;
66         pointer = 0;
67         size = 0;
68     }
69
70     /**
71      * {@inheritDoc}
72      */

73     public void seek(long position) throws IOException JavaDoc
74     {
75         this.pointer = position;
76     }
77
78     /**
79      * {@inheritDoc}
80      */

81     public int read() throws IOException JavaDoc
82     {
83         if (pointer >= this.size)
84         {
85             return -1;
86         }
87         int result = buffer[(int)pointer];
88         pointer++;
89         return result;
90     }
91
92     /**
93      * {@inheritDoc}
94      */

95     public int read(byte[] b, int offset, int length) throws IOException JavaDoc
96     {
97         if (pointer >= this.size)
98         {
99             return 0;
100         }
101         int maxLength = (int) Math.min(length, this.size-pointer);
102         System.arraycopy(buffer, (int) pointer, b, offset, maxLength);
103         pointer += maxLength;
104         return maxLength;
105     }
106
107     /**
108      * {@inheritDoc}
109      */

110     public long length() throws IOException JavaDoc
111     {
112         return size;
113     }
114
115     /**
116      * {@inheritDoc}
117      */

118     public void write(int b) throws IOException JavaDoc
119     {
120         write(new byte[] {(byte) b}, 0, 1);
121     }
122
123     /**
124      * {@inheritDoc}
125      */

126     public void write(byte[] b, int offset, int length) throws IOException JavaDoc
127     {
128         if (pointer+length >= buffer.length)
129         {
130             // expand buffer
131
byte[] temp = new byte[buffer.length+length+EXTRA_SPACE];
132             Arrays.fill(temp, (byte)0);
133             System.arraycopy(buffer, 0, temp, 0, (int) this.size);
134             buffer = temp;
135         }
136         System.arraycopy(b, offset, buffer, (int)pointer, length);
137         pointer += length;
138         if (pointer > this.size)
139         {
140             this.size = pointer;
141         }
142     }
143 }
144
Popular Tags