Java无效流头问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2622716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java invalid stream header Problem
提问by David zsl
im writen a client-server app, and now i′m facing a problem that I dont know how to solve:
我写了一个客户端 - 服务器应用程序,现在我面临一个我不知道如何解决的问题:
This is the client:
这是客户端:
try
{
Socket socket = new Socket(ip, port);
ObjectOutputStream ooos = new ObjectOutputStream(socket
.getOutputStream());
SendMessage message = new SendMessage();
message.numDoc = value.numDoc;
message.docFreq = value.docFreq;
message.queryTerms = query;
message.startIndex = startIndex;
message.count = count;
message.multiple = false;
message.ips = null;
message.ports = null;
message.value = true;
message.docFreq = value.docFreq;
message.numDoc = value.numDoc;
ooos.writeObject(message);
ObjectInputStream ois = new ObjectInputStream(socket
.getInputStream());
ComConstants mensajeRecibido;
Object mensajeAux;
String mensa = null;
byte[] by = null;
do
{
mensajeAux = ois.readObject();
if (mensajeAux instanceof ComConstants)
{
System.out.println("Thread by Thread has Search Results");
String test;
ByteArrayOutputStream testo = new ByteArrayOutputStream();
mensajeRecibido = (ComConstants) mensajeAux;
byte[] wag;
testo.write(
mensajeRecibido.fileContent, 0,
mensajeRecibido.okBytes);
wag = testo.toByteArray();
if (by == null) {
by = wag;
}
else {
int size = wag.length;
System.arraycopy(wag, 0, by, 0, size);
}
} else
{
System.err.println("Mensaje no esperado "
+ mensajeAux.getClass().getName());
break;
}
} while (!mensajeRecibido.lastMessage);
//ByteArrayInputStream bs = new ByteArrayInputStream(by.toByteArray()); // bytes es el byte[]
ByteArrayInputStream bs = new ByteArrayInputStream(by);
ObjectInputStream is = new ObjectInputStream(bs);
QueryWithResult[] unObjetoSerializable = (QueryWithResult[])is.readObject();
is.close();
//AQUI TOCARIA METER EL QUICKSORT
XmlConverter xce = new XmlConverter(unObjetoSerializable, startIndex, count);
String serializedd = xce.runConverter();
tempFinal = serializedd;
ois.close();
socket.close();
} catch (Exception e)
{
e.printStackTrace();
}
i++;
}
And this is the sender:
这是发件人:
try
{
QueryWithResult[] outputLine;
Operations op = new Operations();
boolean enviadoUltimo=false;
ComConstants mensaje = new ComConstants();
mensaje.queryTerms = query;
outputLine = op.processInput(query, value);
//String c = new String();
//c = outputLine.toString();
//StringBuffer swa = sw.getBuffer();
ByteArrayOutputStream bs= new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream (bs);
os.writeObject(outputLine);
os.close();
byte[] mybytearray = bs.toByteArray();
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(mybytearray);
BufferedInputStream bis = new BufferedInputStream(byteArrayInputStream);
int readed = bis.read(mensaje.fileContent,0,4000);
while (readed > -1)
{
mensaje.okBytes = readed;
if (readed < ComConstants.MAX_LENGTH)
{
mensaje.lastMessage = true;
enviadoUltimo=true;
}
else
mensaje.lastMessage = false;
oos.writeObject(mensaje);
if (mensaje.lastMessage)
break;
mensaje = new ComConstants();
mensaje.queryTerms = query;
readed = bis.read(mensaje.fileContent);
}
if (enviadoUltimo==false)
{
mensaje.lastMessage=true;
mensaje.okBytes=0;
oos.writeObject(mensaje);
}
oos.close();
} catch (Exception e)
{
e.printStackTrace();
}
}
And this is the error log:
这是错误日志:
Thread by Thread has Search Results
java.io.StreamCorruptedException: invalid stream header: 20646520
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
at org.tockit.comunication.ServerThread.enviaFicheroMultiple(ServerThread.java:747)
at org.tockit.comunication.ServerThread.run(ServerThread.java:129)
at java.lang.Thread.run(Unknown Source)
Where at org.tockit.comunication.ServerThread.enviaFicheroMultiple(ServerThread.java:747)
is this line ObjectInputStream is = new ObjectInputStream(bs);
on the 1st code just after while (!mensajeRecibido.lastMessage);
紧随其后的第一个代码中的org.tockit.comunication.ServerThread.enviaFicheroMultiple(ServerThread.java:747)
这一行在哪里ObjectInputStream is = new ObjectInputStream(bs);
while (!mensajeRecibido.lastMessage);
Any ideas?
有任何想法吗?
回答by Pindatjuh
The value 20646520
is in ASCII @A
.
该值20646520
是 ASCII @A
。
ObjectInput/OutputStreams use a "magic" value at the beginning of the stream, to indicate it complies to the special serialization of the objects. (I think this was 0xCAFEBABE
, but I'm not sure)
ObjectInput/OutputStreams 在流的开头使用“魔法”值,以表明它符合对象的特殊序列化。(我认为这是0xCAFEBABE
,但我不确定)
This means in your situation that something has already read the stream before the ObjectInputStream has the chance to read the magic, or that the stream it reads is not producted by an ObjectOutputStream;
这意味着在您的情况下,在 ObjectInputStream 有机会读取魔法之前已经读取了流,或者它读取的流不是由 ObjectOutputStream 产生的;
You assign the variable by
to wig
(or append), which is a byte array which is not generated by an ObjectOutputStream, as far as I can tell, since it uses mensajeRecipido.fileContent
. I presume mensajeRecipido.fileContent
is the content of an actual file. In this running instance is not of the same format as an ObjectOutputStream, and that's why you get the stream header exception.
您将变量分配by
给wig
(或追加),这是一个字节数组,据我所知,它不是由 ObjectOutputStream 生成的,因为它使用mensajeRecipido.fileContent
. 我认为mensajeRecipido.fileContent
是实际文件的内容。在这个正在运行的实例中,与 ObjectOutputStream 的格式不同,这就是为什么您会收到流标头异常的原因。