SavedFileParser 源码分析

package com.taobao.timetunnel.savefile.app;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;

import com.taobao.timetunnel.client.Message;
import com.taobao.timetunnel.client.message.MessageFactory;
import com.taobao.timetunnel.savefile.util.BytesUtil;

public class SavedFileParser {

 public static void main(String[] args) throws IOException {
  convertDataFile("D:/2010_09_09_14_16#d06ca100-e29e-45f7-a9cf-9a6a01a1b487");
 }

 private static void convertDataFile(String path) throws IOException {

  RandomAccessFile srvRandomAccessFile = new RandomAccessFile(new File(path), "r");

  FileWriter fw = new FileWriter(new File(path + ".converted"));
  ByteBuffer dataBuffer = ByteBuffer.allocate(40 * 1024 * 1024);//字节流的容量为40M

  int size = srvRandomAccessFile.read(dataBuffer.array());
  dataBuffer.position(size);
  dataBuffer.flip();//弹一下数据流应该和flush()功能一样
  int count = 0;

  byte[] messLenBytes = new byte[4];
  //每一次写messLenBytes[0]<<24+messLenBytes[1]<<16+messLenBytes[2]<<8+messLenBytes[3]的数据
  //直到写完
  while (dataBuffer.hasRemaining()) {//取出的40M数据流
   dataBuffer.get(messLenBytes, 0, 4);
   int len = BytesUtil.bytesToInt(messLenBytes);
   System.out.println("the len from file in convertDataFile is " + len);
   byte[] messageBuffer = new byte[len];
   if (dataBuffer.remaining() < len) {
    System.out.println("incomplete message " + messageBuffer.length);
    break;
   }
   dataBuffer.get(messageBuffer, 0, len);
   // System.out.println("message Buffer:  "+messageBuffer);
   // System.out.println("messageBuffer len is "+
   // messageBuffer.length);
   // System.out.println("dataBuffer remain is "+
   // dataBuffer.remaining());
   try {
    Message m = MessageFactory.getInstance().createMessageFrom(messageBuffer);
    if (m.isCompressed())
     m.decompress();

    // System.out.println("the len of content is: "+m.getContent().length);
    //     
    // System.out.println("content is: "+m.getContent());
    // System.out.println("content is: "+m.getCreatedTime());
    // System.out.println("content is: "+m.getIpAddress());
    // System.out.println("content is: "+m.getTopic());
    // System.out.println("content is: "+m.getId());
    fw.write(new String(m.getContent(), "UTF-8"));
    fw.flush();
    count++;

   } catch (Throwable e) {
    e.printStackTrace();
    System.out.println("Throwable messageBuffer len is " + messageBuffer.length);
    System.out.println("Throwable dataBuffer remain is " + dataBuffer.remaining());
    System.out.println("Throwable the remain incomplete byte is:" + messageBuffer);
    continue;
   }

  }
  System.out.println("the message in file total count: " + count);
  fw.close();

 }
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值