Java Code Examples for java.io.DataInputStream

The following examples show how to use java.io.DataInputStream. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: kylin-on-parquet-v2   Source File: CachedCrudAssist.java    License: Apache License 2.0 6 votes vote down vote up
public T copyForWrite(T entity) {
    if (entity.isCachedAndShared() == false)
        return entity;

    T copy;
    try {
        byte[] bytes;
        try (ByteArrayOutputStream buf = new ByteArrayOutputStream();
                DataOutputStream dout = new DataOutputStream(buf)) {
            serializer.serialize(entity, dout);
            bytes = buf.toByteArray();
        }

        try (DataInputStream in = new DataInputStream(new ByteArrayInputStream(bytes))) {
            copy = serializer.deserialize(in);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    copy.setCachedAndShared(false);
    initEntityAfterReload(copy, entity.resourceName());
    return copy;
}
 
Example 2
Source Project: hadoop   Source File: TestTupleWritable.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Tests compatibility with pre-0.21 versions of TupleWritable
 */
public void testPreVersion21Compatibility() throws Exception {
  Writable[] manyWrits = makeRandomWritables(64);
  PreVersion21TupleWritable oldTuple = new PreVersion21TupleWritable(manyWrits);
  
  for (int i =0; i<manyWrits.length; i++) {
    if (i % 3 == 0) {
      oldTuple.setWritten(i);
    }
  }
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  oldTuple.write(new DataOutputStream(out));
  ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
  TupleWritable dTuple = new TupleWritable();
  dTuple.readFields(new DataInputStream(in));
  assertTrue("Tuple writable is unable to read pre-0.21 versions of TupleWritable", oldTuple.isCompatible(dTuple));
  assertEquals("All tuple data has not been read from the stream",-1,in.read());
}
 
Example 3
Source Project: big-c   Source File: Server.java    License: Apache License 2.0 6 votes vote down vote up
private void saslReadAndProcess(DataInputStream dis) throws
WrappedRpcServerException, IOException, InterruptedException {
  final RpcSaslProto saslMessage =
      decodeProtobufFromStream(RpcSaslProto.newBuilder(), dis);
  switch (saslMessage.getState()) {
    case WRAP: {
      if (!saslContextEstablished || !useWrap) {
        throw new WrappedRpcServerException(
            RpcErrorCodeProto.FATAL_INVALID_RPC_HEADER,
            new SaslException("Server is not wrapping data"));
      }
      // loops over decoded data and calls processOneRpc
      unwrapPacketAndProcessRpcs(saslMessage.getToken().toByteArray());
      break;
    }
    default:
      saslProcess(saslMessage);
  }
}
 
Example 4
Source Project: jdk8u60   Source File: DTD.java    License: GNU General Public License v2.0 6 votes vote down vote up
private AttributeList readAttributeList(DataInputStream in, String[] names)
                throws IOException  {
        AttributeList result = null;
        for (int num = in.readByte(); num > 0; --num) {
            short nameId = in.readShort();
            int type = in.readByte();
            int modifier = in.readByte();
            short valueId = in.readShort();
            String value = (valueId == -1) ? null : names[valueId];
            Vector<String> values = null;
            short numValues = in.readShort();
            if (numValues > 0) {
                values = new Vector<String>(numValues);
                for (int i = 0; i < numValues; i++) {
                    values.addElement(names[in.readShort()]);
                }
            }
result = new AttributeList(names[nameId], type, modifier, value,
                                       values, result);
            // We reverse the order of the linked list by doing this, but
            // that order isn't important.
        }
        return result;
    }
 
Example 5
Source Project: phoenix   Source File: ColumnExpressionTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testSerialization() throws Exception {
    int maxLen = 30;
    int scale = 5;
    PName colName = PNameFactory.newName("c1");
    PColumn column = new PColumnImpl(colName, PNameFactory.newName("f1"), PDecimal.INSTANCE, maxLen, scale,
            true, 20, SortOrder.getDefault(), 0, null, false, null, false, false, colName.getBytes(), HConstants.LATEST_TIMESTAMP);
    ColumnExpression colExp = new KeyValueColumnExpression(column);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dOut = new DataOutputStream(baos);
    colExp.write(dOut);
    dOut.flush();

    ColumnExpression colExp2 = new KeyValueColumnExpression();
    byte[] bytes = baos.toByteArray();
    DataInputStream dIn = new DataInputStream(new ByteArrayInputStream(bytes, 0, bytes.length));
    colExp2.readFields(dIn);
    assertEquals(maxLen, colExp2.getMaxLength().intValue());
    assertEquals(scale, colExp2.getScale().intValue());
    assertEquals(PDecimal.INSTANCE, colExp2.getDataType());
}
 
Example 6
@Listener
public void onServerAboutToStart(GameAboutToStartServerEvent event) {

    // register plugin message channel
    channel = game.getChannelRegistrar().createRawChannel(this, BridgeProtocolConstants.CHANNEL);
    channel.addListener(Platform.Type.SERVER, (data, connection, side) -> {
        if (connection instanceof PlayerConnection) {
            Player player = ((PlayerConnection) connection).getPlayer();
            DataInput input = new DataInputStream(new ChannelBufInputStream(data));
            try {
                bridge.onMessage(player, input);
            } catch (Throwable e) {
                rlExecutor.execute(() -> {
                    logger.error("Unexpected error", e);
                });
            }
        }
    });

    // init bridge
    initBridge();
}
 
Example 7
/**
 * {@inheritDoc}
 */
public void readWire(final DataInputStream in) throws IOException, ClassNotFoundException {

   super.readWire(in);

   bucketOwnerAddress = SerializerUtils.readAddress(in);

   final int keySetSize = in.readInt();
   keySet = new IntObjectHashMap<HashSet<Binary>>(keySetSize);
   for (int i = 0; i < keySetSize; i++) {

      final int bucketNumber = in.readShort();
      final int keysSize = in.readInt();
      final HashSet<Binary> keys = new HashSet<Binary>(keysSize);
      for (int j = 0; j < keysSize; j++) {

         keys.add(SerializerUtils.readBinary(in));
      }
      keySet.put(bucketNumber, keys);
   }
}
 
Example 8
Source Project: whyline   Source File: LineNumberTableAttribute.java    License: MIT License 6 votes vote down vote up
private LineNumberTableEntry[] parseLines() {

		if(lines == null) {
			try {
				DataInputStream data = new DataInputStream(new ByteArrayInputStream(bytes));
				lineNumberTableLength = data.readUnsignedShort();
				lines = new LineNumberTableEntry[lineNumberTableLength];
				for(int i = 0; i < lineNumberTableLength; i++) {
					int startPC = data.readUnsignedShort();
					int line = data.readUnsignedShort();
					Instruction inst = code.getInstructionAtByteIndex(startPC);
					lines[i] = new LineNumberTableEntry(pool.getClassfile(), inst, line);			
				}
				bytes = null;
			} catch(IOException e) {
				e.printStackTrace();
			}

		}
		return lines;
		
	}
 
Example 9
Source Project: RipplePower   Source File: AddressIndexBlock.java    License: Apache License 2.0 6 votes vote down vote up
private static byte[] input(byte[] ins, String name) throws IOException {
	int find = Character.toLowerCase(name.charAt(4));
	ByteArrayInputStream tmp = new ByteArrayInputStream(ins);
	DataInputStream in = new DataInputStream(tmp);
	for (;;) {
		in.readInt();
		int valueLength = in.readInt();
		byte keyBuffer = in.readByte();
		if (keyBuffer == find) {
			byte[] valueBuffer = new byte[valueLength];
			in.read(valueBuffer, 0, valueLength);
			return valueBuffer;
		} else {
			in.skipBytes(valueLength);
		}
	}
}
 
Example 10
Source Project: openjdk-jdk9   Source File: Currency.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static List<OtherCurrencyEntry> readOtherCurrencies(DataInputStream dis,
        int count)
        throws IOException {

    List<OtherCurrencyEntry> list = new ArrayList<>(count);
    String currencyCode;
    int fraction;
    int numericCode;

    for (int i = 0; i < count; i++) {
        currencyCode = dis.readUTF();
        fraction = dis.readInt();
        numericCode = dis.readInt();
        OtherCurrencyEntry oc = new OtherCurrencyEntry(currencyCode,
                fraction,
                numericCode);
        list.add(oc);
    }
    return list;
}
 
Example 11
Source Project: Android-Music-Player   Source File: catchBase.java    License: MIT License 6 votes vote down vote up
public ArrayList<String[]> readArrayList(DataInputStream Din) throws IOException {
    int arLen = Din.readInt();
    ArrayList<String[]> ar = null;
    if(arLen > 0){
        ar = new ArrayList<String[]>();
    }
    for(int i = 0;i < arLen;i++){
        int sLen = Din.readInt();
        String[] sr = new String[sLen];
        for(int j = 0;j < sLen;j++) {
            sr[j] = Din.readUTF();
        }
        ar.add(sr);
    }
    return  ar;
}
 
Example 12
Source Project: hyperloglog   Source File: TestHLLSerialization.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testHLLDenseSerializationHalfDistinct() throws IOException {
  HyperLogLog hll = HyperLogLog.builder().setEncoding(EncodingType.DENSE).build();
  Random rand = new Random(SEED);
  Set<Integer> hashset = new HashSet<Integer>();
  for (int i = 0; i < size; i++) {
    int val = rand.nextInt(size / 2);
    hll.addLong(val);
    hashset.add(val);
  }
  FileOutputStream fos = new FileOutputStream(testFile);
  DataOutputStream out = new DataOutputStream(fos);
  HyperLogLogUtils.serializeHLL(out, hll);
  double threshold = size > 40000 ? longRangeTolerance : shortRangeTolerance;
  double delta = threshold * hashset.size() / 100;
  FileInputStream fis = new FileInputStream(testFile);
  DataInputStream in = new DataInputStream(fis);
  HyperLogLog deserializedHLL = HyperLogLogUtils.deserializeHLL(in);
  assertEquals(hll, deserializedHLL);
  assertEquals(hll.toString(), deserializedHLL.toString());
  assertEquals(hll.toStringExtended(), deserializedHLL.toStringExtended());
  assertEquals(hll.hashCode(), deserializedHLL.hashCode());
  assertEquals(hll.count(), deserializedHLL.count());
  assertEquals(hashset.size(), hll.count(), delta);
  assertEquals(hashset.size(), deserializedHLL.count(), delta);
}
 
Example 13
Source Project: flink   Source File: MetadataV3Serializer.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected OperatorState deserializeOperatorState(DataInputStream dis, @Nullable DeserializationContext context) throws IOException {
	final OperatorID jobVertexId = new OperatorID(dis.readLong(), dis.readLong());
	final int parallelism = dis.readInt();
	final int maxParallelism = dis.readInt();

	final OperatorState operatorState = new OperatorState(jobVertexId, parallelism, maxParallelism);

	// Coordinator state
	operatorState.setCoordinatorState(deserializeAndCheckByteStreamStateHandle(dis, context));

	// Sub task states
	final int numSubTaskStates = dis.readInt();

	for (int j = 0; j < numSubTaskStates; j++) {
		final int subtaskIndex = dis.readInt();
		final OperatorSubtaskState subtaskState = deserializeSubtaskState(dis, context);
		operatorState.putState(subtaskIndex, subtaskState);
	}

	return operatorState;
}
 
Example 14
Source Project: birt   Source File: CubeQueryDefinitionIOUtil.java    License: Eclipse Public License 1.0 6 votes vote down vote up
private static IDimensionDefinition loadDimensionDefinition( DataInputStream dis ) throws IOException
{
	if ( !IOUtil.readBool( dis ) )
	{
		return null;
	}
	String name = IOUtil.readString( dis );
	DimensionDefinition dd = new DimensionDefinition( name );
	int hierarchyCount = IOUtil.readInt( dis );
	for ( int i=0; i<hierarchyCount; i++)
	{
		String hierarchyName = IOUtil.readString( dis );
		IHierarchyDefinition hd = dd.createHierarchy( hierarchyName );
		int levelCount = IOUtil.readInt( dis );
		for ( int j=0; j<levelCount; j++)
		{
			String levelName = IOUtil.readString( dis );
			hd.createLevel( levelName );
		}
	}
	return dd;
}
 
Example 15
Source Project: gemfirexd-oss   Source File: StatArchiveReader.java    License: Apache License 2.0 6 votes vote down vote up
public StatArchiveFile(StatArchiveReader reader,
                       File archiveName,
                       boolean dump,
                       ValueFilter[] filters)
  throws IOException
{
  this.reader = reader;
  this.archiveName = archiveName;
  this.dump = dump;
  this.compressed = archiveName.getPath().endsWith(".gz");
  this.is = new FileInputStream(this.archiveName);
  if (this.compressed) {
    this.dataIn = new DataInputStream(new BufferedInputStream(new GZIPInputStream(this.is, BUFFER_SIZE), BUFFER_SIZE));
  } else {
    this.dataIn = new DataInputStream(new BufferedInputStream(this.is, BUFFER_SIZE));
  }
  this.updateOK = this.dataIn.markSupported();
  this.filters = createFilters(filters);
}
 
Example 16
Source Project: nd4j   Source File: WorkspaceProviderTests.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testWorkspacesSerde1() throws Exception {
    int[] shape = new int[] {17, 57, 79};
    INDArray array = Nd4j.create(shape).assign(1.0);
    INDArray restored = null;

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(bos);
    Nd4j.write(array, dos);

    try (MemoryWorkspace workspace = Nd4j.getWorkspaceManager().getAndActivateWorkspace(bigConfiguration, "WS_1")) {
        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        DataInputStream dis = new DataInputStream(bis);
        restored = Nd4j.read(dis);

        assertEquals(array.length(), restored.length());
        assertEquals(1.0f, restored.meanNumber().floatValue(), 1.0f);

        // we want to ensure it's the same cached shapeInfo used here
        assertTrue(array.shapeInfoDataBuffer() == restored.shapeInfoDataBuffer());
    }
}
 
Example 17
Source Project: joshua   Source File: Vocabulary.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Reads a vocabulary from file. This deletes any additions to the vocabulary made prior to
 * reading the file.
 *
 * @param vocab_file path to a vocabulary file
 * @return Returns true if vocabulary was read without mismatches or collisions.
 * @throws IOException of the file cannot be found or read properly
 */
public static boolean read(final File vocab_file) throws IOException {
  DataInputStream vocab_stream =
      new DataInputStream(new BufferedInputStream(new FileInputStream(vocab_file)));
  int size = vocab_stream.readInt();
  LOG.info("Read {} entries from the vocabulary", size);
  clear();
  for (int i = 0; i < size; i++) {
    int id = vocab_stream.readInt();
    String token = vocab_stream.readUTF();
    if (id != Math.abs(id(token))) {
      vocab_stream.close();
      return false;
    }
  }
  vocab_stream.close();
  return (size + 1 == idToString.size());
}
 
Example 18
ClassfileConstantPool(DataInputStream stream, ClassfileBytecodeProvider context) throws IOException {
    this.context = context;
    byte tag;

    int count = stream.readUnsignedShort();
    entries = new ClassfileConstant[count];

    int i = 1;
    while (i < count) {
        entries[i] = readConstant(stream);
        tag = entries[i].tag;

        if ((tag == ClassfileConstant.CONSTANT_Double) || (tag == ClassfileConstant.CONSTANT_Long)) {
            i += 2;
        } else {
            i += 1;
        }
    }
}
 
Example 19
/**
 * {@inheritDoc}
 */
public void readWire(final DataInputStream in) throws IOException, ClassNotFoundException {

   super.readWire(in);

   //
   newClusterUUID = SerializerUtils.readUuid(in);

   // Originator
   originator = SerializerUtils.readAddress(in);

   // current list
   final int currentListSize = in.readInt();
   currentList = new ArrayList<JoiningNode>(currentListSize + 1); // +1 because we know we will add ourselves
   for (int i = 0; i < currentListSize; i++) {
      currentList.add(SerializerUtils.readJoiningNode(in));
   }

   // previous list
   final int previousListSize = in.readInt();
   previousList = new ArrayList<JoiningNode>(previousListSize);
   for (int i = 0; i < previousListSize; i++) {
      previousList.add(SerializerUtils.readJoiningNode(in));
   }
}
 
Example 20
Source Project: hbase   Source File: TestDataBlockEncoders.java    License: Apache License 2.0 6 votes vote down vote up
private void testAlgorithm(byte[] encodedData, ByteBuffer unencodedDataBuf,
    DataBlockEncoder encoder) throws IOException {
  // decode
  ByteArrayInputStream bais = new ByteArrayInputStream(encodedData, ENCODED_DATA_OFFSET,
      encodedData.length - ENCODED_DATA_OFFSET);
  DataInputStream dis = new DataInputStream(bais);
  ByteBuffer actualDataset;
  HFileContext meta = new HFileContextBuilder().withHBaseCheckSum(false)
      .withIncludesMvcc(includesMemstoreTS).withIncludesTags(includesTags)
      .withCompression(Compression.Algorithm.NONE).build();
  actualDataset = encoder.decodeKeyValues(dis, encoder.newDataBlockDecodingContext(meta));
  actualDataset.rewind();

  // this is because in case of prefix tree the decoded stream will not have
  // the
  // mvcc in it.
  assertEquals("Encoding -> decoding gives different results for " + encoder,
      Bytes.toStringBinary(unencodedDataBuf), Bytes.toStringBinary(actualDataset));
}
 
Example 21
Source Project: kylin   Source File: TrieDictionaryTest.java    License: Apache License 2.0 6 votes vote down vote up
private static TrieDictionary<String> testSerialize(TrieDictionary<String> dict) {
    try {
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        DataOutputStream dataout = new DataOutputStream(bout);
        dict.write(dataout);
        dataout.close();
        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
        DataInputStream datain = new DataInputStream(bin);
        TrieDictionary<String> r = new TrieDictionary<String>();
        r.readFields(datain);
        datain.close();
        return r;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 22
Source Project: Social   Source File: NativeRuntime.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 执行本地用户命令
 * @date 2016-1-18 下午8:23:01
 * @param pacaageName
 * @param command
 * @param sb_out_Result
 * @return
 */
public boolean RunLocalUserCommand(String pacaageName, String command, StringBuffer sb_out_Result) {
    Process process = null;
    try {
        process = Runtime.getRuntime().exec("sh"); // 获得shell进程
        DataInputStream inputStream = new DataInputStream(process.getInputStream());
        DataOutputStream outputStream = new DataOutputStream(process.getOutputStream());
        outputStream.writeBytes("cd /data/data/" + pacaageName + "\n"); // 保证在command在自己的数据目录里执行,才有权限写文件到当前目录
        outputStream.writeBytes(command + " &\n"); // 让程序在后台运行,前台马上返回
        outputStream.writeBytes("exit\n");
        outputStream.flush();
        process.waitFor();
        byte[] buffer = new byte[inputStream.available()];
        inputStream.read(buffer);
        String s = new String(buffer);
        if (sb_out_Result != null)
            sb_out_Result.append("CMD Result:\n" + s);
    } catch (Exception e) {
        if (sb_out_Result != null)
            sb_out_Result.append("Exception:" + e.getMessage());
        return false;
    }
    return true;
}
 
Example 23
Source Project: grpc-java   Source File: TrafficControlProxy.java    License: Apache License 2.0 5 votes vote down vote up
private void startWorkers() throws IOException {
  DataInputStream clientIn = new DataInputStream(clientSock.getInputStream());
  DataOutputStream clientOut = new DataOutputStream(serverSock.getOutputStream());
  DataInputStream serverIn = new DataInputStream(serverSock.getInputStream());
  DataOutputStream serverOut = new DataOutputStream(clientSock.getOutputStream());

  MessageQueue clientPipe = new MessageQueue(clientIn, clientOut);
  MessageQueue serverPipe = new MessageQueue(serverIn, serverOut);

  executor.execute(new Reader(clientPipe));
  executor.execute(new Writer(clientPipe));
  executor.execute(new Reader(serverPipe));
  executor.execute(new Writer(serverPipe));
}
 
Example 24
Source Project: birt   Source File: FixedLayoutPageHintReader.java    License: Eclipse Public License 1.0 5 votes vote down vote up
synchronized public IPageHint getPageHint( long pageNumber )
		throws IOException
{
	long indexOffset = getHintOffset( pageNumber );
	indexStream.seek( indexOffset );
	long offset = indexStream.readLong( );
	hintsStream.seek( offset );
	return readPageHint( version, new DataInputStream( hintsStream ) );
}
 
Example 25
Source Project: openxds   Source File: LinesOfFile.java    License: Apache License 2.0 5 votes vote down vote up
public LinesOfFile(File file) {
	this.file = file;
	strLine = null;
	try{
		// Open the file that is the first 
		// command line parameter
		fstream = new FileInputStream(file);
		// Get the object of DataInputStream
		in = new DataInputStream(fstream);
		br = new BufferedReader(new InputStreamReader(in));
	} catch (Exception e) {
		System.err.println("Error: " + e.getMessage());
	}	
}
 
Example 26
Source Project: birt   Source File: TextContent.java    License: Eclipse Public License 1.0 5 votes vote down vote up
protected void readField( int version, int filedId, DataInputStream in,
		ClassLoader loader ) throws IOException
{
	switch ( filedId )
	{
		case FIELD_TEXT :
			text = IOUtil.readString( in );
			break;
		default :
			super.readField( version, filedId, in, loader );
	}
}
 
Example 27
Source Project: mochadoom   Source File: line_t.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public void read(DataInputStream f)
        throws IOException {

    // For histerical reasons, these are the only parts of line_t that
    // are archived in vanilla savegames. Go figure.
    this.flags = DoomIO.readLEShort(f);
    this.special = DoomIO.readLEShort(f);
    this.tag = DoomIO.readLEShort(f);
}
 
Example 28
/**
    * Reads PLM marker segment and realigns the codestream where the next
    * marker should be found. Informations stored in these fields are
    * currently not taken into account.
    *
    * @param ehs The encoder header stream.
    *
    * @exception IOException If an I/O error occurs while reading from the
    * encoder header stream
    * */
   private void readPLM(DataInputStream ehs) throws IOException{
int length;

length = ehs.readUnsignedShort();
//Ignore all informations contained
ehs.skipBytes(length-2);

       FacilityManager.getMsgLogger().
           printmsg(MsgLogger.INFO,"Skipping unsupported PLM marker");
   }
 
Example 29
Source Project: vertexium   Source File: DataInputStreamUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static Direction decodeDirection(DataInputStream in) throws IOException {
    byte direction = in.readByte();
    switch (direction) {
        case 'I':
            return Direction.IN;
        case 'O':
            return Direction.OUT;
        default:
            throw new VertexiumAccumuloIteratorException(String.format("Unhandled direction: 0x%02x", direction));
    }
}
 
Example 30
Source Project: classpy   Source File: Mutf8Decoder.java    License: MIT License 5 votes vote down vote up
/**
 * Decode modified UTF-8 string from byte[].
 * todo: optimize
 * 
 * @param bytes
 * @return 
 * @throws java.io.IOException 
 */
public static String decodeMutf8(byte[] bytes) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(bytes.length + 2);
    DataOutputStream dos = new DataOutputStream(baos);
    dos.writeShort(bytes.length);
    dos.write(bytes);

    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    DataInputStream dis = new DataInputStream(bais);
    return dis.readUTF();
}