Java无效流头问题 [英] Java invalid stream header Problem

查看:181
本文介绍了Java无效流头问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个客户端 - 服务器应用程序,现在我遇到了一个我不知道如何解决的问题:



这是客户端:

 尝试
{

套接字套接字=新套接字(ip,端口);


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具有搜索结果);

字符串测试;

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);
}


}其他
{

System.err.println(Mensaje no esperado
+ mensajeAux。 。的getClass()的getName());
休息;
}
} 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(例外e)
{
e.printStackTrace();
}

i ++;

}

这是发件人:

 尝试
{

QueryWithResult [] outputLine;

操作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(例外e)
{
e.printStackTrace();
}
}

这是错误日志:

  Thread by Thread有搜索结果
java.io.StreamCorruptedException:无效的流标题:20646520
at java.io. ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream。< init>(Unknown Source)
at org.tockit.comunication.ServerThread.enviaFicheroMultiple(ServerThread.java:747)
org.tockit.comunication.ServerThread.run(ServerThread.java:129)
at java.lang.Thread.run(Unknown Source)

org.tockit.comunication.ServerThread.enviaFicheroMultiple(ServerThread.java:747)的位置此行 ObjectInputStream is = new ObjectInputStream(bs); 在第一个代码之后 while(!mensajeRecibido.lastMessage);



任何想法?

解决方案

价值 20646520 是ASCII @A



ObjectInput / OutputStreams在流的开头使用magic值,表示它符合对象的特殊序列化。 (我认为这是 0xCAFEBABE ,但我不确定)



这意味着在你的情况下已经在ObjectInputStream有机会读取魔法之前读取了流,或者它读取的流不是由ObjectOutputStream生成的;



您指定变量 by wig (或append),这是一个不是由ObjectOutputStream生成的字节数组,据我所知,因为它使用 mensajeRecipido.fileContent 。我认为 mensajeRecipido.fileContent 是实际文件的内容。在此运行实例中,与ObjectOutputStream的格式不同,这就是您获取流标头异常的原因。


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);

Any ideas?

解决方案

The value 20646520 is in 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)

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;

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.

这篇关于Java无效流头问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