Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RMI - serializing strings into a byte array

Status
Not open for further replies.

TroubleX

Programmer
Dec 3, 2003
3
GB
Hey people, wondering if anyone can help me out here...

the situation is this, I am designing a RMI prog whereby the client sends a string in a byte array to the server.

the interface is this for the particular method
Code:
void putObjectString(int key, byte [] b) throws java.rmi.RemoteException;
	/**
	 * @param key identifies the object in the store (not -1)
	 * @param b the object string sent from the client
	 * @exception java.rmi.RemoteException if there is a 
	 * problem.
	 */
I cant seem to convert my string into the byte array. here is my method to try and do so on the client
Code:
    private void callsendObject()
    {
        try
        {
            byte[] bytes;
            int keyInteger = Integer.parseInt(inputKey.getText());   // get key from GUI
            String StringValue = inputString.getText(); // get string from GUI
            ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
            ObjectOutputStream writebyte = new ObjectOutputStream(byteStream);
            writebyte.write(bytes);
            service.putObjectString(keyInteger, bytes);
        }
        catch(Exception e)
        {  
            display.append("Error sending object. " + e);
        }
    }
I am just wondering if I am near to converting the string to correct way. in my above code i dunno where to insert the StringValue so it gets converted into a byte[] bytes

Thanks in advance for any help you people can give me.


 
Hi,
With RMI, you don't need to serialize the objects you are sending over the network - this all comes for free when using RMI.

To get a byte array out of a String object, all you do is

String myString = "123abc";
byte[] buffer = myString.getBytes();

But using RMI, you can infact just send objects over the wire as long as they implement java.io.Serializable - so you can just send your String object instead of getting the byte data.

What RMI is doing behind the scenes, is infact similar to what you have attempted - ie it serializes and deserializes all objects into byte[] when it transports them over the network.

Ben
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top