【问题标题】:Find End of Block Marker Flash ByteArray with Deflate Compressed Data使用 Deflate 压缩数据查找块标记 Flash ByteArray 的结尾
【发布时间】:2014-03-31 20:41:51
【问题描述】:

我有一些通过网络套接字发送到 Flash 客户端的数据。该数据在发送到客户端之前使用 Java Deflate 类进行压缩。

我得到了一个

错误 #2058:解压缩数据时出错

当通过网络传输的数据很大并调用 byteArray 的 deflate 方法时

Deflate 服务器上的代码 (Scala):

def deflate(str:String):Array[Byte] = {
    val data = str.getBytes
    val deflater = new Deflater(9, true)
    deflater.setInput(data)

    val outputStream = new ByteArrayOutputStream(data.length)
    deflater.finish
    val buffer = new Array[Byte](1024)
    while(!deflater.finished) {
      val count = deflater.deflate(buffer)
      outputStream.write(buffer, 0, count)
    }
    outputStream.close
    val output = outputStream.toByteArray
    output
  }

发送给客户:

def sendToClient(message:String) {
      println("original message length: " + message.length)
      val compressed = deflate(message)
      //wrap the compressed data in base64 encoded string because of a requirement for this on the client          
      val toClient = Base64.encodeBase64String(compressed)
      clientConnection.sendMessage(toClient)
    }

客户:

//read the data into the ByteArray
while(socket.bytesAvaialble > 4) {
    //unwrap base64 encoded stuff
    myByteArray[position] = socket.readUnsignedByte();
}

//inflate the data in the ByteArray
myByteArray.inflate();

当来自 websocket 的数据很大时,对 inflate 的调用会失败,因为并非所有数据都在那里。在调用 inflate() 之前,我想弄清楚如何确保所有数据都在那里

根据this(第9页),我应该寻找一个块头来确定所有压缩内容何时到达。

如何使用 ByteArray API 查找该标头?

【问题讨论】:

  • 我看不到您在哪里解码 Base64 编码。这也不能是您的实际客户端代码,因为您拼错了“可用”。

标签: sockets apache-flex websocket bytearray deflate


【解决方案1】:

找到 deflate 流结束的唯一方法是给它充气。例如,通过简单地查看某些位或字节模式,结尾并不明显。 deflate 流中的所有表和代码都需要按顺序解码,直到遇到结尾。

我不能代表您正在使用的 API,但通常 zlib 的接口允许一次向充气对象提供一段放气流。当压缩数据结束时,inflator 会告诉您,在 deflate 流之后,您将留下未使用的字节。

【讨论】:

  • 感谢您的回复。我已经更新了这个问题,使其更具解释性。我已经为 deflate 调用使用了 nowrapper 参数(请参阅服务器附加的服务器代码)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-12
  • 2010-12-12
  • 1970-01-01
  • 2011-09-06
  • 1970-01-01
  • 2013-05-05
相关资源
最近更新 更多