001: /*
002: * Copyright (C) 2005-2006 data2c GmbH (www.data2c.com)
003: *
004: * Authors:
005: * Wolfgang S. Kechel - wolfgang.kechel@data2c.com
006: *
007: * Licensed under the Apache License, Version 2.0 (the "License");
008: * you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: package org.hecl.rms;
021:
022: import java.io.InputStream;
023: import java.io.IOException;
024:
025: import javax.microedition.rms.RecordEnumeration;
026: import javax.microedition.rms.RecordStore;
027: import javax.microedition.rms.RecordStoreException;
028:
029: public class RMSInputStream extends InputStream {
030: public RMSInputStream(String storename) throws IOException {
031: super ();
032: rsname = storename;
033: numbytes = 0;
034: try {
035: rs = RecordStore.openRecordStore(rsname, false);
036: RecordEnumeration records = rs.enumerateRecords(null, null,
037: false);
038: ids = new int[records.numRecords()];
039: int nfilled = 0;
040:
041: // sort the record nums to be ascending
042: while (records.hasNextElement()) {
043: boolean done = false;
044: int tosort = records.nextRecordId();
045: for (int i = 0; i < nfilled; ++i) {
046: if (ids[i] > tosort) {
047: System.arraycopy(ids, i, ids, i + 1, nfilled);
048: ids[i] = tosort;
049: nfilled++;
050: done = true;
051: break;
052: }
053: }
054: if (!done)
055: ids[nfilled++] = tosort;
056: }
057: for (int i = 0; i < ids.length; ++i) {
058: //#ifdef notdef
059: System.err.println("r[" + i + "]=" + ids[i]);
060: //#endif
061: byte[] buf = rs.getRecord(ids[i]);
062: numbytes += buf.length;
063: buf = null;
064: }
065: records.destroy();
066: } catch (Exception e) {
067: e.printStackTrace();
068: if (rs != null) {
069: try {
070: close();
071: } catch (Exception ex) {
072: }
073: }
074: throw new IOException(e.toString());
075: }
076: }
077:
078: public int available() throws IOException {
079: //System.err.println("RMSInputStream.available() --> "+nremaining);
080: return nremaining;
081: }
082:
083: public void close() throws IOException {
084: if (rs != null) {
085: try {
086: rs.closeRecordStore();
087: } catch (Exception e) {
088: e.printStackTrace();
089: } finally {
090: rs = null;
091: buf = null;
092: nremaining = -1;
093: }
094: //System.err.println("RMSInputStream.super.close()...");
095: // should not fail
096: super .close();
097: //System.err.println("RMSInputStream.super.close() done");
098: }
099: }
100:
101: public int getSize() {
102: return numbytes;
103: }
104:
105: public void mark(int readLimit) {
106: if (rs != null)
107: markposition = readLimit;
108: }
109:
110: public boolean markSupported() {
111: return true;
112: }
113:
114: public int read() throws IOException {
115: if (rs == null)
116: throw new IOException("record store closed.");
117: if (nremaining == 0) {
118: fillBuffer();
119: //System.err.println("filed buffer size="+nremaining);
120: }
121: if (nremaining > 0) {
122: --nremaining;
123: int abyte = buf[bufferpos++] & 0xff;
124: //System.err.print(" "+Integer.toHexString(abyte));
125: return abyte;
126: }
127: //System.err.println("RMSInputStream.read() --> -1");
128: return -1;
129: }
130:
131: public void reset() throws IOException {
132: if (rs == null)
133: throw new IOException("Stream closed.");
134:
135: ididx = -1;
136: while (markposition >= 0) {
137: fillBuffer();
138: markposition -= nremaining;
139: }
140: }
141:
142: public long skip(long n) throws IOException {
143: long nskipped = 0;
144: while (n > 0) {
145: if (read() == -1) {
146: return nskipped;
147: }
148: nskipped++;
149: }
150: return nskipped;
151: }
152:
153: protected synchronized void fillBuffer() throws IOException {
154: try {
155: if (ididx < ids.length - 1) {
156: buf = rs.getRecord(ids[++ididx]);
157: if (buf == null) {
158: // no more data!
159: nremaining = -1;
160: } else {
161: nremaining = buf.length;
162: bufferpos = 0;
163: }
164: } else {
165: // no more data
166: buf = null;
167: bufferpos = 0;
168: nremaining = -1;
169: }
170: }
171: //RecordStoreNotOpenException,
172: //InvalidRecordIDException,
173: //RecordStoreException
174: catch (Exception e) {
175: e.printStackTrace();
176: throw new IOException(e.toString());
177: }
178: //System.err.println("buffer filled, nremaining="+nremaining);
179: }
180:
181: private static final int BUFSIZE = 1024;
182:
183: private String rsname;
184: private RecordStore rs;
185: private int[] ids = null;
186: private int bufferpos = 0;
187: private int ididx = -1;
188: private int nremaining = 0;
189: private int markposition = 0;
190: private int numbytes = 0;
191: private byte[] buf = null;
192: }
|