我想把一个压缩后的输出流转变为输出时解压缩后继续输出。

pqc4391 2009-02-27 09:45:40
附件中一个是输入流的改造,可以实现输入时压缩继续输入,但我想改一个输出时解压缩继续输出就不成功了,请帮我改一个。我的邮箱是pqc4391@yeah.net.成功后我给200分,不够还可以加。
以下是调用测试程序段
try{
BufferedInputStream br1 = new BufferedInputStream(engineCompress(new FileInputStream("/soft/pqcjava/pqcnet/1.txt")));
System.out.println("br1 create!");
File fp=new File("/soft/pqcjava/pqctestzip1.txt");
BufferedOutputStream bo1 = new BufferedOutputStream(engineCompress(new FileOutputStream(fp))); //在此被阻塞了
System.out.println("bo1 create!");
int buflen=8192;
byte b1[]=new byte[buflen];
int readlen=0;
while ((readlen = br1.read(b1,0,buflen)) != -1)
{
System.out.println("count1="+readlen);
bo1.write(b1,0,readlen);

System.out.println("write ="+readlen);
// System.out.println("count1="+count1);
}
br1.close();
bo1.close();
}
catch(Exception e)
{
e.printStackTrace();
}


private static OutputStream engineCompress (OutputStream out)
throws IOException {
return new OutputEngineOutputStream
(new IOStreamInputEngine (out, new GZIPInputStreamFactory ()));
}
private static InputStream engineCompress (InputStream in)
throws IOException {
return new OutputEngineInputStream
(new IOStreamEngine (in, new GZIPOutputStreamFactory ()));
}
...全文
119 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
pqc4391 2009-03-01
  • 打赏
  • 举报
回复
没有关注了吗
uastation 2009-02-28
  • 打赏
  • 举报
回复
沙发..顶起来..楼主
pqc4391 2009-02-28
  • 打赏
  • 举报
回复
package pqcnet.outputstream;
import java.io.*;
import java.util.zip.*;
/**
* An input stream that reads data from an OutputEngine.
*
* @author Copyright (c) 2002 Merlin Hughes <merlin@merlin.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*/
public class OutputEngineOutputStream extends OutputStream {
private static final int DEFAULT_INITIAL_BUFFER_SIZE = 8192;
private InputEngine engine;
private byte[] buffer;
private int index, limit, capacity;
private boolean closed, eof;
public OutputEngineOutputStream (InputEngine engine) throws IOException {
this(engine, DEFAULT_INITIAL_BUFFER_SIZE);
}
public OutputEngineOutputStream(InputEngine engine, int initialBufferSize)
throws IOException {
System.out.println("OutputEngineOutputStream()");
this.engine = engine;
capacity = initialBufferSize;
buffer = new byte[capacity];
System.out.println("OutputEngineOutputStream() end");
engine.initialize (new InputStreamImpl ());
System.out.println("OutputEngineOutputStream() end1");
}
private byte[] one = new byte[1];
public void write (int datum) throws IOException {
one[0] = (byte) datum;
write (one, 0, 1);
}
public void write(byte[] data, int offset, int length)
throws IOException {
System.out.println("outputengineoutputstream write"+offset+"||"+length);
if (data == null) {
throw new NullPointerException ();
} else if
((offset < 0) || (length < 0) || (offset + length > data.length)) {
throw new IndexOutOfBoundsException ();
} else if (eof) {
throw new IOException ("Stream closed");
} else {
writeImpl (data, offset, length);
}
}
public void close () {
eof = true;
}
private void writeImpl (byte[] data, int offset, int length) {
if (index >= limit)
index = limit = 0;
if (limit + length > capacity) {
capacity = capacity * 2 + length;
byte[] tmp = new byte[capacity];
System.arraycopy (buffer, index, tmp, 0, limit - index);
buffer = tmp;
limit -= index;
index = 0;
}
System.arraycopy (data, offset, buffer, limit, length);
limit += length;
}
private class InputStreamImpl extends InputStream {


public int read () throws IOException {
int amount = read (one, 0, 1);
return (amount < 0) ? -1 : one[0] & 0xff;
}
public int read (byte data[], int offset, int length)
throws IOException {
System.out.println("outputengineoutputstream inputstreamimpl read()");
if (data == null) {
throw new NullPointerException ();
} else if
((offset < 0) || (length < 0) || (offset + length > data.length)) {
throw new IndexOutOfBoundsException ();
} else if (closed) {
throw new IOException ("Stream closed");
} else {
while (index >= limit) {
if (eof)
return -1;
engine.execute ();
}
if (limit - index < length)
length = limit - index;
System.arraycopy (buffer, 0, data, offset, length);
// index += length;
limit=limit+length;
return length;
}
}
public long skip (long amount) throws IOException {
if (closed) {
throw new IOException ("Stream closed");
} else if (amount <= 0) {
return 0;
} else {
while (index >= limit) {
if (eof)
return 0;
engine.execute ();
}
if (limit - index < amount)
amount = limit - index;
// index += (int) amount;
return amount;
}
}
public int available () throws IOException {
if (closed) {
throw new IOException ("Stream closed");
} else {
return limit - index;
}
}
public void close () throws IOException {
if (!closed) {
closed = true;
eof=true;
engine.finish ();
}
}
}

}
以下是调用函数
private static OutputStream engineCompress (OutputStream out)
throws IOException {
return new OutputEngineOutputStream
(new IOStreamInputEngine (out, new GZIPInputStreamFactory ()));
}
pqc4391 2009-02-28
  • 打赏
  • 举报
