01: /*
02: * Copyright (C) 2005-2006 data2c GmbH (www.data2c.com)
03: *
04: * Authors:
05: * Wolfgang S. Kechel - wolfgang.kechel@data2c.com
06: *
07: * Licensed under the Apache License, Version 2.0 (the "License");
08: * you may not use this file except in compliance with the License.
09: * You may obtain a copy of the License at
10: *
11: * http://www.apache.org/licenses/LICENSE-2.0
12: *
13: * Unless required by applicable law or agreed to in writing, software
14: * distributed under the License is distributed on an "AS IS" BASIS,
15: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16: * See the License for the specific language governing permissions and
17: * limitations under the License.
18: */
19:
20: package org.hecl.rms;
21:
22: import java.io.InputStream;
23: import java.io.OutputStream;
24: import java.io.IOException;
25:
26: import javax.microedition.rms.RecordStore;
27:
28: public class RMSUtils {
29: private static void copyBytes(InputStream from, OutputStream to)
30: throws IOException {
31: byte[] buf = new byte[CHUNKSIZE];
32: int n = 0;
33:
34: while ((n = from.read(buf)) > 0) {
35: //System.err.println("writing a chunk of size="+n);
36: to.write(buf, 0, n);
37: /*
38: if(enc == null) {
39: } else {
40: for(int i=0; i<n; ++i)
41: to.write(buf[i] & 0xff);
42: }
43: */
44: }
45: }
46:
47: public static void copyFrom(String rsname, OutputStream os)
48: throws IOException {
49: RMSInputStream is = null;
50: try {
51: is = new RMSInputStream(rsname);
52: copyBytes(is, os);
53: InputStream iis = is;
54: is = null;
55: iis.close();
56: } catch (IOException e) {
57: if (is != null) {
58: try {
59: is.close();
60: } catch (IOException ee) {
61: }
62: }
63: throw e;
64: }
65: }
66:
67: public static void saveTo(String rsname, InputStream is)
68: throws IOException {
69: RMSOutputStream os = null;
70: try {
71: os = new RMSOutputStream(rsname);
72: copyBytes(is, os);
73: OutputStream oos = os;
74: os = null;
75: oos.close();
76: } catch (IOException e) {
77: if (os != null) {
78: try {
79: os.close();
80: } catch (IOException ee) {
81: }
82: }
83: throw e;
84: }
85: }
86:
87: public static final int CHUNKSIZE = 1024;
88: }
|