Java Doc for Datagram.java in  » 6.0-JDK-Modules » j2me » javax » microedition » io » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » 6.0 JDK Modules » j2me » javax.microedition.io 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


javax.microedition.io.Datagram

All known Subclasses:   com.sun.cdc.io.j2me.datagram.DatagramObject,
Datagram
public interface Datagram extends DataInput,DataOutput(Code)
This class defines an abstract interface for datagram packets. The implementations of this interface hold data to be sent or received from a DatagramConnection object.

Since this is an interface class, the internal structure of the datagram packets is not defined here. However, it is assumed that each implementation of this interface will provide the following fields / state variables (the actual implementation and the names of these fields may vary):

  • buffer: the internal buffer in which data is stored
  • offset: the read/write offset for the internal buffer
  • length: the length of the data in datagram packet
  • address: the destination or source address
  • read/write pointer: a pointer that is added to the offset to point to the current data location during a read or write operation

Reading and Writing

The Datagram interface extends interfaces DataInput and DataOutput in order to provide a simple way to read and write binary data in and out of the datagram buffer instead of using getData and setData methods. Writing automatically increments length and reading will continue while the read/write pointer is less than length. Before any writing is done reset must be called. If setData() is to be used when reading or writing, any value for the offset parameter other than 0 is not supported.

For example to write to datagram:

 datagram = connection.newDatagram(max);
 // Reset prepares the datagram for writing new message.
 datagram.reset();
 // writeUTF automatically increases the datagram length.
 datagram.writeUTF("hello world");
 connection.send(datagram);
 
For example to read from a datagram (single use only):
 datagram = connection.newDatagram(max);
 connection.receive(datagram);
 message = datagram.readUTF();
 
Reusing Datagrams

It should be noted the length above is returned from getLength and can have different meanings at different times. When sending length is the number of bytes to send. Before receiving length is the maximum number of bytes to receive. After receiving length is the number of bytes that were received. So when reusing a datagram to receive after sending or receiving, length must be set back to the maximum using setLength.

 datagram = connection.newDatagram(max);
 while (notDone) {
 // The last receive in the loop changed the length
 // so put it back to the maximum length.
 datagram.setLength(max);
 connection.receive(datagram);
 data = datagram.getData();
 bytesReceived = datagram.getLength();
 // process datagram ...
 }
 
When reading instead of using getData the reset method must be used.
 datagram = connection.newDatagram(max);
 while (notDone) {
 // The last read in the loop changed the read pointer
 // so reset the pointer.
 datagram.reset();
 datagram.setLength(max);
 connection.receive(datagram);
 message = datagram.readUTF(message);
 // process message ...
 }
 
For example to reread a datagram:
 connection.receive(datagram);
 message = datagram.readUTF(message);
 len = datagram.getLength();
 datagram.reset();
 datagram.setLength(len);
 copy = datagram.readUTF(message);
 

version:
   12/17/01 (CLDC 1.1)
since:
   CLDC 1.0




Method Summary
public  StringgetAddress()
     Get the address of the datagram.
public  byte[]getData()
     Get the contents of the data buffer.

Depending on the implementation, this operation may return the internal buffer or a copy of it.

public  intgetLength()
     Get the length of the datagram.
public  intgetOffset()
     Get the offset.
public  voidreset()
     Zero the read/write pointer as well as the offset and length state variables.
public  voidsetAddress(String addr)
     Set datagram address.
public  voidsetAddress(Datagram reference)
     Set datagram address, copying the address from another datagram.
public  voidsetData(byte[] buffer, int offset, int len)
     Set the buffer, offset and length state variables.
public  voidsetLength(int len)
     Set the length state variable.



Method Detail
getAddress
public String getAddress()(Code)
Get the address of the datagram. the address in string form, or null if no address was set
See Also:   Datagram.setAddress



getData
public byte[] getData()(Code)
Get the contents of the data buffer.

Depending on the implementation, this operation may return the internal buffer or a copy of it. However, the user must not assume that the contents of the internal data buffer can be manipulated by modifying the data returned by this operation. Rather, the setData operation should be used for changing the contents of the internal buffer. the data buffer as a byte array
See Also:   Datagram.setData




getLength
public int getLength()(Code)
Get the length of the datagram. the length state variable
See Also:   Datagram.setLength



getOffset
public int getOffset()(Code)
Get the offset. the offset state variable



reset
public void reset()(Code)
Zero the read/write pointer as well as the offset and length state variables.



setAddress
public void setAddress(String addr) throws IOException(Code)
Set datagram address.

The actual addressing scheme is implementation-dependent. Please read the general comments on datagram addressing in DatagramConnection.java.

Note that if the address of a datagram is not specified, then it defaults to that of the connection.
Parameters:
  addr - the new target address as a URL
exception:
  IllegalArgumentException - if the address is not valid
exception:
  IOException - if a some kind of I/O error occurs
See Also:   Datagram.getAddress




setAddress
public void setAddress(Datagram reference)(Code)
Set datagram address, copying the address from another datagram.
Parameters:
  reference - to the datagram whose address will be copied asthe new target address for this datagram.
exception:
  IllegalArgumentException - if the address is not valid
See Also:   Datagram.getAddress



setData
public void setData(byte[] buffer, int offset, int len)(Code)
Set the buffer, offset and length state variables. Depending on the implementation, this operation may copy the buffer or just set the state variable buffer to the value of the buffer argument. However, the user must not assume that the contents of the internal data buffer can be manipulated by modifying the buffer passed on to this operation.
Parameters:
  buffer - the data buffer
Parameters:
  offset - the offset into the data buffer
Parameters:
  len - the length of the data in the buffer
exception:
  IllegalArgumentException - if the length or offsetor offset plus length fall outside the buffer, or if the buffer parameter is invalid
See Also:   Datagram.getData



setLength
public void setLength(int len)(Code)
Set the length state variable.
Parameters:
  len - the new length of the datagram
exception:
  IllegalArgumentException - if the length or length plus offsetfall outside the buffer
See Also:   Datagram.getLength



www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.