01: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
02:
03: This file is part of the db4o open source object database.
04:
05: db4o is free software; you can redistribute it and/or modify it under
06: the terms of version 2 of the GNU General Public License as published
07: by the Free Software Foundation and as clarified by db4objects' GPL
08: interpretation policy, available at
09: 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.internal;
22:
23: import com.db4o.marshall.*;
24:
25: /**
26: * @exclude
27: */
28: public final class UnicodeStringIO extends LatinStringIO {
29:
30: public int bytesPerChar() {
31: return 2;
32: }
33:
34: public byte encodingByte() {
35: return Const4.UNICODE;
36: }
37:
38: public int length(String str) {
39: return (str.length() * 2) + Const4.OBJECT_LENGTH
40: + Const4.INT_LENGTH;
41: }
42:
43: public String read(ReadBuffer buffer, int length) {
44: char[] chars = new char[length];
45: for (int ii = 0; ii < length; ii++) {
46: chars[ii] = (char) ((buffer.readByte() & 0xff) | ((buffer
47: .readByte() & 0xff) << 8));
48: }
49: return new String(chars, 0, length);
50: }
51:
52: public String read(byte[] bytes) {
53: int length = bytes.length / 2;
54: char[] chars = new char[length];
55: int j = 0;
56: for (int ii = 0; ii < length; ii++) {
57: chars[ii] = (char) ((bytes[j++] & 0xff) | ((bytes[j++] & 0xff) << 8));
58: }
59: return new String(chars, 0, length);
60: }
61:
62: public int shortLength(String str) {
63: return (str.length() * 2) + Const4.INT_LENGTH;
64: }
65:
66: public void write(WriteBuffer buffer, String str) {
67: final int length = str.length();
68: char[] chars = new char[length];
69: str.getChars(0, length, chars, 0);
70: for (int i = 0; i < length; i++) {
71: buffer.writeByte((byte) (chars[i] & 0xff));
72: buffer.writeByte((byte) (chars[i] >> 8));
73: }
74: }
75:
76: byte[] write(String str) {
77: final int length = str.length();
78: char[] chars = new char[length];
79: str.getChars(0, length, chars, 0);
80: byte[] bytes = new byte[length * 2];
81: int j = 0;
82: for (int i = 0; i < length; i++) {
83: bytes[j++] = (byte) (chars[i] & 0xff);
84: bytes[j++] = (byte) (chars[i] >> 8);
85: }
86: return bytes;
87: }
88:
89: }
|