- 浏览: 1589382 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (289)
- java 语法基础 (51)
- spring (8)
- mvc struct /Ant --build.xml (8)
- SOA (0)
- oracle 9i/10g (23)
- sql server 2000-2005 (3)
- 数据库基础知识 (6)
- 设计模式与软件架构 (10)
- Hibernate 持久化 (9)
- J2SE/J2EE/J2ME/AJAX 技术 (8)
- JSF 技术 (3)
- JAVA 图形化 (0)
- JMS (40)
- Eclipse 3.2 IDE 开发技巧 (13)
- 项目处理方法集合 (2)
- html/jsp/javascript (2)
- Unix/Linux (9)
- j2me/ARM/windriver/嵌入式 (4)
- 电信科学 (8)
- jsp (1)
- c/c++ (1)
- LZW压缩算法(java) (2)
- Android (77)
- 版本管理git/svn (2)
最新评论
-
huihai:
有demo吗?
NamingStrategy实现动态表名映射 -
cangbaotu:
推荐给大家一些有用的爬虫源码:https://github.c ...
网络爬虫(源代码参考) -
tuspark:
除了.classpath文件以外,.project文件也应该了 ...
Eclipse .classpath文件浅谈 -
tuspark:
造成eclipse自动关闭的原因有很多,这里有很多介绍:ecl ...
eclipse 自动关闭 解决方案 -
DEMONU:
网上都是这些,这种文章。。。
ActiveMQ中的消息持久性
共7个文件:
1:
package LZW;
/*
* LZW.java
*
* Created on 01 Dec 2005
*
* Implementation of LZW compression/decompression algorithm
*/
import java.io.* ;
/**
*
*
* @author Moshe Fresko
* @course Algorithmic Programming 1
* @exercise 3
*/
public class LZW implements Compression
{
boolean stopped = false ;
Dict dict ;
// The bits that should be written for each code
int numOfBits ;
// The previous string that we should remember
// in order to insert into the dictionary
final ByteArray emptyBA = new ByteArray() ;
ByteArray w=emptyBA ;
// Constructor gets the number of bits to be written for each code
public LZW()
{
numOfBits = 12 ;
// Create a new Limited Dictionary
// For maximum of 2^bits entries
dict = new LimitedDict(1<<numOfBits) ;
// Add all ascii characters to the dictionary
for (int i=0;i<256;++i)
dict.add(new ByteArray((byte)i)) ;
}
// Encodes the next character.
// If there is a code generated returns it.
// If not returns -1.
int encodeOneChar(int n) {
byte c = (byte) n ;
ByteArray nw = w.conc(c) ;
int code = dict.numFromStr(nw) ;
// if it exists then we continue to search for a longer string
if (code!=-1) {
w = nw ;
return -1 ;
} else {
dict.add(nw) ;
nw = w ;
w = new ByteArray(c) ;
return dict.numFromStr(nw) ;
}
}
// If there is something left in w, returns its code
int encodeLast() {
ByteArray nw = w ;
w = emptyBA ;
return dict.numFromStr(nw) ;
}
// Write the code in bits into output stream
void writeCode(OutputStream os, int code) throws IOException
{
for (int i=0;i<numOfBits;++i) {
os.write(code&1) ;
code /= 2 ;
}
}
int readCode(InputStream is) throws IOException
{
int num = 0 ;
for (int i=0;i<numOfBits;++i) {
int next = is.read() ;
if (next<0)
return -1 ;
num += next<<i ;
}
return num ;
}
// We need to call the close() method of BitOutputStream,
// but without closing the encompassing OutputStream
private class UnClosedOutputStream extends FilterOutputStream {
public UnClosedOutputStream(OutputStream os)
{ super(os) ; }
public void write(byte b[], int off, int len) throws IOException
{ out.write(b,off,len) ; }
// Does not close anything
public void close() throws IOException
{ }
}
public void compress(InputStream is, OutputStream os) throws IOException {
os = new BitOutputStream(new UnClosedOutputStream(os)) ;
int next ; // next input character
int code ; // next code generated
while ((next=is.read())>=0) {
if (stopped)
break ;
code = encodeOneChar(next) ;
if (code>=0)
writeCode(os,code) ;
}
code = encodeLast() ;
if (code>=0)
writeCode(os,code) ;
os.close() ;
}
ByteArray decodeOne(int code) {
// Either "ABA" or null, w="AB"
ByteArray str = dict.strFromNum(code) ;
if (str==null) {
str = w.conc(w.getAt(0)) ;
dict.add(str) ;
} else
if (! w.isEmpty())
dict.add(w.conc(str.getAt(0))) ;
w = str ;
return w ;
}
public void decompress(InputStream is, OutputStream os) throws IOException {
is = new BitInputStream(is) ;
ByteArray str ; // Next entry
int code ; // Next code to be read
while ((code=readCode(is))>=0) {
if (stopped)
break ;
str = decodeOne(code) ;
os.write(str.getBytes()) ;
}
}
public void stop()
{ stopped = true ; }
public static void main(String args[]){
LZW lzw=new LZW();
try{
lzw.compress(new FileInputStream("D:/des_test/src/LZW/TEST.PNG"),new FileOutputStream("D:/des_test/src/LZW/test.lzw"));
//lzw.decompress(new FileInputStream("D:/des_test/src/LZW/test.lzw"),new FileOutputStream("D:/des_test/src/LZW/TEST1.PNG"));
//lzw.compress(new FileInputStream("LZW.JAVA"),new FileOutputStream("lzw.lzw"));
//lzw.decompress(new FileInputStream("lzw.lzw"),new FileOutputStream("lzw1.java"));
}catch(Exception e){
}
}
}
==========
2.
package LZW;
// Keeps a limited sized dictionary
public class LimitedDict extends Dict {
int maxSize ;
LimitedDict(int maxSize)
{ this.maxSize = maxSize ; }
public void add(ByteArray str)
{ if (size()<maxSize)
super.add(str) ; }
}
==============
3.
package LZW;
import java.util.* ;
// The dictionary
public class Dict {
// mp keeps : Word => Index
// ls keeps : Index => Word
Map mp = new HashMap() ;
List ls = new ArrayList() ;
// Adds an element into the dictionary
public void add(ByteArray str)
{ mp.put(str,new Integer(ls.size())) ;
ls.add(str) ;
}
// Gets the number for the given string.
// If it does not exist, returns -1
public final int numFromStr(ByteArray str)
{ return ( mp.containsKey(str) ?
((Integer)mp.get(str)).intValue() :
-1 ) ; }
// Gets the string for the given number
// If the number does not exist, return null
public final ByteArray strFromNum(int i)
{ return ( i<ls.size() ?
(ByteArray) ls.get(i) :
null ) ; }
public final int size()
{ return ls.size() ; }
} ;
================
4.
package LZW;
/*
* Compression.java
*
* Created on 01 Dec 2005
*
* Interface for any compression algorithm
*/
import java.io.* ;
/**
*
* @author Moshe Fresko
* @course Algorithmic Programming 1
* @exercise 2
*/
interface Compression
{
// Gets the input from the Input Stream
// and writes the encoded code into Output Stream
void compress(InputStream inp, OutputStream out) throws IOException ;
// Gets the already encoded input stream
// Decodes it and writes into output stream
void decompress(InputStream inp, OutputStream out) throws IOException ;
}
==============
5.
package LZW;
// ByteArray is used instead of String,
// so that we will have no conversion from/to char,
// Conversion to (char) from int is not recognized for some specific characters
public class ByteArray {
// The single member variable is a byte array kept inside, it is immutable
final byte[] arr ;
// Constructor with a byte arrays will clone it, since we dont want access from outside
ByteArray(byte[] b)
{ arr = (byte[]) b.clone() ; }
// Default Constructor, 0 length array
ByteArray()
{ arr = new byte[0] ; }
// Constructor with a single byte
ByteArray(byte b)
{ arr = new byte[] { b } ; }
// For the hash-table we need this
public boolean equals(Object o)
{ ByteArray ba = (ByteArray) o ;
return java.util.Arrays.equals(arr,ba.arr) ; }
// For the hash-table we need to give a hash code. (Change must be done for a better hash code)
public int hashCode()
{ int code = 0 ;
for (int i=0;i<arr.length;++i)
code = code*2+arr[i] ;
return code ; }
// returns the size of the byte array
public int size()
{ return arr.length ; }
// returns the byte in a given position
byte getAt(int i)
{ return arr[i] ; }
// concatenates another byte array into this one,
// and returns the concatenation in another newly created one. (ByteArray is immutable)
public ByteArray conc(ByteArray b2)
{ int sz = size()+b2.size() ;
byte[] b = new byte[sz] ;
for (int i=0;i<size();++i) b[i]=getAt(i) ;
for (int i=0;i<b2.size();++i) b[i+size()]=b2.getAt(i) ;
return new ByteArray(b) ; }
// Concatenates a byte into this ByteArray.
// The result is returned in a new ByteArray. (ByteArray is immutable)
public ByteArray conc(byte b2)
{ return conc(new ByteArray(b2)) ; }
// Returns a byte array of the copy of the inner byte arrays
public byte[] getBytes()
{ return (byte[]) arr.clone() ; }
// Checks if it is zero length
public boolean isEmpty()
{ return size()==0 ; }
// Drops the last character and returns it
public byte getLast()
{ return arr[size()-1] ; }
public ByteArray dropLast()
{ byte[] newarr = new byte[size()-1] ;
for (int i=0;i<newarr.length;++i)
newarr[i] = arr[i] ;
return new ByteArray(newarr) ; }
public String toString()
{ return new String(arr) ; }
}
==========
6.
package LZW;
/*
* BitOutputStream.java
*
* Created on 01 Dec 2003
*/
import java.io.* ;
/**
*
* @author Moshe Fresko
* @course Algorithmic Programming 1
* @exercise 2
*/
public class BitOutputStream extends FilterOutputStream
{
class BitManager {
int buf = 0 ;
int cnt = 0 ;
// Returns -1 if there is nothing yet to be written
int writeOne(int next)
{ int ret = -1 ;
buf=buf*2+next ;
cnt++ ;
if (cnt==7) {
cnt = 0 ;
ret = buf ;
buf = 0 ;
} else {
ret = -1 ;
}
return ret ; }
//
int writeLast()
{ int x=0 ;
for (int i=0;i<7-cnt;++i)
x=x*2+1 ;
for (int i=7-cnt;i<8;++i)
x=x*2 ;
return buf|x ; }
}
BitManager bitManager = new BitManager() ;
/** Constructor creates a new instance of BitOutputStream,
A decarotor to OutputStream, via FilterOutputStream */
public BitOutputStream(OutputStream os)
{ super(os) ; }
/** Writes a single bit into the included stream.
Although the input is a single bit, it is given as an int.
If it is non-zero, it is threated as 1. */
public void write(int i) throws IOException
{ int x = bitManager.writeOne(i>=1?1:0) ;
if (x>=0)
out.write(x) ; }
/** Writes a list of bits given in the byte array as 0's and 1's */
public void write(byte[] arr) throws IOException
{ write(arr,0,arr.length) ; }
public void write(byte[] arr, int off, int len) throws IOException
{ int clen = 0 ;
for (int i=0;i<len;++i) {
int x = bitManager.writeOne(arr[off+i]) ;
if (x>=0)
arr[off+(clen++)]=(byte)x ;
}
out.write(arr,off,clen) ; }
/** Closes the included stream. Before closing flushes the necessary buffer.
Flush writes the partial byte kept in the internal buffer. */
public void close() throws IOException
{ out.write(bitManager.writeLast()) ;
out.close(); }
// "Main" reads a file in the form of characters of '0's and '1's
// and prints them as bits into another file as a BitStream
public static void main(String[] args)
{
if (args.length<2)
{
System.out.println("Usage: java BitOutputStream FromFile ToFile") ;
System.out.println("where 'FromFile' includes characters of '0' and '1'") ;
System.out.println("and they are written as bits into 'ToFile'") ;
System.exit(1) ;
}
try {
InputStream is = new BufferedInputStream(new FileInputStream(args[0])) ;
OutputStream os = new BitOutputStream(new BufferedOutputStream(new FileOutputStream(args[1]))) ;
int next ;
while ((next=is.read())>=0) {
char ch = (char) next ;
if (ch=='0' || ch=='1')
os.write((int)(ch-'0')) ;
}
is.close() ;
os.close() ;
} catch (FileNotFoundException fnfe) {
System.out.println(args[0]+" file not found") ;
System.exit(1) ;
} catch (IOException ioe) {
System.out.println("Error in reading file "+args[0]+" or writing file "+args[1]) ;
System.exit(1) ;
}
}
}
===========
7.
package LZW;
/*
* BitInputStream.java
*
* Created on 08 Dec 2005
*/
import java.io.* ;
/**
*
* @author Moshe Fresko
* @course Algorithmic Programming 1
* @exercise 1
*/
public class BitInputStream extends FilterInputStream
{
/** Constructor creates a new instance of BitInputStream,
A decarotor to InputStream, via FilterInputStream */
public BitInputStream(InputStream is)
{ super(is) ; }
class BitManager {
// Buffer to keep max of 7 bits (one byte)
private int[] buf = new int[8] ;
// Counter showing the bit number we are reading now
private int cnt = -1 ;
// If we are at the end of the stream
boolean atTheEnd()
{ return ((buf[7]==1)&&(cnt<0)) ; }
// Set the flag for the end of stream
void setTheEnd()
{ buf[7]=1; cnt=-1; }
// No more buffer, means we need to read the next byte
boolean noMoreBuffer()
{ return cnt<0 ; }
// set the buffer
void setNext(int next)
{ // put the bits of the byte into the array
for (cnt=0; cnt<8; ++cnt) {
buf[cnt] = next % 2 ;
next /= 2 ;
}
// if this was the last byte
if (buf[7]==1) {
for (cnt=7;cnt>=0;cnt--)
if (buf[cnt]==0)
break ;
cnt-- ;
} else {
cnt = 6 ;
}
}
// get the next bit
int getNext()
{ return buf[cnt--] ; }
// how many left
int left()
{ return cnt+1 ; }
} ;
BitManager bitManager = new BitManager() ;
byte[] tempBuf = null ;
int tempBufPtr = 0 ;
int tempBufLen = 0 ;
private int readNextByte() throws IOException
{ int val = -1 ;
if (tempBufPtr==tempBufLen)
val = super.read() ;
else {
byte b = tempBuf[tempBufPtr++] ;
if ((b&0x80)>0)
val = ((int)(b&0x7F))|0x80 ;
else
val = b ;
}
return val ;
}
/** Reads a single bit from the included stream.
Returns either 1 or 0, and at the end of stream returns -1. */
public int read() throws IOException {
// If we are already at the end, return -1
if (bitManager.atTheEnd())
return -1 ;
// If we are in the last bit, then refill the buffer
if (bitManager.noMoreBuffer()) {
int i = readNextByte() ;
if (i<0)
bitManager.setTheEnd() ;
else
bitManager.setNext(i) ;
return read() ;
}
// Return the specific bit
return bitManager.getNext() ;
}
/** Reads a list of bits given in the byte array as 0's and 1's*/
public int read(byte[] arr) throws IOException
{ return read(arr,0,arr.length) ; }
public int read(byte[] arr, int off, int len) throws IOException
{ int bytelen = ( (len-bitManager.left()) / 7 ) ;
tempBuf = new byte[bytelen] ;
tempBufLen = in.read(tempBuf) ;
tempBufPtr = 0 ;
for (int i=0;i<len;++i) {
int next = read() ;
if (next<0)
return i ;
arr[off+i]=(byte) next ;
}
return len ;
}
public static void main(String[] args)
{
if (args.length<2)
{
System.out.println("Usage: java BitInputStream FromFile ToFile") ;
System.out.println("where 'FromFile' is a file to be open as a Bit Stream") ;
System.out.println("and they are written as characters of '0's and '1's") ;
System.out.println("every line having one char") ;
System.exit(1) ;
}
try {
InputStream is = new BitInputStream(new BufferedInputStream(new FileInputStream(args[0]))) ;
PrintWriter os = new PrintWriter(new OutputStreamWriter(new FileOutputStream(args[1]))) ;
int next ;
while ((next=is.read())>=0)
os.println(next) ;
is.close() ;
os.close() ;
} catch (FileNotFoundException fnfe) {
System.out.println(args[0]+" file cannot be opened") ;
System.exit(1) ;
} catch (IOException ioe) {
System.out.println("Error in reading file "+args[0]+" or writing file "+args[1]) ;
System.exit(1) ;
}
}
}
相关推荐
标题 "lzw.zip_lzw_lzw_java_zip" 暗示了这个压缩包与使用LZW(Lempel-Ziv-Welch)压缩算法在Java环境下处理ZIP文件有关。LZW是一种广泛应用于数据压缩的无损算法,尤其在文本和图像压缩中常见。在这个压缩包中,...
_lzw_lzw java"这个压缩包中的"assignment-4"文件,很可能是某个项目或作业中用于实现LZW算法的Java代码。使用Java语言实现LZW压缩,可以利用其强大的面向对象特性,实现高效且可维护的代码结构。 1. **压缩过程**...
以下是对LZW算法及其在Java中的实现进行详细讲解。 1. LZW算法原理: LZW算法基于词典编码,它通过查找和更新一个动态词典来压缩数据。首先,词典包含一些基本的字符或字符串,然后通过输入数据流中找到的连续重复...
- **文件操作**:需要读取和写入文件,可以使用Java的`java.io`包中的类,如`FileInputStream`和`FileOutputStream`。 在提供的文件列表中,我们看到有`LZW.BMP`,这可能是一个使用LZW编码压缩的BMP图像文件。`....
在Java编程环境中实现LZW编码和解码,可以帮助开发者理解算法原理,并将其应用到实际项目中。 LZW算法的核心思想是通过构建一个编码表来逐步压缩数据。它分为两个主要阶段:编码和解码。 1. **编码过程**: - **...
在这个"LZW.rar"压缩包中,我们看到的是使用Java语言实现的LZW算法。 LZW算法的主要步骤如下: 1. **初始化字典**:算法开始时,字典包含所有单个字符,每个字符对应一个唯一的编码,通常从256(ASCII字符集的大小...
在Java中实现LZW算法通常涉及以下步骤: 1. **创建字典**:首先,创建一个空字典,用于存储字符串及其对应的编码。 2. **编码**:遍历输入数据,找到最长的已存在于字典中的前缀,然后为这个前缀及其后续字符生成...
在Java中实现LZW压缩与解压缩涉及到以下几个关键步骤和概念: 1. **字典构建**:LZW算法的核心在于动态构建字典。一开始,字典包含所有单个字符。当处理数据时,如果遇到连续的已存在于字典中的字符序列,就将这个...
同时,Java的`java.util.zip`包提供了这些压缩算法的内置支持,方便开发者使用。 总结,LZW压缩算法通过构建动态字典实现数据压缩,其Java实现主要涉及字典管理、编码输出和数据流操作。理解算法原理和实现细节对于...
8. **理解与实现**:“lzw.txt”文件可能包含了对LZW算法的详细步骤解释,或者是一个用特定编程语言(如C, Python, Java等)实现LZW算法的代码示例,对于学习和理解LZW算法来说非常有价值。 通过深入学习和实践...
但在实际应用中,Java的标准库已经提供了`java.util.zip`包,包含了GZIPOutputStream和InflaterInputStream等类,它们基于更高效且广泛应用的GZIP或DEFLATE算法。因此,除非有特定需求,否则通常不建议自定义实现...
在Java和C++这两种编程语言中实现LZW算法,可以理解为以下步骤:** 1. **初始化字典**: 在LZW算法开始时,字典包含所有单个字符,通常是256个ASCII字符。每个字符对应一个唯一的编码,范围从1到256。 2. **输入...
import java.awt.AlphaComposite; import java.awt.Composite; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.GridBagConstraints; import java.awt.Insets; import java.io.IOException;...
在Java编程环境下,我们可以实现LZW压缩和解压缩来处理文件。以下是对LZW算法及其Java实现的详细解释: LZW算法的基本思想是通过构建一个编码表来减少数据的重复性。在压缩过程中,它首先扫描输入数据,查找已存在...
在这个Java实现中,我们可以看到如何将LZW的概念转化为可执行的代码,以实现数据的高效压缩和解压缩。 LZW算法的基本原理是建立一个动态的编码表,该表在压缩过程中不断更新。它首先将输入数据分解成最短的不重复的...
总的来说,通过Java实现LZW压缩是数据压缩领域的一个经典实践,它展示了如何利用编程语言处理和转化信息,以达到节省存储空间和提高传输效率的目的。理解和掌握这种算法,对于深入理解数据压缩原理和提升编程技能都...
在Java环境中实现LZW压缩,主要涉及以下几个关键步骤和概念: 1. **编码表构建**:LZW算法的核心是编码表,它会随着压缩过程动态更新。初始时,编码表包含所有可能的单个字符或字节,每个字符或字节对应一个唯一的...
"LZW_JAVA"和"LZW_C"应该是分别用Java和C语言编写的LZW算法实现源代码,可以通过阅读这些代码加深对算法实现的理解。 总的来说,LZW算法是一种高效的无损压缩技术,其C和Java实现可以帮助开发者理解不同编程语言...
在Java中实现LZW压缩需要理解其基本原理并将其转化为代码。 首先,LZW算法的核心思想是建立一个动态更新的字典。在压缩过程中,字典最初包含所有单个字符,然后随着压缩过程的进行,新发现的连续字符序列会被添加到...