Changes between Initial Version and Version 1 of Ticket #66


Ignore:
Timestamp:
11/15/2011 05:05:30 PM (14 years ago)
Author:
huangxianduan
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #66 – Description

    initial v1  
    11115. 实在没办法,还是返回头仔细检查代码的写入和读取的判断是否一致,最后在代码上发现了写的判断有问题,代码如下: 
    1212{{{ 
     13#!java 
    1314public void setContent(byte[] content) { 
    1415        if (content != null){ 
     
    4243        this.content = content; 
    4344} 
     45再来看看读取的方法 
     46public byte[] getContent() { 
     47        if (!zip) { 
     48                return content; 
     49        } else {//只要数据库标识是GZIP的就用GZIP解压读取 
     50                byte[] result = null; 
     51                ByteArrayInputStream bis = null; 
     52                GZIPInputStream gzip = null; 
     53                ByteArrayOutputStream baos = null; 
     54                try { 
     55                        bis = new ByteArrayInputStream(content); 
     56                        gzip = new GZIPInputStream(bis); 
     57                        byte[] buf = new byte[1024]; 
     58                        int num = -1; 
     59                        baos = new ByteArrayOutputStream(); 
     60                        while ((num = gzip.read(buf, 0, buf.length)) != -1) { 
     61                                baos.write(buf, 0, num); 
     62                        } 
     63                        baos.flush(); 
     64                        result = baos.toByteArray(); 
     65                } catch (Exception ex) { 
     66                        throw new RuntimeException(ex); 
     67                } finally { 
     68                        try { 
     69                                if (gzip != null) { 
     70                                        gzip.close(); 
     71                                } 
     72                                if (bis != null) { 
     73                                        bis.close(); 
     74                                } 
     75                                if (baos != null) { 
     76                                        baos.close(); 
     77                                } 
     78                        } catch (Exception e) { 
     79                        } 
     80                } 
     81                return result; 
     82        } 
     83} 
    4484}}} 
    45  
    46  
     85- 问题很明显了,当第一次写入的数据长度大于1024的以GZIP格式写入,但第二次再更新写入是数据长度小于1024是却没有改变GZIP的状态,这样导致读取是还是当做GZIP格式来解压读取,肯定是会报格式不对的异常的。