KickJava   Java API By Example, From Geeks To Geeks.

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


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.DataOutputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.OutputStream JavaDoc;
29 import java.nio.ByteBuffer JavaDoc;
30 import java.nio.CharBuffer JavaDoc;
31 import java.nio.charset.CharsetEncoder JavaDoc;
32
33 /**
34  * Decorates a DataOutputStream with the {@link #writeLongUTF(String)} method
35  * that allows writing of UTF strings larger than <code>65535</code> bytes
36  *
37  * @see java.io.DataOutputStream
38  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
39  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
40  * @author <a HREF="mailto:Marc.Herbert@emicnetworks.com">Marc Herbert</a>
41  * @author <a HREF="mailto:Gilles.Rayrat@emicnetworks.com">Gilles Rayrat</a>
42  */

43 public class LongUTFDataOutputStream extends DataOutputStream JavaDoc
44 {
45
46   private final CharsetEncoder JavaDoc utf8enc = DriverStream.UTF8Codec.newEncoder();
47
48   /**
49    * @see DataOutputStream#DataOutputStream(java.io.OutputStream)
50    */

51   public LongUTFDataOutputStream(OutputStream JavaDoc out)
52   {
53     super(out);
54   }
55
56   /**
57    * Sends UTF strings larger than <code>65535</code> bytes (encoded), chunk
58    * by chunk. Historically the purpose of these functions was to work around
59    * the limitation of {@link java.io.DataInputStream#readUTF()}, but now we
60    * use real UTF8, and no more modified UTF8
61    * http://en.wikipedia.org/wiki/UTF-8#Modified_UTF-8. Chunking is still useful
62    * to avoid handling big strings all at once and being a memory hog.
63    *
64    * @see java.io.DataOutputStream#writeUTF(java.lang.String)
65    * @param string a String to write in UTF form to the stream
66    * @throws IOException if an error occurs
67    */

68   public void writeLongUTF(String JavaDoc string) throws IOException JavaDoc
69   {
70     if (null == string)
71     {
72       super.writeBoolean(false);
73       return;
74     }
75
76     super.writeBoolean(true);
77     int idx;
78     final int maxSize = DriverStream.STRING_CHUNK_SIZE;
79
80     this.writeInt(string.length());
81
82     // First send all full, maxSize long chunks
83
for (idx = 0; idx + maxSize <= string.length(); idx += maxSize)
84       // substring() does no copy, cool.
85
writeUTF8(string.substring(idx, idx + maxSize));
86
87     // Send the tail separately because
88
// - string.substring(begin, TOO_LONG) is unfortunately not legal.
89
// - we do not send any empty string, this is useless and would complexify
90
// the receiver.
91
// The tail is in most (short) cases just the string as is.
92

93     if (string.length() > idx)
94       writeUTF8(string.substring(idx));
95   }
96
97   /**
98    * Sending real UTF-8, not the modified one.
99    *
100    * @throws IOException
101    * @see org.continuent.sequoia.common.protocol.SQLDataSerialization.BytesSerializer
102    */

103   void writeUTF8(String JavaDoc s) throws IOException JavaDoc
104   {
105     if (false) // old code (modified UTF-8). See SEQUOIA-133
106
super.writeUTF(s);
107     else
108     { // new code, real UTF8
109
CharBuffer JavaDoc cb = CharBuffer.wrap(s); // no copy; good.
110
ByteBuffer JavaDoc bb = utf8enc.encode(cb);
111       super.writeInt(bb.remaining());
112       super.write(bb.array(), 0, bb.remaining()); // no copy either
113
}
114   }
115 }
116
Popular Tags