summaryrefslogtreecommitdiff
path: root/songdbj/org/tritonus/lowlevel/pogg/Buffer.java
diff options
context:
space:
mode:
authorBjörn Stenberg <bjorn@haxx.se>2007-01-08 23:53:00 +0000
committerBjörn Stenberg <bjorn@haxx.se>2007-01-08 23:53:00 +0000
commit7039a05147b8bbfc829babea1c65bd436450b505 (patch)
tree4ba555eb84ed97b72b0575034d5b0530a393713e /songdbj/org/tritonus/lowlevel/pogg/Buffer.java
parent6d4c19707ef95942e323cbdc89fbbfdbe45e7cc5 (diff)
downloadrockbox-7039a05147b8bbfc829babea1c65bd436450b505.tar.gz
rockbox-7039a05147b8bbfc829babea1c65bd436450b505.zip
Splitting out songdbj
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@11953 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'songdbj/org/tritonus/lowlevel/pogg/Buffer.java')
-rw-r--r--songdbj/org/tritonus/lowlevel/pogg/Buffer.java284
1 files changed, 0 insertions, 284 deletions
diff --git a/songdbj/org/tritonus/lowlevel/pogg/Buffer.java b/songdbj/org/tritonus/lowlevel/pogg/Buffer.java
deleted file mode 100644
index 6d94c37740..0000000000
--- a/songdbj/org/tritonus/lowlevel/pogg/Buffer.java
+++ /dev/null
@@ -1,284 +0,0 @@
1/*
2 * Buffer.java
3 *
4 * This file is part of Tritonus: http://www.tritonus.org/
5 */
6
7/*
8 * Copyright (c) 2000 - 2005 by Matthias Pfisterer
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Library General Public License as published
12 * by the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Library General Public License for more details.
19 *
20 * You should have received a copy of the GNU Library General Public
21 * License along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25/*
26|<--- this code is formatted to fit into 80 columns --->|
27*/
28
29package org.tritonus.lowlevel.pogg;
30
31import java.io.UnsupportedEncodingException;
32
33import org.tritonus.share.TDebug;
34
35
36/** Wrapper for oggpack_buffer.
37 */
38public class Buffer
39{
40 static
41 {
42 Ogg.loadNativeLibrary();
43 if (TDebug.TraceOggNative)
44 {
45 setTrace(true);
46 }
47 }
48
49
50 /**
51 * Holds the pointer to oggpack_buffer
52 * for the native code.
53 * This must be long to be 64bit-clean.
54 */
55 private long m_lNativeHandle;
56
57
58
59 public Buffer()
60 {
61 if (TDebug.TraceOggNative) { TDebug.out("Buffer.<init>(): begin"); }
62 int nReturn = malloc();
63 if (nReturn < 0)
64 {
65 throw new RuntimeException("malloc of ogg_page failed");
66 }
67 if (TDebug.TraceOggNative) { TDebug.out("Buffer.<init>(): end"); }
68 }
69
70
71
72 public void finalize()
73 {
74 // TODO: call free()
75 // call super.finalize() first or last?
76 // and introduce a flag if free() has already been called?
77 }
78
79
80
81 private native int malloc();
82 public native void free();
83
84
85 /** Calls oggpack_writeinit().
86 */
87 public native void writeInit();
88
89
90 /** Calls oggpack_writetrunc().
91 */
92 public native void writeTrunc(int nBits);
93
94
95 /** Calls oggpack_writealign().
96 */
97 public native void writeAlign();
98
99
100 /** Calls oggpack_writecopy().
101 */
102 public native void writeCopy(byte[] abSource, int nBits);
103
104
105 /** Calls oggpack_reset().
106 */
107 public native void reset();
108
109
110 /** Calls oggpack_writeclear().
111 */
112 public native void writeClear();
113
114
115 /** Calls oggpack_readinit().
116 */
117 public native void readInit(byte[] abBuffer, int nBytes);
118
119
120 /** Calls oggpack_write().
121 */
122 public native void write(int nValue, int nBits);
123
124
125 /** Calls oggpack_look().
126 */
127 public native int look(int nBits);
128
129
130 /** Calls oggpack_look1().
131 */
132 public native int look1();
133
134
135 /** Calls oggpack_adv().
136 */
137 public native void adv(int nBits);
138
139
140 /** Calls oggpack_adv1().
141 */
142 public native void adv1();
143
144
145 /** Calls oggpack_read().
146 */
147 public native int read(int nBits);
148
149
150 /** Calls oggpack_read1().
151 */
152 public native int read1();
153
154
155 /** Calls oggpack_bytes().
156 */
157 public native int bytes();
158
159
160 /** Calls oggpack_bits().
161 */
162 public native int bits();
163
164
165 /** Calls oggpack_get_buffer().
166 */
167 public native byte[] getBuffer();
168
169
170 /** Writes a string as UTF-8.
171 Note: no length coding and no end byte are written,
172 just the pure string!
173 */
174 public void write(String str)
175 {
176 write(str, false);
177 }
178
179
180 /** Writes a string as UTF-8, including a length coding.
181 In front of the string, the length in bytes is written
182 as a 32 bit integer. No end byte is written.
183 */
184 public void writeWithLength(String str)
185 {
186 write(str, true);
187 }
188
189
190 /** Writes a string as UTF-8, with or without a length coding.
191 If a length coding is requested, the length in (UTF8-)bytes is written
192 as a 32 bit integer in front of the string.
193 No end byte is written.
194 */
195 private void write(String str, boolean bWithLength)
196 {
197 byte[] aBytes = null;
198 try
199 {
200 aBytes = str.getBytes("UTF-8");
201 }
202 catch (UnsupportedEncodingException e)
203 {
204 if (TDebug.TraceAllExceptions) TDebug.out(e);
205 }
206 if (bWithLength)
207 {
208 write(aBytes.length, 32);
209 }
210 for (int i = 0; i < aBytes.length; i++)
211 {
212 write(aBytes[i], 8);
213 }
214 }
215
216
217 /** Reads a UTF-8 coded string with length coding.
218 It is expected that at the current read position,
219 there is a 32 bit integer containing the length in (UTF8-)bytes,
220 followed by the specified number of bytes in UTF-8 coding.
221
222 @return the string read from the buffer or null if an error occurs.
223 */
224 public String readString()
225 {
226 int length = read(32);
227 if (length < 0)
228 {
229 return null;
230 }
231 return readString(length);
232 }
233
234
235 /** Reads a UTF-8 coded string without length coding.
236 It is expected that at the current read position,
237 there is string in UTF-8 coding.
238
239 @return the string read from the buffer or null if an error occurs.
240 */
241 public String readString(int nLength)
242 {
243 byte[] aBytes = new byte[nLength];
244 for (int i = 0; i < nLength; i++)
245 {
246 aBytes[i] = (byte) read(8);
247 }
248 String s = null;
249 try
250 {
251 s = new String(aBytes, "UTF-8");
252 }
253 catch (UnsupportedEncodingException e)
254 {
255 if (TDebug.TraceAllExceptions) TDebug.out(e);
256 }
257 return s;
258 }
259
260
261 /** Reads a single bit.
262 */
263 public boolean readFlag()
264 {
265 return (read(1) != 0);
266 }
267
268 private static native void setTrace(boolean bTrace);
269
270 // for debugging
271 public static void outBuffer(byte[] buffer)
272 {
273 String s = "";
274 for (int i = 0; i < buffer.length; i++)
275 {
276 s += "" + buffer[i] + ", ";
277 }
278 TDebug.out("buffer: " + s);
279 }
280}
281
282
283
284/*** Buffer.java ***/