2016-03-30 7 views
-1

に私はベース64エンコードで文字列を収縮と膨張するためにコードを書いていますが、私は次のエラーを取得しています:デフレートしてふくらませるのJavaのStringはメモリジップ例外エラー

Exception in thread "main" java.util.zip.ZipException: incorrect header check 
    at java.util.zip.InflaterOutputStream.write(InflaterOutputStream.java:284) 
    at java.io.FilterOutputStream.write(FilterOutputStream.java:108) 
    at serializer.test.SerializerTest.main(SerializerTest.java:43) 

私のコードは次のとおりです。

XsltObject Xslt = new XsltObject(); 
      Xslt.setXslt(readFile("C:\\codebase\\OverallSystem\\EBE_TEMPERED_XMLS\\bank_timestamp-0.xml")); 
      System.out.println("Original String Length: "+ Xslt.getXslt().length()); 
      //JSONObject jsonObj = new JSONObject(Xslt); 
      // System.out.println(jsonObj); 
      //System.out.println("Json Length:" + jsonObj); 

      DeflaterOutputStream outputStream; 

      for (int i = 1; i <= 9; ++i) { 
       ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream(); 
       outputStream = new DeflaterOutputStream(arrayOutputStream, new Deflater(i, true)); 
       outputStream.write(Xslt.getXslt().getBytes()); 
       outputStream.close(); 
       //System.out.println("Deflate (lvl=" + i + ");" + arrayOutputStream.toString("ISO-8859-1")); 
       System.out.println("Deflate (lvl=" + i + ");" + arrayOutputStream.toString("ISO-8859-1").length()); 

       String temp = DatatypeConverter.printBase64Binary(arrayOutputStream.toString("UTF-8").getBytes()); 
       System.out.println(temp); 
       System.out.println("Base 64 len: " + temp.length()); 

       byte[] data =DatatypeConverter.parseBase64Binary(temp); 
       ByteArrayOutputStream inflateArrayOutputStream = new ByteArrayOutputStream(); 
       InflaterOutputStream iis = new InflaterOutputStream(inflateArrayOutputStream, new Inflater()); 
       iis.write(data); 
       iis.close(); 
       System.out.println("Inflate (lvl=" + i + ");" + inflateArrayOutputStream.toString("ISO-8859-1")); 
       System.out.println("Inflate (lvl=" + i + ");" + inflateArrayOutputStream.toString("ISO-8859-1").length()); 

私は間違っていますか?

答えて

0

は、これはすべての私の問題を修正し、すべてのJDKの使用である:

package serializer.test; 

import java.io.BufferedReader; 
import java.io.FileReader; 
import java.io.IOException; 
import java.io.UnsupportedEncodingException; 
import java.util.Arrays; 
import java.util.zip.*; 

import javax.xml.bind.DatatypeConverter; 

public class DeflationApp 
{ 
    private String compressBase64(String stringToCompress, int level) 
      throws UnsupportedEncodingException 
    { 
     byte[] compressedData = new byte[1024]; 
     byte[] stringAsBytes = stringToCompress.getBytes("UTF-8"); 

     Deflater compressor = new Deflater(level, false); 
     compressor.setInput(stringAsBytes); 
     compressor.finish(); 
     int compressedDataLength = compressor.deflate(compressedData); 

     byte[] bytes = Arrays.copyOf(compressedData, compressedDataLength); 
     return DatatypeConverter.printBase64Binary(bytes); 
    } 

    private String decompressToStringBase64(String base64String) 
      throws UnsupportedEncodingException, DataFormatException 
    { 
     byte[] compressedData = DatatypeConverter 
       .parseBase64Binary(base64String); 

     Inflater deCompressor = new Inflater(); 
     deCompressor.setInput(compressedData, 0, compressedData.length); 
     byte[] output = new byte[100000]; 
     int decompressedDataLength = deCompressor.inflate(output); 
     deCompressor.end(); 

     return new String(output, 0, decompressedDataLength, "UTF-8"); 
    } 

    public static void main(String[] args) throws DataFormatException, 
      IOException 
    { 
     DeflationApp m = new DeflationApp(); 
     String strToBeCompressed = readFile(
       "C:\\codebase\\OverallSystem\\MappingMapToEBECommon.xslt") 
       .trim(); 
     for (int i = 1; i <= 9; ++i) 
     { 
      String compressedData = m.compressBase64(strToBeCompressed, i); 
      String deCompressedString = m.decompressToStringBase64(compressedData); 

      System.out.println("Base 64:"); 
      System.out.println("Original Length with level("+i+"): " + strToBeCompressed.length()); 
      System.out.println("Compressed with level("+i+"): " + compressedData.toString()); 
      System.out.println("Compressed with level("+i+") Length: " + compressedData.toString().length()); 
      System.out.println("Decompressed with level("+i+"): " + 
        + deCompressedString.length()); 
      System.out.println("Decompressed with level("+i+"): " + deCompressedString); 
     } 

     for (int i = 1; i <= 9; ++i) 
     { 
      byte[] compressedData = m.compress(strToBeCompressed, i); 
      String deCompressedString = m.decompressToString(compressedData); 

      System.out.println("Without Base 64:"); 
      System.out.println("Original Length with level("+i+"): " + strToBeCompressed.length()); 
      System.out.println("Compressed with level("+i+"): " + new String(compressedData)); 
      System.out.println("Compressed with level("+i+") Length: " + new String(compressedData).length()); 
      System.out.println("Decompressed with level("+i+"): " + 
        + deCompressedString.length()); 
      System.out.println("Decompressed with level("+i+"): " + deCompressedString); 
     } 

    } 

    private byte[] compress(String stringToCompress, int level) throws UnsupportedEncodingException 
    { 
     byte[] compressedData = new byte[1024]; 
     byte[] stringAsBytes = stringToCompress.getBytes("UTF-8"); 

     Deflater compressor = new Deflater(level, false); 
     compressor.setInput(stringAsBytes); 
     compressor.finish(); 
     int compressedDataLength = compressor.deflate(compressedData); 

     return Arrays.copyOf(compressedData, compressedDataLength); 
    } 

    private String decompressToString(byte[] compressedData) throws UnsupportedEncodingException, DataFormatException 
    { 
     Inflater deCompressor = new Inflater(); 
     deCompressor.setInput(compressedData, 0, compressedData.length); 
     byte[] output = new byte[100000]; 
     int decompressedDataLength = deCompressor.inflate(output); 
     deCompressor.end(); 

     return new String(output, 0, decompressedDataLength, "UTF-8"); 
    } 

    public static String readFile(String file) throws IOException 
    { 
     BufferedReader reader = new BufferedReader(new FileReader(file)); 
     String line = null; 
     StringBuilder stringBuilder = new StringBuilder(); 
     String ls = System.getProperty("line.separator"); 

     try 
     { 
      while ((line = reader.readLine()) != null) 
      { 
       stringBuilder.append(line); 
       stringBuilder.append(ls); 
      } 

      return stringBuilder.toString(); 
     } 
     finally 
     { 
      reader.close(); 
     } 
    } 

} 
0

私はあまりにもDeflaterOutputStreamとメモリの問題があった - あなたはそれがデフォルトコンストラクタを使用させている場合、それは動作します。これは正常に動作します:

for (Entry<String, String> entry : valueMap.entrySet()) { 
    String key = entry.getKey(); 
    String value = entry.getValue();      
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    DeflaterOutputStream dos = new DeflaterOutputStream(baos); 
    try { 
     dos.write(value.getBytes()); 
     dos.flush(); 
     dos.close(); 
    } 
    catch (IOException e) { 
     throw new RuntimeException(e); 
    } 
    byte[] zipData = baos.toByteArray(); 
    zipValueMap.put(key, zipData); 
} 

しかしにそれを変更:

ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
Deflater deflater = new Deflater(Deflater.BEST_SPEED); 
DeflaterOutputStream dos = new DeflaterOutputStream(baos, deflater); 

そして、それは私の80グラムを取り、私のミントのシステムをクラッシュJVM Cコードでメモリリークを与えます。それはひどく失敗に私は自分のデフレーターを渡すとき、なぜデフォルトコンストラクタの仕事とはまだです:

public void close() throws IOException { 
    if (!closed) { 
     finish(); 
     if (usesDefaultDeflater) 
      def.end(); 
     out.close(); 
     closed = true; 
    } 
} 

I:

デコードDeflaterOutputStream(javaの1.8_40)私は近い方法でいくつかの特別なコードを見つけます彼らはそれをデフレーターの回避策の問題に置くと推測します。

try { 
    dos.write(value.getBytes()); 
    dos.flush(); 
    dos.close(); 
    deflater.end(); 
} 

そして、いやそれ以上のメモリリーク:

最善の解決策は、ループ内で明示的に呼び出すことでした。それはCサイドからのものであるため、メモリリークも悪いので、JVMエラーを起こすことはありませんでした.40gのRAMをすべて噛んで、スワップ領域で起動しました。私は箱にsshしてそれを殺さなければならなかった。

関連する問題