- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
当我使用长度为 256 的符号表(字典)时,我的 LZW 压缩工作正常,编码器和解码器都使用 256 并且一切正常但是当我将这个数字增加到例如 512、1024、4096 时,解码文件输出是与第一个输入文件不一样...有什么提示吗?
源代码:
LZWEncoder.java:
import java .io .*;
public class LZWEncoder
{
public static void main ( String [] args )
throws FileNotFoundException , IOException
{
File file = new File ("calgary/book1");
File fileOut = new File ( "calgary/book1_enc");
FileInputStream reader = new FileInputStream ( file );
FileOutputStream writer = new FileOutputStream ( fileOut );
int size_st;
long file_size;
file_size = file.length();
size_st = (int) file_size/1024;
System.out.println("File size " + file_size + " Sysmbol tree" + size_st);
if (size_st < 256)
size_st = 256;
else if (size_st < 512)
size_st = 512;
else if (size_st < 1024)
size_st = 1024;
else if (size_st < 2048)
size_st = 2048;
else
size_st = 4096;
byte[] size_stInBytes = (Integer.toString(size_st)+"\n").getBytes();
// writer.write(size_stInBytes);
System.out.println("File size " + file_size + " Sysmbol tree " + size_st);
// input stream with lookahead
LookAheadIn in = new LookAheadIn ( file );
LZWst st = new LZWst (4096); // specialised ST
while (! in.isEmpty ())
{
int codeword = st.getput (in );
writer.write ( codeword );
}
writer.close ();
reader.close ();
}
}
LZWDecoder.java:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
public class LZWDecoder
{
public static void main ( String [] args )
throws FileNotFoundException , IOException
{
File file = new File ("calgary/book1_enc");
Scanner first_line = new Scanner("calgary/book1_enc");
File fileOut = new File ( "calgary/book1_dec2");
FileInputStream reader = new FileInputStream ( file );
FileOutputStream writer = new FileOutputStream ( fileOut );
String size_st;
size_st =first_line.nextLine();
System.out.println(" Sysmbol tree " + size_st);
String [] st = new String [4096];
int i;
for (i=0; i <128; i++)
st[i] = Character.toString (( char ) i);
String prev = "";
int codeword ;
while (( codeword = reader.read())!= -1)
{
String s;
if ( codeword == i) // Tricky situation !
s = prev + prev.charAt(0);
else
s = st[codeword ];
for (int j = 0; j<s.length(); j++)
writer.write(s.charAt (j));
if ( prev.length() > 0 && i < 4096 )
st[i++] = prev + s.charAt(0);
prev = s;
}
writer.close();
reader.close();
}
}
LZWst.java:
import java.io.FileNotFoundException;
import java.io.IOException;
public class LZWst
{
private int i; // next codeword to assign
private int codeword ; // codeword to return
private Node [] roots ; // array of TSTs
private int st_size;
public LZWst (int st_sz)
{
st_size = st_sz;
roots = new Node [128];
for (i=0; i <128; i++) // init with ASCII
roots [i] = new Node (( char ) i,i);
}
private class Node
{ // standard node code
Node (int c, int codeword )
{
this .c = c;
this . codeword = codeword ;
}
int c;
Node left , mid , right ;
int codeword ;
}
public int getput ( LookAheadIn in)
throws FileNotFoundException , IOException
{
int c = in. readChar ();
if ( c == -1) return -1; // EOF
roots [c] = getput (c, roots [c],in );
in. backup ();
return codeword ; // longest prefix
}
public Node getput ( int c, Node x, LookAheadIn in)
throws FileNotFoundException , IOException
{ // recursive search *and* insert
if (x== null ) {
if (i<st_size){
x = new Node (c,i++);
System.out.println("Value of i: " + i);
}
return x;
}
if (c<x.c) x. left = getput (c,x.left ,in );
else if (c>x.c) x. right = getput (c,x.right ,in );
else {
int next = in.readChar();
codeword = x. codeword ;
x.mid = getput (next ,x.mid ,in );
}
return x;
}
}
LookAheadIn.java:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class LookAheadIn
{
private FileInputStream in = null ;
private int last ;
private boolean backup = true ;
public LookAheadIn ( File file )
throws FileNotFoundException , IOException
{
in = new FileInputStream ( file );
last = in. read ();
backup = true ;
}
public void backup () { backup = true ; }
public int readChar () throws FileNotFoundException , IOException
{
if (! backup ) last = in. read();
backup = false ;
return last ;}
public boolean isEmpty(){ return last == -1; }
}
最佳答案
您正在读取字节,它们不能存储更大的值。如果您想使用大于 255 的代码执行 LZW,则需要对比特流进行编码,或者为了测试(作为临时 hack)写入两个字节的字(大小为 16 的无符号整数)。
替换
writer.write ( codeword );
与
writer.write ( codeword>>8 );
writer.write ( codeword&0xff );
然后检查它是否有效。如果是这样,您可以花一些时间实现基于位的流。您还需要更新阅读器和解码器。
关于java - LZW 编码器解码器 - 符号表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17306656/
我有课 class Header { @FCBTag(type="type1") --My custom annotation int a = "valueA"; @FCBTa
我一直在使用 Apache MINA 并正在学习 Netty。我过去使用过 MINA 累积编码器/解码器,我有兴趣看看 Netty 是否有类似的功能。我查看了 API,但没有看到任何内容。 最佳答案
我有一组使用 wsdl2java (Axis 1.4) 创建的类,我正在寻找一种方法来解码和编码来自/到字符串和对象的数据。我已经编写了一个 JAXB 解码器,它适用于我们的一些较新的内部对象,因为我
在我的自定义类 WLNetworkClient 中,我必须实现这样的方法: required init(coder aDecoder: NSCoder) { fatalError("init(
基于 transformer 的编码器-解码器模型是 表征学习 和 模型架构 这两个领域多年研究成果的结晶。本文简要介绍了神经编码器-解码器模型的历史,更多背景知识,建议读者阅读由 Seba
在使用 FFMPEG android java 库时发生以下异常的视频播放速度(使视频变慢)。 [aac @ 0x416c26f0] The encoder 'aac' is experimental
我正在从一个程序运行 ffmpeg,我们自己构建了 ffmpeg(我们没有使用包管理器或预构建的东西安装它)。 这是构建的命令: 2020-07-31 12:14:11.942 INFO ffmpeg
许多基于LSTM的seq2seq编码器-解码器架构教程(例如英法翻译),将模型定义如下: encoder_inputs = Input(shape=(None,)) en_x= Embedding(
如何覆盖使用 marshmallow 的 JSON 编码器库,以便它可以序列化 Decimal字段?我想我可以通过覆盖 json_module 来做到这一点在基地Schema或 Meta课,但我不知道
在我的 Grails 2.5.0 应用程序中,我使用了一组自定义 JSON 编码器来严格控制由我的 REST 端点返回的 JSON 格式。目前我在这样的服务中注册这些编码器 class Marshal
我需要多个自定义 JSON 编码器,因为我想针对不同的目的以不同的方式进行编码。我知道如何使用以下方法设置自定义编码器应用程序: JSON.registerObjectMarshaller(MyCla
查看文档,它是这样说的: https://netty.io/4.0/api/io/netty/channel/ChannelPipeline.html A user is supposed to ha
我希望为以下案例类提供 JSON 编码器: import io.circe.generic.extras.Configuration final case class Hello[T]( so
我正在构建一个 JPEG 图像编码器。就目前情况而言,为了对图像进行编码,用户输入他们希望编码的文件的名称以及由此创建的文件的名称。 我希望用户能够在命令行中设置编码的质量。我尝试重命名 new Jp
我有想要在 webview 中显示的 html 文本。 如specification ,数据必须经过 URI 转义。所以我尝试使用 URLEncoder.encode() 函数,但这对我没有帮助,因为
我目前正在自己实现 PNG 滤镜。我正在使用神经网络尝试创建比当前现有的 PNG 过滤器更好的预测: 0 - 无 1 - 子 2 - 向上 3 - 平均 4 - 派斯 5 - 我的实现(使用神经网
让我们假设我们有与 Schema 一致的 XML 和带有一些公共(public)字段的 Java 类: public clas
在我的 Java 应用程序中,我正在寻找 URLEncoder.encode(String s, String enc) 的流媒体版本.我想使用“application/x-www-form-urle
我确实有一个对象层次结构,我想使用“import javax.xml.bind.Marshaller”将其从 Java 对象转换为 xml。我的java类文件被编码在“Cp1252”中,我无法更改它。
使用 Netty 4.0.27 和 Java 1.8.0_20 所以我试图通过构建一个简单的聊天服务器(我猜是典型的网络教程程序?)来了解 Netty 的工作原理。设计我自己的简单协议(protoco
我是一名优秀的程序员,十分优秀!