- Network Programming with J2ME
- Data Access Using StreamConnection
- Data Access Using ContentConnection
- Conclusion
- Additional Resources
Data Access Using ContentConnection
The ContentConnection interface works the same as the StreamConnection interface except that it supplies a few more helpful methods. Of interest here is the getLength() method, which returns the length of the content that's being provided. To make use of this method, declare a ContentConnection variable:
ContentConnection connStream = null;
I'll move the creation of the byte[] array down inside the try{} clause and change three other items:
Typecast the output of the Connector.open() method to a ContentConnection.
Once I have a ContentConnection, call c.getLength() to retrieve the length of the data.
Once this length is available, dynamically create a byte[] array of that size.
These changes can be seen here (everything else remains the same as in the above example):
ConnStream = (ContentConnection) Connector.open("http://localhost/addressbook.txt"); int len = (int)connStream.getLength(); byte[] b = new byte[len];