Java Doc for SocketChannel.java in  » Apache-Harmony-Java-SE » java-package » java » nio » channels » 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 » Apache Harmony Java SE » java package » java.nio.channels 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


java.nio.channels.SelectableChannel
   java.nio.channels.spi.AbstractSelectableChannel
      java.nio.channels.SocketChannel

SocketChannel
abstract public class SocketChannel extends AbstractSelectableChannel implements ByteChannel,ScatteringByteChannel,GatheringByteChannel(Code)
A SocketChannel is a selectable channel for part abstraction of stream connecting socket. The socket method of this class can return the related Socket instance, which can handle the socket.

A socket channel is open but not connected when created by open method. After connected by calling the connect method, it will keep connected before closed. The connection is non-blocking that the connect method is for the initial connection and following finishConnect method is for the final steps of connection. The isConnectionPending method can tell the connection is blocked or not; the isConnected method can tell the socket is connected finally or not.

The shut down operation can be independent and asynchronous for input and output. The shutdownInput method is for input, and can make the following read operation fail as end of stream. If the input is shut down and another thread is pending in read operation, the read will end without effect and return end of stream. The shutdownOutput method is for output, and can make the following write operation throwing a ClosedChannelException. If the output is shut down and another is pending in a write operation, an AsynchronousCloseException will thrown to the pending thread.

Socket channels are thread-safe, no more than one thread can read or write at given time. The connect and finishConnect methods are concurrent each other, when they are processing, other read and write will block.




Constructor Summary
protected  SocketChannel(SelectorProvider selectorProvider)
     Constructor for this class.

Method Summary
abstract public  booleanconnect(SocketAddress address)
     Connect the socket to remote address.

If the channel is blocking, this method will suspend before connection finished or an I/O exception.

abstract public  booleanfinishConnect()
     Complete the connection.

This method is used when the channel is connectable to finish the connection, and the connectable status of a channel means the channel is after initiating in non-blocking mode and calling its connect method.

abstract public  booleanisConnected()
     Answer whether this channel's socket is connected or not.
abstract public  booleanisConnectionPending()
     Answer whether this channel's socket is in connecting or not.
public static  SocketChannelopen()
     Create a open and not-connected socket channel.
public static  SocketChannelopen(SocketAddress address)
     Create a socket channel and connect it to a socket address.

This method perform just as open method following by the connect method.


Parameters:
  address - The socket address to be connected.
abstract public  intread(ByteBuffer target)
     Reads bytes from the channel into the given buffer.

The maximum number of bytes that will be read is the remaining() number of bytes in the buffer when the method invoked.

abstract public  longread(ByteBuffer[] targets, int offset, int length)
     Reads bytes from the channel into a subset of the given buffers.

This method attempts to read all of the remaining() bytes from length byte buffers, in order, starting at targets[offset].

final public synchronized  longread(ByteBuffer[] targets)
     Reads bytes from the channel into all the given buffers.

This method is equivalent to:

 read(targets, 0, targets.length);
 

See Also:   java.nio.channels.ScatteringByteChannel.read(java.nio.ByteBuffer[])
Parameters:
  targets - the array of byte buffers to receive the bytes being read.
abstract public  Socketsocket()
     Return the related socket of this channel, which won't declare public methods that not declared in Socket.
final public  intvalidOps()
     Get the valid operations of this channel.
abstract public  intwrite(ByteBuffer source)
     Writes bytes from the given buffer to the channel.

The maximum number of bytes that will be written is the remaining() number of bytes in the buffer when the method invoked.

abstract public  longwrite(ByteBuffer[] sources, int offset, int length)
     Writes a subset of the given bytes from the buffers to the channel.

This method attempts to write all of the remaining() bytes from length byte buffers, in order, starting at sources[offset].

final public synchronized  longwrite(ByteBuffer[] sources)
     Writes bytes from all the given buffers to the channel.

This method is equivalent to:

 write(buffers, 0, buffers.length);
 


See Also:   java.nio.channels.GatheringByteChannel.write(java.nio.ByteBuffer[])
Parameters:
  sources - the buffers containing bytes to be written.


