1 | /* ============================================================= |
2 | * SmallSQL : a free Java DBMS library for the Java(tm) platform |
3 | * ============================================================= |
4 | * |
5 | * (C) Copyright 2004-2006, by Volker Berlin. |
6 | * |
7 | * Project Info: http://www.smallsql.de/ |
8 | * |
9 | * This library is free software; you can redistribute it and/or modify it |
10 | * under the terms of the GNU Lesser General Public License as published by |
11 | * the Free Software Foundation; either version 2.1 of the License, or |
12 | * (at your option) any later version. |
13 | * |
14 | * This library is distributed in the hope that it will be useful, but |
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
16 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
17 | * License for more details. |
18 | * |
19 | * You should have received a copy of the GNU Lesser General Public |
20 | * License along with this library; if not, write to the Free Software |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, |
22 | * USA. |
23 | * |
24 | * [Java is a trademark or registered trademark of Sun Microsystems, Inc. |
25 | * in the United States and other countries.] |
26 | * |
27 | * --------------- |
28 | * MemoryStream.java |
29 | * --------------- |
30 | * Author: Volker Berlin |
31 | * |
32 | */ |
33 | package smallsql.database; |
34 | |
35 | import java.io.*; |
36 | |
37 | /** |
38 | * @author Volker Berlin |
39 | * |
40 | */ |
41 | class MemoryStream { |
42 | private byte[] puffer; |
43 | private int offset; |
44 | |
45 | MemoryStream(){ |
46 | puffer = new byte[256]; |
47 | } |
48 | |
49 | void writeTo(RandomAccessFile file) throws IOException{ |
50 | file.write(puffer, 0, offset); |
51 | } |
52 | |
53 | void writeByte(int value){ |
54 | verifyFreePufferSize(1); |
55 | puffer[ offset++ ] = (byte)(value); |
56 | } |
57 | |
58 | void writeShort(int value){ |
59 | verifyFreePufferSize(2); |
60 | puffer[ offset++ ] = (byte)(value >> 8); |
61 | puffer[ offset++ ] = (byte)(value); |
62 | } |
63 | |
64 | void writeInt(int value){ |
65 | verifyFreePufferSize(4); |
66 | puffer[ offset++ ] = (byte)(value >> 24); |
67 | puffer[ offset++ ] = (byte)(value >> 16); |
68 | puffer[ offset++ ] = (byte)(value >> 8); |
69 | puffer[ offset++ ] = (byte)(value); |
70 | } |
71 | |
72 | void writeLong(long value){ |
73 | verifyFreePufferSize(8); |
74 | puffer[ offset++ ] = (byte)(value >> 56); |
75 | puffer[ offset++ ] = (byte)(value >> 48); |
76 | puffer[ offset++ ] = (byte)(value >> 40); |
77 | puffer[ offset++ ] = (byte)(value >> 32); |
78 | puffer[ offset++ ] = (byte)(value >> 24); |
79 | puffer[ offset++ ] = (byte)(value >> 16); |
80 | puffer[ offset++ ] = (byte)(value >> 8); |
81 | puffer[ offset++ ] = (byte)(value); |
82 | } |
83 | |
84 | void writeChars(char[] value){ |
85 | verifyFreePufferSize(2*value.length); |
86 | for(int i=0; i<value.length; i++){ |
87 | char c = value[i]; |
88 | puffer[ offset++ ] = (byte)(c >> 8); |
89 | puffer[ offset++ ] = (byte)(c); |
90 | } |
91 | } |
92 | |
93 | void writeBytes(byte[] value, int off, int length){ |
94 | verifyFreePufferSize(length); |
95 | System.arraycopy(value, off, puffer, offset, length); |
96 | offset += length; |
97 | } |
98 | |
99 | private void verifyFreePufferSize(int freeSize){ |
100 | int minSize = offset+freeSize; |
101 | if(minSize < puffer.length){ |
102 | int newSize = puffer.length << 1; |
103 | if(newSize < minSize) newSize = minSize; |
104 | byte[] temp = new byte[newSize]; |
105 | System.arraycopy(puffer, 0, temp, 0, offset); |
106 | puffer = temp; |
107 | } |
108 | } |
109 | |
110 | |
111 | int readByte(){ |
112 | return puffer[ offset++ ]; |
113 | } |
114 | |
115 | |
116 | int readShort(){ |
117 | return ((puffer[ offset++ ] & 0xFF) << 8) | (puffer[ offset++ ] & 0xFF); |
118 | } |
119 | |
120 | |
121 | int readInt(){ |
122 | return ((puffer[ offset++ ] & 0xFF) << 24) |
123 | | ((puffer[ offset++ ] & 0xFF) << 16) |
124 | | ((puffer[ offset++ ] & 0xFF) << 8) |
125 | | (puffer[ offset++ ] & 0xFF); |
126 | } |
127 | |
128 | |
129 | long readLong(){ |
130 | return (((long)(puffer[ offset++ ] & 0xFF)) << 56) |
131 | | (((long)(puffer[ offset++ ] & 0xFF)) << 48) |
132 | | (((long)(puffer[ offset++ ] & 0xFF)) << 40) |
133 | | (((long)(puffer[ offset++ ] & 0xFF)) << 32) |
134 | | ((puffer[ offset++ ] & 0xFF) << 24) |
135 | | ((puffer[ offset++ ] & 0xFF) << 16) |
136 | | ((puffer[ offset++ ] & 0xFF) << 8) |
137 | | (puffer[ offset++ ] & 0xFF); |
138 | } |
139 | |
140 | |
141 | char[] readChars(int length){ |
142 | char[] chars = new char[length]; |
143 | for(int i=0; i<length; i++){ |
144 | chars[i] = (char)readShort(); |
145 | } |
146 | return chars; |
147 | } |
148 | |
149 | byte[] readBytes(int length){ |
150 | byte[] bytes = new byte[length]; |
151 | System.arraycopy(puffer, offset, bytes, 0, length); |
152 | offset += length; |
153 | return bytes; |
154 | } |
155 | } |