回复
package pqcnet.outputstream;
import java.io.*;
/**
* An incremental data source that writes data to an OutputStream.
*
* @author Copyright (c) 2002 Merlin Hughes <merlin@merlin.org>
*
* This program is free software; you can redistribute
* it and/or modify it under the terms of the GNU
* General Public License as published by the Free
* Software Foundation; either version 2
* of the License, or (at your option) any later version.
*/
public interface InputEngine {
public void initialize (InputStream in) throws IOException;
public void execute () throws IOException;
public void finish () throws IOException;
}
package pqcnet.outputstream;
import java.io.*;
/**
* An output engine that copies data from an InputStream through
* a FilterOutputStream to the target OutputStream.
*
* @author Copyright (c) 2002 Merlin Hughes <merlin@merlin.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*/
public class IOStreamInputEngine implements InputEngine {
private static final int DEFAULT_BUFFER_SIZE = 8192;
private InputStream in;
private InputStreamFactory factory;
private byte[] buffer;
private OutputStream out;
public IOStreamInputEngine ( OutputStream out, InputStreamFactory factory) {
this (out, factory, DEFAULT_BUFFER_SIZE);
}
public IOStreamInputEngine
( OutputStream out,InputStreamFactory factory, int bufferSize) {
this.out = out;
this.factory = factory;
buffer = new byte[bufferSize];
System.out.println("IOStreamInputEngine()");
}
public void initialize (InputStream in) throws IOException {
if (this.in != null) {
throw new IOException ("Already initialised");
} else {
System.out.println("OutputEngineOutputStream() end3");
try{
this.in = factory.getInputStream (in);
}
catch(Exception e1){
System.out.println("OutputEngineOutputStream() end4");
}
System.out.println("iostreaminputengine init");
}
}
public void execute () throws IOException {
System.out.println("iostreaminputengine execute");
out.write(buffer,0,buffer.length);
System.out.println("iostreaminputengine execute out.write");
in.read(buffer,0,buffer.length);
System.out.println("iostreaminputengine execute in.read");
}
public void finish () throws IOException {
out.close ();
}

public static interface InputStreamFactory {
public InputStream getInputStream (InputStream in)
throws IOException;
}
}

package pqcnet.outputstream;
import java.io.*;
import java.util.zip.*;
public class GZIPInputStreamFactory
implements IOStreamInputEngine.InputStreamFactory {
public InputStream getInputStream (InputStream in)
throws IOException {
return new GZIPInputStream (in);
}
}
pqc4391 2009-02-28
  • 打赏
  • 举报
回复
在http://download.csdn.net/source/1051369里有完善的源文件
老紫竹 2009-02-28
  • 打赏
  • 举报
回复
GZIPOutputStreamFactory

鬼才知道这个是啥东西。我又不是神仙。
pqc4391 2009-02-28
  • 打赏
  • 举报
回复
谢谢YidingHe,但你提供的这个两个都是输入流,我已经有了一个输入时压缩的类,我需要一个在输出时解压的类,如果是从输入流里解压可以直接在inputstream上叠加上GZIPInputStream流就行了,我主要是要在outputstream中叠加上解压的功能,就好比一个输入时压缩的类的逆操作。
捏造的信仰 2009-02-28
  • 打赏
  • 举报
回复
楼主直接看看 http://commons.apache.org/sandbox/compress/ 好了。这个包提供楼主要求的功能。

org.apache.commons.compress
Interface Compressor

All Known Implementing Classes:
AbstractCompressor, BZip2Compressor

有这样两个方法:
java.io.InputStream compress(java.io.FileInputStream input)
java.io.InputStream decompress(java.io.FileInputStream inputStream)
pqc4391 2009-02-27
  • 打赏
  • 举报
回复
附件地址:http://download.csdn.net/source/1051369

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