Constructor Detail
SocketChannel
protected SocketChannel(SelectorProvider selectorProvider)(Code)
Constructor for this class.
Parameters:
  selectorProvider - A instance of SelectorProvider




Method Detail
connect
abstract public boolean connect(SocketAddress address) throws IOException(Code)
Connect the socket to remote address.

If the channel is blocking, this method will suspend before connection finished or an I/O exception. If the channel is non-blocking, this method will return true if the connection is finished at once or return false and the connection must wait finishConnect to finished otherwise.

This method can be called at any moment, and can block other read and write operations while connecting.

This method just execute the same security checks as the connect method of the Socket class.


Parameters:
  address - The address to be connected. true if connection is finished,falseotherwise.
throws:
  AlreadyConnectedException - If the channel is connected already.
throws:
  ConnectionPendingException - A non-blocking connecting is doing on this channel.
throws:
  ClosedChannelException - If the channel is already closed.
throws:
  AsynchronousCloseException - If the channel is closed by another thread while this methodis in operation.
throws:
  ClosedByInterruptException - If another thread interrupts the calling thread while theoperation is in progress. The calling thread will have theinterrupt state set, and the channel will be closed.
throws:
  UnresolvedAddressException - If the address is not resolved.
throws:
  UnsupportedAddressTypeException - If the address type is not supported.
throws:
  SecurityException - If there is a security manager, and the address is notpermitted to access.
throws:
  IOException - Some other IO error occurred.



finishConnect
abstract public boolean finishConnect() throws IOException(Code)
Complete the connection.

This method is used when the channel is connectable to finish the connection, and the connectable status of a channel means the channel is after initiating in non-blocking mode and calling its connect method. It will throw related IOException if the connection failed.

This method will return true if the connection is finished already, and return false if the channel is non-blocking and the connection is not finished yet.

If the channel is in blocking mode, this method will suspend, and return true for connection finished or throw some exception otherwise. The channel will be closed if the connection failed and this method thrown some exception.

This method can be called at any moment, and can block other read and write operations while connecting.

true if the connection is successfully finished,false otherwise.
throws:
  NoConnectionPendingException - If the channel is not connected and the connection is notinitiated.
throws:
  ClosedChannelException - If the channel is already closed.
throws:
  AsynchronousCloseException - If the channel is closed by another thread while this methodis in operation.
throws:
  ClosedByInterruptException - If another thread interrupts the calling thread while theoperation is in progress. The calling thread will have theinterrupt state set, and the channel will be closed.
throws:
  IOException - Some other IO error occurred.



isConnected
abstract public boolean isConnected()(Code)
Answer whether this channel's socket is connected or not. true for this channel's socket is connected;false otherwise.



isConnectionPending
abstract public boolean isConnectionPending()(Code)
Answer whether this channel's socket is in connecting or not. true for the connection is initiated but notfinished; false otherwise.



open
public static SocketChannel open() throws IOException(Code)
Create a open and not-connected socket channel.

This channel is got by openSocketChannel method of the default SelectorProvider instance.

The new created channel which is open but not-connected.
throws:
  IOException - If some IO problem occurs.



open
public static SocketChannel open(SocketAddress address) throws IOException(Code)
Create a socket channel and connect it to a socket address.

This method perform just as open method following by the connect method.


Parameters:
  address - The socket address to be connected. The new opened channel.
throws:
  AsynchronousCloseException - If the channel is closed by another thread while this methodis in operation.
throws:
  ClosedByInterruptException - If another thread interrupts the calling thread while theoperation is in progress. The calling thread will have theinterrupt state set, and the channel will be closed.
throws:
  UnresolvedAddressException - If the address is not resolved.
throws:
  UnsupportedAddressTypeException - If the address type is not supported.
throws:
  SecurityException - If there is a security manager, and the address is notpermitted to access.
throws:
  IOException - Some other IO error occurred.



read
abstract public int read(ByteBuffer target) throws IOException(Code)
Reads bytes from the channel into the given buffer.

