01: /**
02: * Sequoia: Database clustering technology.
03: * Copyright (C) 2002-2004 French National Institute For Research In Computer
04: * Science And Control (INRIA).
05: * Copyright (C) 2005 AmicoSoft, Inc. dba Emic Networks
06: * Contact: sequoia@continuent.org
07: *
08: * Licensed under the Apache License, Version 2.0 (the "License");
09: * you may not use this file except in compliance with the License.
10: * You may obtain a copy of the License at
11: *
12: * http://www.apache.org/licenses/LICENSE-2.0
13: *
14: * Unless required by applicable law or agreed to in writing, software
15: * distributed under the License is distributed on an "AS IS" BASIS,
16: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17: * See the License for the specific language governing permissions and
18: * limitations under the License.
19: *
20: * Initial developer(s): Nicolas Modrzyk
21: * Contributor(s): Emmanuel Cecchet. Marc Herbert.
22: */package org.continuent.sequoia.common.stream;
23:
24: import java.io.DataInputStream;
25: import java.io.IOException;
26: import java.io.InputStream;
27: import java.nio.ByteBuffer;
28: import java.nio.CharBuffer;
29: import java.nio.charset.CharsetDecoder;
30:
31: /**
32: * Decorates DataInputStream with the {@link #readLongUTF()} method that allows
33: * reading of UTF strings larger than <code>65535</code> bytes
34: *
35: * @see java.io.DataInputStream
36: * @see org.continuent.sequoia.common.stream.DriverBufferedOutputStream
37: * @author <a href="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
38: * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
39: * @author <a href="mailto:Marc.Herbert@emicnetworks.com">Marc Herbert</a>
40: * @author <a href="mailto:Gilles.Rayrat@emicnetworks.com">Gilles Rayrat</a>
41: */
42: public class LongUTFDataInputStream extends DataInputStream {
43: private final CharsetDecoder utf8dec = DriverStream.UTF8Codec
44: .newDecoder();
45:
46: /**
47: * @see DataInputStream#DataInputStream(java.io.InputStream)
48: */
49: public LongUTFDataInputStream(InputStream in) {
50: super (in);
51: }
52:
53: /**
54: * @see LongUTFDataOutputStream#writeLongUTF(String)
55: * @return a String in UTF format
56: * @throws IOException if an error occurs
57: */
58: public String readLongUTF() throws IOException {
59: if (!super .readBoolean())
60: return null;
61:
62: final int maxSize = DriverStream.STRING_CHUNK_SIZE;
63:
64: int strlen = super .readInt();
65: StringBuffer sbuf = new StringBuffer(strlen);
66:
67: // idx semantic: chars at idx and after had not yet the opportunity
68: // to be received.
69: for (int idx = 0; idx < strlen; idx += maxSize)
70: sbuf.append(readUTF8());
71:
72: return new String(sbuf);
73: }
74:
75: /**
76: * @return decoded string from stream
77: * @throws IOException network error
78: * @see LongUTFDataOutputStream#writeUTF8(String)
79: * @see org.continuent.sequoia.common.protocol.SQLDataSerialization.BytesSerializer
80: */
81: String readUTF8() throws IOException {
82: if (false) // old code (modified UTF-8). See SEQUOIA-133
83: return super .readUTF();
84: else { // new code, real UTF8
85: int len = super .readInt();
86: byte[] b = new byte[len];
87: super .readFully(b);
88: ByteBuffer bb = ByteBuffer.wrap(b); // no copy, nice.
89: CharBuffer cb = utf8dec.decode(bb);
90: return cb.toString();
91: }
92: }
93: }
|