001: package com.quadcap.sql.io;
002:
003: /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.IOException;
042: import java.io.ObjectInput;
043: import java.io.ObjectOutput;
044:
045: import com.quadcap.util.Debug;
046:
047: /**
048: * ExternalizeProxy for strings.
049: *
050: * @author Stan Bailes
051: */
052: public class ExternProxyString implements ExternalizeProxy {
053:
054: //#ifdef SMALLDB
055: public Object readObject(ObjectInput in) throws IOException {
056: int cnt = in.readInt();
057: if (cnt == -1)
058: return null;
059: char[] buf = new char[cnt];
060: for (int i = 0; i < cnt; i++) {
061: buf[i] = in.readChar();
062: }
063: return new String(buf);
064: }
065:
066: public void writeObject(ObjectOutput out, Object object)
067: throws IOException {
068: if (object == null) {
069: out.writeInt(-1);
070: } else {
071: String s = (String) object;
072: int cnt = s.length();
073: out.writeInt(cnt);
074: for (int i = 0; i < cnt; i++) {
075: out.writeChar(s.charAt(i));
076: }
077: }
078: }
079: //#else
080: //- public Object readObject(ObjectInput in) throws IOException {
081: //- int cnt = in.readInt();
082: //- if (cnt == -1) return null;
083: //- char[] buf = new char[cnt];
084: //- byte[] b = new byte[cnt << 1];
085: //- in.readFully(b);
086: //- int k = 0;
087: //- for (int i = 0; i < cnt; i++) {
088: //- int s = (b[k++] & 0xff) << 8;
089: //- s |= (b[k++] & 0xff);
090: //- buf[i] = (char)s;
091: //- }
092: //- return new String(buf);
093: //- }
094: //-
095: //- public void writeObject(ObjectOutput out, Object object)
096: //- throws IOException
097: //- {
098: //- if (object == null) {
099: //- out.writeInt(-1);
100: //- } else {
101: //- String s = (String)object;
102: //- int cnt = s.length();
103: //- out.writeInt(cnt);
104: //- byte[] buf = new byte[cnt << 1];
105: //- int k = 0;
106: //- for (int i = 0; i < cnt; i++) {
107: //- int c = s.charAt(i);
108: //- buf[k++] = (byte)((c >> 8) & 0xff);
109: //- buf[k++] = (byte)(c & 0xff);
110: //- }
111: //- out.write(buf);
112: //- }
113: //- }
114: //#endif
115: }
|