1 23 24 package org.continuent.sequoia.common.stream; 25 26 import java.io.DataInputStream ; 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.nio.ByteBuffer ; 30 import java.nio.CharBuffer ; 31 import java.nio.charset.CharsetDecoder ; 32 33 44 public class LongUTFDataInputStream extends DataInputStream 45 { 46 private final CharsetDecoder utf8dec = DriverStream.UTF8Codec.newDecoder(); 47 48 51 public LongUTFDataInputStream(InputStream in) 52 { 53 super(in); 54 } 55 56 61 public String readLongUTF() throws IOException 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 sbuf = new StringBuffer (strlen); 70 71 for (int idx = 0; idx < strlen; idx += maxSize) 74 sbuf.append(readUTF8()); 75 76 return new String (sbuf); 77 } 78 79 85 String readUTF8() throws IOException 86 { 87 if (false) return super.readUTF(); 89 else 90 { int len = super.readInt(); 92 byte[] b = new byte[len]; 93 super.readFully(b); 94 ByteBuffer bb = ByteBuffer.wrap(b); CharBuffer cb = utf8dec.decode(bb); 96 return cb.toString(); 97 } 98 } 99 } | Popular Tags |