Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java: invalid type code

Options
  • 12-04-2008 2:10am
    #1
    Registered Users Posts: 590 ✭✭✭


    Hi.

    I'm trying to write a piece of code that reads a file (a serialized java object) from a ftp server. It works perfectly in Linux but for some reason will not work on any Windows machine I've tried. It throws a StreamCorruptedException saying invalid type code 0A. This seems to be happening in the reading of the object using the ObjectInputStream object. Below you can see two different methods I've attempted. I also tried using the InputStream = ftp.retrieve(String fileName) method with the same results. Anyone have any ideas as to what's going on here? It seems to be a Windows specific problem. Note that neither Windows or Linux has any problems writing to the ftp server. And Linux can read the file written by Windows fine.
    try {
    			ftp.connect("ftp.hostname.com");
    
    			int reply = ftp.getReplyCode();
    			if (!FTPReply.isPositiveCompletion(reply)) {
    				ftp.disconnect();
    				JOptionPane.showMessageDialog(null,
    						"FTP server refused connection.");
    				throw new IOException();
    			}
    
    			ftp.login("username", "password");
    
    			ftp.changeWorkingDirectory(folderLocation);
    		
    			/*
    			FileOutputStream fos = new FileOutputStream("C:\\rankings.ser");
    			ftp.retrieveFile(fileName, fos);
    			fos.flush();
    			JOptionPane.showMessageDialog(null, "finished output");
    			ObjectInputStream ois = new ObjectInputStream(new FileInputStream("C:\\rankings.ser"));
    			JOptionPane.showMessageDialog(null, "About to read object");
    			readObject = ois.readObject();
    			JOptionPane.showMessageDialog(null, "object read");
    			fos.close();
    			ois.close();
    			*/
    			
    			ByteArrayOutputStream baos = new ByteArrayOutputStream();			
    			ftp.retrieveFile(fileName, baos);
    			ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    			ObjectInputStream ois = new ObjectInputStream(bais);
    			readObject = ois.readObject();
    			baos.close();
    			ois.close();
    			
    			ftp.logout();
    		} catch (StreamCorruptedException e) {
    			JOptionPane.showMessageDialog(null, "Stream Corrupted Exception");
    			GamePanel.errorMsg += e.getMessage();
    			GamePanel.errorMsg += e.getStackTrace();	
    		} catch (SocketException e) {
    			JOptionPane.showMessageDialog(null, "Socket exception");
    			GamePanel.errorMsg += e.getMessage();
    			GamePanel.errorMsg += e.getStackTrace();
    		} catch (IOException e) {
    			JOptionPane.showMessageDialog(null, "IO exception");
    			GamePanel.errorMsg += e.getMessage();
    			GamePanel.errorMsg += e.getStackTrace();
    		} catch (ClassNotFoundException e) {
    			JOptionPane.showMessageDialog(null, "Class not found");
    			GamePanel.errorMsg += e.getMessage();
    			GamePanel.errorMsg += e.getStackTrace();
    		} finally {
    			if (ftp.isConnected()) {
    				try {
    					ftp.disconnect();
    				} catch (IOException ioe) {
    					JOptionPane.showMessageDialog(null,
    							"Error disconnecting. Details: "
    									+ ioe.getStackTrace());
    				}
    			}
    		}
    

    Any help would be appreciated as I'm really stuck on this one!


Comments

  • Registered Users Posts: 590 ✭✭✭bman


    Got the answer elsewhere. Had to put the connection into binary mode (in case someone else ever gets stuck on this one).


Advertisement