This question already has answers here:
java.io.StreamCorruptedException: invalid stream header: 54657374
(2 answers)
Closed 4 years ago.
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.
Related
I am running the following program and get the Stream Closed IO Error.
but only in the second loop. the first one works fine.
Can anybody please tell me why? (I checked that the file is existing and not empty.)
private static TimerTask perform(){
//logging on to FTP-Server
InputStream in = client.retrieveFileStream("./htdocs/pwiMain/main.txt");
InputStream pw = client.retrieveFileStream("./htdocs/pwiMain/cred_pwd.txt");
BufferedInputStream inbf = new BufferedInputStream(in);
int bytesRead;
byte[] buffer = new byte[1024];
String wholeFile = null;
String[] contents;
while((bytesRead = inbf.read(buffer)) != -1){
wholeFile = new String(buffer,0,bytesRead);
}
sentPassword = wholeFile.substring(wholeFile.indexOf("#lap"));
inbf.close();
inbf = new BufferedInputStream(pw);
while((bytesRead = inbf.read(buffer)) != -1){ // this is line72 where the error occurrs...
wholeFile = new String(buffer,0,bytesRead);
}
md5hash = wohleFile;
inbf.close();
contents = sentPassword.split("\\r\\n|\\n|\\r");
System.out.println("contents: " + contents[0] + " " + contents[1]);
//check the password
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("ioexception");
} finally {
}
return null;
}
Here is the error message:
java.io.IOException: Stream closed
at java.io.BufferedInputStream.getInIfOpen(BufferedInputStream.java:159)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:286)
at java.io.BufferedInputStream.read(BufferedInputStream.java:345)
at java.io.FilterInputStream.read(FilterInputStream.java:107)
at com.protonmail.taylor.faebl.development.main.perform(main.java:72)
at com.protonmail.taylor.faebl.development.main.main(main.java:23)
Thanks a lot for your help :)
You evidently can't have two retrieval streams active at the same time, which isn't surprising. Just reorder your code:
private static TimerTask perform(){
try {
//logging on to FTP-Server
InputStream in = client.retrieveFileStream("./htdocs/pwiMain/main.txt");
BufferedInputStream inbf = new BufferedInputStream(in);
int bytesRead;
byte[] buffer = new byte[1024];
String wholeFile = null;
String wholeCred = null;
String[] contents;
while((bytesRead = inbf.read(buffer)) != -1){
wholeFile = new String(buffer,0,bytesRead);
}
inbf.close(); // ADDED
InputStream pw = client.retrieveFileStream("./htdocs/pwiMain/cred_pwd.txt");
BufferedInputStream pwbf = new BufferedInputStream(pw);
int pwBytesRead; // YOU DON'T NEED THIS, you could reuse the previous one
byte[] pwBuffer = new byte [1024]; // DITTO
while((pwBytesRead = pwbf.read(pwBuffer)) != -1){
wholeCred = new String(pwBuffer,0,pwBytesRead);
}
pwbf.close(); // ADDED
sentPassword = wholeFile.substring(sentPassword.indexOf("#lap"));
md5hash = wholeCred;
contents = sentPassword.split("\\r\\n|\\n|\\r");
System.out.println("contents: " + contents[0] + " " + contents[1]);
//check the password
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("ioexception");
} finally {
}
return null;
}
There's no point or advantage in the way you're doing it now, you're just wasting space, and as you've discovered it doesn't work.
Of course you will then discover that if either of the inputs exceeds one buffer it won't work, but you didn't ask about that.
I have been writing an updater for my game.
It checks a .version file on drop box and compares it to the local .version file.
If there is any link missing from the local version of the file, it downloads the required link one by one.
This is the error that it shows
Exception in thread "Thread-9" java.lang.OutOfMemoryError: Java heap space
at com.fox.listeners.ButtonListener.readFile(ButtonListener.java:209)
at com.fox.listeners.ButtonListener.readFile(ButtonListener.java:204)
at com.fox.listeners.ButtonListener.UpdateStart(ButtonListener.java:132)
at com.fox.listeners.ButtonListener$1.run(ButtonListener.java:58)
It only shows for some computers though and not all of them this is the readFile method
private byte[] readFile(URL u) throws IOException {
return readFile(u, getFileSize(u));
}
private static byte[] readFile(URL u, int size) throws IOException {
byte[] data = new byte[size];
int index = 0, read = 0;
try {
HttpURLConnection conn = null;
conn = (HttpURLConnection) u.openConnection();
conn.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
InputStream is = conn.getInputStream();
progress_a = 0;
progress_b = data.length;
while(index < data.length) {
read = is.read(data, index, size-index);
index += read;
progress_a = index;
}
} catch(Exception e) {
e.printStackTrace();
}
return data;
}
private byte[] readFile(File f) {
byte[] data = null;
try {
data = new byte[(int)f.length()];
#SuppressWarnings("resource")
DataInputStream dis = new DataInputStream(new FileInputStream(f));
dis.readFully(data);
} catch (IOException e) {
e.printStackTrace();
}
return data;
}
This is the main method that is ran
public void UpdateStart() {
System.out.println("Starting Updater..");
if(new File(cache_dir).exists() == false) {
System.out.print("Creating cache dir.. ");
while(new File(cache_dir).mkdir() == false);
System.out.println("Done");
}
try {
version_live = new Version(new URL(version_file_live));
} catch(MalformedURLException e) {
e.printStackTrace();
}
version_local = new Version(new File(version_file_local));
Version updates = version_live.differences(version_local);
System.out.println("Updated");
int i = 1;
try {
byte[] b = null, data = null;
FileOutputStream fos = null;
BufferedWriter bw = null;
for(String s : updates.files) {
if(s.equals(""))
continue;
System.out.println("Reading file "+s);
AppFrame.pbar.setString("Downloading file "+ i + " of "+updates.files.size());
if(progress_b > 0) {
s = s + " " +(progress_a * 1000L / progress_b / 10.0)+"%";
}
b = readFile(new URL(s));
progress_a = 0;
progress_b = b.length;
AppFrame.pbar.setString("Unzipping file "+ i++ +" of "+updates.files.size());
ZipInputStream zipStream = new ZipInputStream(new ByteArrayInputStream(b));
File f = null, parent = null;
ZipEntry entry = null;
int read = 0, entry_read = 0;
long entry_size = 0;
progress_b = 0;
while((entry = zipStream.getNextEntry()) != null)
progress_b += entry.getSize();
zipStream = new ZipInputStream(new ByteArrayInputStream(b));
while((entry = zipStream.getNextEntry()) != null) {
f = new File(cache_dir+entry.getName());
if(entry.isDirectory())
continue;
System.out.println("Making file "+f.toString());
parent = f.getParentFile();
if(parent != null && !parent.exists()) {
System.out.println("Trying to create directory "+parent.getAbsolutePath());
while(parent.mkdirs() == false);
}
entry_read = 0;
entry_size = entry.getSize();
data = new byte[1024];
fos = new FileOutputStream(f);
while(entry_read < entry_size) {
read = zipStream.read(data, 0, (int)Math.min(1024, entry_size-entry_read));
entry_read += read;
progress_a += read;
fos.write(data, 0, read);
}
fos.close();
}
bw = new BufferedWriter(new FileWriter(new File(version_file_local), true));
bw.write(s);
bw.newLine();
bw.close();
}
} catch(Exception e) {
e.printStackTrace();
return;
}
System.out.println(version_live);
System.out.println(version_local);
System.out.println(updates);
CacheUpdated = true;
if(CacheUpdated) {
AppFrame.pbar.setString("All Files are downloaded click Launch to play!");
}
}
I don't get why it is working for some of my players and then some of my other players it does not i have been trying to fix this all day and i am just so stumped at this point but this seems like its the only big issue left for me to fix.
Either increase the memory allocated to your JVM (How can I increase the JVM memory?), or make sure that the file being loaded in memory isn't gigantic (if it is, you'll need to find an alternate solution, or just read chunks of it at a time instead of loading the entire thing in memory).
Do your update in several steps. Here's some pseudo-code with Java 8. It's way shorter than what you wrote because Java has a lot of built-in tools that you re-write much less efficiently.
// Download
Path zipDestination = Paths.get(...);
try (InputStream in = source.openStream()) {
Files.copy(in, zipDestination);
}
// Unzip
try (ZipFile zipFile = new ZipFile(zipDestination.toFile())) {
for (ZipEntry e: Collections.list(zipFile.entries())) {
Path entryDestination = Paths.get(...);
Files.copy(zipFile.getInputStream(e), entryDestination);
}
}
// Done.
This question already has answers here:
Java sending and receiving file (byte[]) over sockets
(6 answers)
Closed 7 years ago.
I was experimenting with JAVA and found this question online.
Java sending and receiving file (byte[]) over sockets.
Just for curiosity i played with the code in the accepted answer, and with other code i found similar to the question. I tried the accepted answer,yes it works and is very fast. But the problem is Archive files are getting corrupted. So here is other code i tried. The downfall of my experimental code is it consume CPU cycles and takes more time than accepted answer (And i have no idea why it is happening so). So here is my code. Can somebody help me to optimize and improve this code more.
Time Taken by accepted Answer = 11ms for 4 Mb file.
Time taken by my experiment= 4 seconds for same file.
Server.java
public class Server implements Runnable {
private ServerSocket serverSocket = null;
private Socket socket = null;
private ObjectInputStream inStream = null;
public Server() {
}
#Override
public void run() {
try {
serverSocket = new ServerSocket(4445);
socket = serverSocket.accept();
DataInputStream dIn = new DataInputStream(socket.getInputStream());
OutputStream os = socket.getOutputStream();
DataOutputStream outToClient = new DataOutputStream(os);
System.out.println("Connected");
File myFile = new File("lib1.zip");
long flength = myFile.length();
System.out.println("File Length"+flength);
outToClient.writeLong(flength);
FileInputStream fis;
BufferedInputStream bis;
byte[] mybytearray = new byte[8192];
fis = new FileInputStream(myFile);
bis = new BufferedInputStream(fis);
int theByte = 0;
System.out.println("Sending " + myFile.getAbsolutePath() + "(" + myFile.length() + " bytes)");
while ((theByte = bis.read()) != -1) {
outToClient.write(theByte);
// bos.flush();
}
/*int count;
BufferedOutputStream bos= new BufferedOutputStream(os);
while ((count = bis.read(mybytearray))>0) {
bos.write(mybytearray, 0, count);
}*/
bis.close();
socket.close();
} catch (SocketException se) {
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Thread t = new Thread(new Server());
t.start();
}
}
ReceiveFile.java
public class RecieveFile {
public final static int SOCKET_PORT = 4445; // you may change this
String SERVER = "127.0.0.1"; // localhost
ArrayList<String> logmsg = new ArrayList<>();
public static void main(String[] args) {
new RecieveFile();
}
public RecieveFile() {
try (Socket sock = new Socket(SERVER, SOCKET_PORT)) {
System.out.println("Connecting...");
try (OutputStream os = sock.getOutputStream(); DataOutputStream outToServer = new DataOutputStream(os)) {
try (DataInputStream dIn = new DataInputStream(sock.getInputStream())) {
long fileLen, downData;
int bufferSize = sock.getReceiveBufferSize();
long starttime = System.currentTimeMillis();
File myFIle = new File("lib1.zip");
try (FileOutputStream fos = new FileOutputStream(myFIle); BufferedOutputStream bos = new BufferedOutputStream(fos)) {
fileLen = dIn.readLong();
/*for (long j = 0; j <= fileLen; j++) {
int tempint = is.read();
bos.write(tempint);
}*/
downData = fileLen;
int n = 0;
byte[] buf = new byte[8192];
while (fileLen > 0 && ((n = dIn.read(buf, 0, buf.length)) != -1)) {
bos.write(buf, 0, n);
fileLen -= n;
// System.out.println("Remaining "+fileLen);
}
/*while ((n = dIn.read(buf)) > 0) {
bos.write(buf, 0, n);
}*/
bos.flush();
long endtime = System.currentTimeMillis();
System.out.println("File " + myFIle.getAbsolutePath()
+ " downloaded (" + downData + " bytes read) in " + (endtime - starttime) + " ms");
}
}
}
} catch (IOException ex) {
Logger.getLogger(RecieveFile.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
You're copying a byte at a time. This is slow. You're also declaring a byte array but not using it. Try this:
int count;
byte[] buffer = new byte[8192]; // or more, double or quadruple it
while ((count = in.read(buffer)) > 0)
{
out.write(buffer, 0, count);
}
Your solution takes a lot of time probably because you are reading a character at time, instead of all the buffer.
The solution is to use a construct similar to the linked question; the problem you got about corrupted file is really improbable, a malformed TCP packed that pass CRC check is really rare occurrence, and I would blame a bug instead. try to post the code you used. But you can add some hash check on the file and some part of it, if you are concerned about this
Here is a cleaned up version of your code, it should perform faster as it avoids single byte operations:
public class Server implements Runnable {
#Override
public void run() {
try {
ServerSocket serverSocket = new ServerSocket(4445);
Socket socket = serverSocket.accept();
OutputStream os = socket.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
File myFile = new File("lib1.zip");
long flength = myFile.length();
dos.writeLong(flength);
InputStream fis = new FileInputStream(myFile);
byte[] buf = new byte[16*1024]; // 16K
long written = 0;
while ((count = fis.read(buf))>0) {
dos.write(buf, 0, count);
written+=count;
}
if (written != flength)
System.out.println("Warning: file changed");
dos.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
System.exit();
}
}
An possible improvement would be to use NIO with channel.sendTo() but this should already have an acceptable performance. Note you do not need to use buffered streams on reading or writing as you use a larger byte array buffer anyway.
One possible improvement would be to not use the DataOutputStream for the long but poke the 8 bytes of it into the first buffer (array) write.
BTW: writing 4MB in 11ms is 390MB/s, that would be faster than most desktop disks can read and write.
I want read a large number of BitSet objects from a file (12MB). I used following code but only read first object from file and repeated it. thanks
public static void main(String[] args) {
// TODO code application logic here
ObjectInputStream Input = null;
FileInputStream Database = null;
Object Buffer = null;
BitSet H = null;
try
{
Database = new FileInputStream("BloomFilters.txt");
Input = new ObjectInputStream(Database);
while((Buffer = Input.readObject()) != null)
{
H = (BitSet)Buffer;
System.out.println(H);
System.out.println("Yes" );
}
}
catch(Exception e)
{
System.out.println("Exp = " + e.getMessage());
}
and following code create a file of BitSet objects, I want read objects from this file
public class Main {
public static void main(String[] args) {
BloomFilter Set = new BloomFilter(512, 100);
ObjectOutputStream Output = null;
DataInputStream Input = null;
FileOutputStream DBOut = null;
FileInputStream DBIn = null;
String Sequence = "";
try
{
DBOut = new FileOutputStream("Bloomfilters.txt");
Output = new ObjectOutputStream(DBOut);
DBIn = new FileInputStream("DB.txt");
Input = new DataInputStream(DBIn);
while((Sequence = (String) Input.readLine()) != null)
{
Set.clear();
for(int i = 0; i < Sequence.length() - 1; i++)
Set.add((Sequence.substring(i, i + 2)));
BitSet buffer = Set.getBitSet();
Output.writeObject(buffer);
}
Input.close();
Output.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
I think you need a Scanner see this code Java Bitset error with large index. It's a different question but the first loop is to read a large file with numbers into a bitset,
I am trying to zip a long string into a file and to retrieve it.
The following code does not work. What it retrieves is gibberish.
public static void main(String args[]) {
try {
// Creating base for data
StringBuilder sbb = new StringBuilder();
for (int i=0;i<1000;i++)
sbb.append("ùertyty!|").append(Integer.toString(i));
File FileAll = new File(".\\All.data");
FileAll.createNewFile();
// Zipping into file
DeflaterOutputStream g = new DeflaterOutputStream(
new FileOutputStream(FileAll));
OutputStreamWriter osw = new OutputStreamWriter(g);
String base = sbb.toString();
osw.write(base);
osw.close();
FileInputStream ALL_FIS = new FileInputStream(FileAll);
// Re-reading from file
DeflaterInputStream dis = new DeflaterInputStream(ALL_FIS);
InputStreamReader isr = new InputStreamReader(dis);
StringBuilder sb = new StringBuilder();
char[] c = new char[1000];
int count = isr.read(c);
while ( count != -1 ) {
sb.append(c, 0, count);
count = isr.read(c);
}
isr.close();
String retr = sb.toString();
System.out.println("Are equal: " + retr.equals(base));
System.out.println("Base: " + base);
System.out.println("Retr: " + retr);
} catch (IOException e) {
System.err.println(e.toString());
}
}
What am I doing wrong?
P.S.: It seems like DeflaterInputStream does not do its job and returns the content of the file as is.
You need to use a InflaterInputStream (which decompresses) instead of a DeflaterInputStream
http://download.oracle.com/javase/1.5.0/docs/api/java/util/zip/InflaterInputStream.html