Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Java: invalid type code

  • 12-04-2008 02:10AM
    #1
    Registered Users, Registered Users 2 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, Registered Users 2 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