The maximum number of bytes that will be read is the remaining() number of bytes in the buffer when the method invoked. The bytes will be read into the buffer starting at the buffer's position.

The call may block if other threads are also attempting to read on the same channel.

Upon completion, the buffer's position() is updated to the end of the bytes that were read. The buffer's limit() is unmodified.


See Also:   java.nio.channels.ReadableByteChannel.read(java.nio.ByteBuffer)
Parameters:
  target - The byte buffer to receive the bytes. The number of bytes actually read.
throws:
  NotYetConnectedException - If the channel is not connected yet.
throws:
  ClosedChannelException - If the channel is already closed.
throws:
  AsynchronousCloseException - If the channel is closed by another thread while this methodis in operation.
throws:
  ClosedByInterruptException - If another thread interrupts the calling thread while theoperation is in progress. The calling thread will have theinterrupt state set, and the channel will be closed.
throws:
  IOException - Some other IO error occurred.



read
abstract public long read(ByteBuffer[] targets, int offset, int length) throws IOException(Code)
Reads bytes from the channel into a subset of the given buffers.

This method attempts to read all of the remaining() bytes from length byte buffers, in order, starting at targets[offset]. The number of bytes actually read is returned.

If a read operation is in progress, subsequent threads will block until the read is completed, and will then contend for the ability to read.


See Also:   java.nio.channels.ScatteringByteChannel.read(java.nio.ByteBuffer[]intint)
Parameters:
  targets - the array of byte buffers into which the bytes will be read.
Parameters:
  offset - the index of the first buffer to read.
Parameters:
  length - the maximum number of buffers to read.
throws:
  NotYetConnectedException - If the channel is not connected yet.
throws:
  ClosedChannelException - If the channel is already closed.
throws:
  AsynchronousCloseException - If the channel is closed by another thread while this methodis in operation.
throws:
  ClosedByInterruptException - If another thread interrupts the calling thread while theoperation is in progress. The calling thread will have theinterrupt state set, and the channel will be closed.
throws:
  IOException - Some other IO error occurred.



read
final public synchronized long read(ByteBuffer[] targets) throws IOException(Code)
Reads bytes from the channel into all the given buffers.

This method is equivalent to:

 read(targets, 0, targets.length);
 

See Also:   java.nio.channels.ScatteringByteChannel.read(java.nio.ByteBuffer[])
Parameters:
  targets - the array of byte buffers to receive the bytes being read. the number of bytes actually read.
throws:
  NotYetConnectedException - If the channel is not connected yet.
throws:
  ClosedChannelException - If the channel is already closed.
throws:
  AsynchronousCloseException - If the channel is closed by another thread while this methodis in operation.
throws:
  ClosedByInterruptException - If another thread interrupts the calling thread while theoperation is in progress. The calling thread will have theinterrupt state set, and the channel will be closed.
throws:
  IOException - Some other IO error occurred.



socket
abstract public Socket socket()(Code)
Return the related socket of this channel, which won't declare public methods that not declared in Socket. The related Socket instance.



validOps
final public int validOps()(Code)
Get the valid operations of this channel. Socket channels support connect, read and write operation, so this method returns ( SelectionKey.OP_CONNECT | SelectionKey.OP_READ | SelectionKey.OP_WRITE ).
See Also:   java.nio.channels.SelectableChannel.validOps Valid operations in bit-set.



write
abstract public int write(ByteBuffer source) throws IOException(Code)
Writes bytes from the given buffer to the channel.

The maximum number of bytes that will be written is the remaining() number of bytes in the buffer when the method invoked. The bytes will be written from the buffer starting at the buffer's position.

The call may block if other threads are also attempting to write on the same channel.

Upon completion, the buffer's position() is updated to the end of the bytes that were written. The buffer's limit() is unmodified.


See Also:   java.nio.channels.WritableByteChannel.write(java.nio.ByteBuffer)
Parameters:
  source - the byte buffer containing the bytes to be written. the number of bytes actually written.
throws:
  NotYetConnectedException - If the channel is not connected yet.
