KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > common > stream > LongUTFDataInputStream


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Copyright (C) 2005 AmicoSoft, Inc. dba Emic Networks
6  * Contact: sequoia@continuent.org
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * 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  */

23
24 package org.continuent.sequoia.common.stream;
25
26 import java.io.DataInputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.nio.ByteBuffer JavaDoc;
30 import java.nio.CharBuffer JavaDoc;
31 import java.nio.charset.CharsetDecoder JavaDoc;
32
33 /**
34  * Decorates DataInputStream with the {@link #readLongUTF()} method that allows
35  * reading of UTF strings larger than <code>65535</code> bytes
36  *
37  * @see java.io.DataInputStream
38  * @see org.continuent.sequoia.common.stream.DriverBufferedOutputStream
39  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
40  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
41  * @author <a HREF="mailto:Marc.Herbert@emicnetworks.com">Marc Herbert</a>
42  * @author <a HREF="mailto:Gilles.Rayrat@emicnetworks.com">Gilles Rayrat</a>
43  */

44 public class LongUTFDataInputStream extends DataInputStream JavaDoc
45 {
46   private final CharsetDecoder JavaDoc utf8dec = DriverStream.UTF8Codec.newDecoder();
47
48   /**
49    * @see DataInputStream#DataInputStream(java.io.InputStream)
50    */

51   public LongUTFDataInputStream(InputStream JavaDoc in)
52   {
53     super(in);
54   }
55
56   /**
57    * @see LongUTFDataOutputStream#writeLongUTF(String)
58    * @return a String in UTF format
59    * @throws IOException if an error occurs
60    */

61   public String JavaDoc readLongUTF() throws IOException JavaDoc
62   {
63     if (!super.readBoolean())
64       return null;
65
66     final int maxSize = DriverStream.STRING_CHUNK_SIZE;
67
68     int strlen = super.readInt();
69     StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc(strlen);
70
71     // idx semantic: chars at idx and after had not yet the opportunity
72
// to be received.
73
for (int idx = 0; idx < strlen; idx += maxSize)
74       sbuf.append(readUTF8());
75
76     return new String JavaDoc(sbuf);
77   }
78
79   /**
80    * @return decoded string from stream
81    * @throws IOException network error
82    * @see LongUTFDataOutputStream#writeUTF8(String)
83    * @see org.continuent.sequoia.common.protocol.SQLDataSerialization.BytesSerializer
84    */

85   String JavaDoc readUTF8() throws IOException JavaDoc
86   {
87     if (false) // old code (modified UTF-8). See SEQUOIA-133
88
return super.readUTF();
89     else
90     { // new code, real UTF8
91
int len = super.readInt();
92       byte[] b = new byte[len];
93       super.readFully(b);
94       ByteBuffer JavaDoc bb = ByteBuffer.wrap(b); // no copy, nice.
95
CharBuffer JavaDoc cb = utf8dec.decode(bb);
96       return cb.toString();
97     }
98   }
99 }
Popular Tags