KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > YapStringIOUnicode


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 final class YapStringIOUnicode extends YapStringIO{
27     
28     public int bytesPerChar(){
29         return 2;
30     }
31     
32     public byte encodingByte(){
33         return YapConst.UNICODE;
34     }
35     
36 // Currently not needed
37

38 // boolean isEqual(YapBytes bytes, String a_string){
39
// char ch;
40
// final int len = a_string.length();
41
// for (int ii = 0; ii < len; ii ++){
42
// ch = a_string.charAt(ii);
43
// if(bytes.i_bytes[bytes.i_offset++] != (byte) (ch & 0xff)){
44
// return false;
45
// }
46
// if(bytes.i_bytes[bytes.i_offset++] != (byte)(ch >> 8)){
47
// return false;
48
// }
49
// }
50
// return true;
51
// }
52

53     public int length(String JavaDoc a_string){
54         return (a_string.length() * 2) + YapConst.OBJECT_LENGTH + YapConst.INT_LENGTH;
55     }
56     
57     public String JavaDoc read(YapReader bytes, int a_length){
58         checkBufferLength(a_length);
59         for(int ii = 0; ii < a_length; ii++){
60             chars[ii] = (char)((bytes._buffer[bytes._offset ++]& 0xff) | ((bytes._buffer[bytes._offset ++]& 0xff) << 8));
61         }
62         return new String JavaDoc(chars,0,a_length);
63     }
64     
65     public String JavaDoc read(byte[] a_bytes){
66         int len = a_bytes.length / 2;
67         checkBufferLength(len);
68         int j = 0;
69         for(int ii = 0; ii < len; ii++){
70             chars[ii] = (char)((a_bytes[j++]& 0xff) | ((a_bytes[j++]& 0xff) << 8));
71         }
72         return new String JavaDoc(chars,0,len);
73     }
74     
75     public int shortLength(String JavaDoc a_string){
76         return (a_string.length() * 2) + YapConst.INT_LENGTH;
77     }
78     
79     public void write(YapReader bytes, String JavaDoc string){
80         final int len = writetoBuffer(string);
81         for (int i = 0; i < len; i ++){
82             bytes._buffer[bytes._offset++] = (byte) (chars[i] & 0xff);
83             bytes._buffer[bytes._offset++] = (byte) (chars[i] >> 8);
84         }
85     }
86     
87     byte[] write(String JavaDoc string){
88         final int len = writetoBuffer(string);
89         byte[] bytes = new byte[len * 2];
90         int j = 0;
91         for (int i = 0; i < len; i ++){
92             bytes[j++] = (byte) (chars[i] & 0xff);
93             bytes[j++] = (byte) (chars[i] >> 8);
94         }
95         return bytes;
96     }
97     
98 }
99
Popular Tags