throws:
  ClosedChannelException - If the channel is already closed.
throws:
  AsynchronousCloseException - If the channel is closed by another thread while this methodis in operation.
throws:
  ClosedByInterruptException - If another thread interrupts the calling thread while theoperation is in progress. The calling thread will have theinterrupt state set, and the channel will be closed.
throws:
  IOException - Some other IO error occurred.



write
abstract public long write(ByteBuffer[] sources, int offset, int length) throws IOException(Code)
Writes a subset of the given bytes from the buffers to the channel.

This method attempts to write all of the remaining() bytes from length byte buffers, in order, starting at sources[offset]. The number of bytes actually written is returned.

If a write operation is in progress, subsequent threads will block until the write is completed, and will then contend for the ability to write.


See Also:   java.nio.channels.GatheringByteChannel.write(java.nio.ByteBuffer[]intint)
Parameters:
  sources - the array of byte buffers containing the source of remainingbytes that will be attempted to be written.
Parameters:
  offset - the index of the first buffer to write.
Parameters:
  length - the number of buffers to write. the number of bytes actually written.
throws:
  NotYetConnectedException - If the channel is not connected yet.
throws:
  ClosedChannelException - If the channel is already closed.
throws:
  AsynchronousCloseException - If the channel is closed by another thread while this methodis in operation.
throws:
  ClosedByInterruptException - If another thread interrupts the calling thread while theoperation is in progress. The calling thread will have theinterrupt state set, and the channel will be closed.
throws:
  IOException - Some other IO error occurred.



write
final public synchronized long write(ByteBuffer[] sources) throws IOException(Code)
Writes bytes from all the given buffers to the channel.

This method is equivalent to:

 write(buffers, 0, buffers.length);
 


See Also:   java.nio.channels.GatheringByteChannel.write(java.nio.ByteBuffer[])
Parameters:
  sources - the buffers containing bytes to be written. the number of bytes actually written.
throws:
  NotYetConnectedException - If the channel is not connected yet.
throws:
  ClosedChannelException - If the channel is already closed.
throws:
  AsynchronousCloseException - If the channel is closed by another thread while this methodis in operation.
throws:
  ClosedByInterruptException - If another thread interrupts the calling thread while theoperation is in progress. The calling thread will have theinterrupt state set, and the channel will be closed.
throws:
  IOException - Some other IO error occurred.



Methods inherited from java.nio.channels.spi.AbstractSelectableChannel
final public Object blockingLock()(Code)(Java Doc)
final public SelectableChannel configureBlocking(boolean blockingMode) throws IOException(Code)(Java Doc)
final protected synchronized void implCloseChannel() throws IOException(Code)(Java Doc)
abstract protected void implCloseSelectableChannel() throws IOException(Code)(Java Doc)
abstract protected void implConfigureBlocking(boolean blockingMode) throws IOException(Code)(Java Doc)
final public boolean isBlocking()(Code)(Java Doc)
final public synchronized boolean isRegistered()(Code)(Java Doc)
final public synchronized SelectionKey keyFor(Selector selector)(Code)(Java Doc)
final public SelectorProvider provider()(Code)(Java Doc)
final public SelectionKey register(Selector selector, int interestSet, Object attachment) throws ClosedChannelException(Code)(Java Doc)

Methods inherited from java.nio.channels.SelectableChannel
abstract public Object blockingLock()(Code)(Java Doc)
abstract public SelectableChannel configureBlocking(boolean block) throws IOException(Code)(Java Doc)
abstract public boolean isBlocking()(Code)(Java Doc)
abstract public boolean isRegistered()(Code)(Java Doc)
abstract public SelectionKey keyFor(Selector sel)(Code)(Java Doc)
abstract public SelectorProvider provider()(Code)(Java Doc)
final public SelectionKey register(Selector selector, int operations) throws ClosedChannelException(Code)(Java Doc)
abstract public SelectionKey register(Selector sel, int ops, Object att) throws ClosedChannelException(Code)(Java Doc)
abstract public int validOps()(Code)(Java Doc)

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