1 23 24 package org.continuent.sequoia.common.stream; 25 26 import java.io.DataOutputStream ; 27 import java.io.IOException ; 28 import java.io.OutputStream ; 29 import java.nio.ByteBuffer ; 30 import java.nio.CharBuffer ; 31 import java.nio.charset.CharsetEncoder ; 32 33 43 public class LongUTFDataOutputStream extends DataOutputStream 44 { 45 46 private final CharsetEncoder utf8enc = DriverStream.UTF8Codec.newEncoder(); 47 48 51 public LongUTFDataOutputStream(OutputStream out) 52 { 53 super(out); 54 } 55 56 68 public void writeLongUTF(String string) throws IOException 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 for (idx = 0; idx + maxSize <= string.length(); idx += maxSize) 84 writeUTF8(string.substring(idx, idx + maxSize)); 86 87 93 if (string.length() > idx) 94 writeUTF8(string.substring(idx)); 95 } 96 97 103 void writeUTF8(String s) throws IOException 104 { 105 if (false) super.writeUTF(s); 107 else 108 { CharBuffer cb = CharBuffer.wrap(s); ByteBuffer bb = utf8enc.encode(cb); 111 super.writeInt(bb.remaining()); 112 super.write(bb.array(), 0, bb.remaining()); } 114 } 115 } 116 | Popular Tags |