KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > YapStringIO


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o;
22
23 /**
24  * @exclude
25  */

26 public class YapStringIO {
27     
28     protected char[] chars = new char[0];
29     
30     public int bytesPerChar(){
31         return 1;
32     }
33     
34     public byte encodingByte(){
35         return YapConst.ISO8859;
36     }
37     
38     static YapStringIO forEncoding(byte encodingByte){
39         switch (encodingByte) {
40         case YapConst.ISO8859:
41             return new YapStringIO();
42         default:
43             return new YapStringIOUnicode();
44         }
45     }
46
47
48 // Currently not needed
49

50 // boolean isEqual(YapBytes bytes, String a_string){
51
// char ch;
52
// final int len = a_string.length();
53
// for (int ii = 0; ii < len; ii ++){
54
// ch = a_string.charAt(ii);
55
// if(bytes.i_bytes[bytes.i_offset++] != (byte) (ch & 0xff)){
56
// return false;
57
// }
58
// }
59
// return true;
60
// }
61

62     public int length(String JavaDoc a_string){
63         return a_string.length() + YapConst.OBJECT_LENGTH + YapConst.INT_LENGTH;
64     }
65     
66     protected void checkBufferLength(int a_length){
67         if(a_length > chars.length){
68             chars = new char[a_length];
69         }
70     }
71     
72     public String JavaDoc read(YapReader bytes, int a_length){
73         checkBufferLength(a_length);
74         for(int ii = 0; ii < a_length; ii++){
75             chars[ii] = (char)(bytes._buffer[bytes._offset ++]& 0xff);
76         }
77         return new String JavaDoc(chars,0,a_length);
78     }
79     
80     public String JavaDoc read(byte[] a_bytes){
81         checkBufferLength(a_bytes.length);
82         for(int i = 0; i < a_bytes.length; i++){
83             chars[i] = (char)(a_bytes[i]& 0xff);
84         }
85         return new String JavaDoc(chars,0,a_bytes.length);
86     }
87     
88     public int shortLength(String JavaDoc a_string){
89         return a_string.length() + YapConst.INT_LENGTH;
90     }
91     
92     protected int writetoBuffer(String JavaDoc str){
93         final int len = str.length();
94         checkBufferLength(len);
95         str.getChars(0, len, chars, 0);
96         return len;
97     }
98             
99     
100     public void write(YapReader bytes, String JavaDoc string){
101         final int len = writetoBuffer(string);
102         for (int i = 0; i < len; i ++){
103             bytes._buffer[bytes._offset++] = (byte) (chars[i] & 0xff);
104         }
105     }
106     
107     byte[] write(String JavaDoc string){
108         final int len = writetoBuffer(string);
109         byte[] bytes = new byte[len];
110         for (int i = 0; i < len; i ++){
111             bytes[i] = (byte) (chars[i] & 0xff);
112         }
113         return bytes;
114     }
115     
116 }
117
Popular Tags