PageRenderTime 57ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/sr-1.0.2.1/util/src/main/java/etch/util/core/nio/StreamHandler.java

#
Java | 106 lines | 50 code | 13 blank | 43 comment | 2 complexity | ecb85909645f27e86025191250648a05 MD5 | raw file
Possible License(s): Apache-2.0
  1. /* $Id: StreamHandler.java 719414 2008-11-20 22:50:55Z sccomer $
  2. *
  3. * Copyright 2007-2008 Cisco Systems Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  6. * use this file except in compliance with the License. You may obtain a copy
  7. * of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. * License for the specific language governing permissions and limitations
  15. * under the License.
  16. */
  17. package etch.util.core.nio;
  18. import java.io.IOException;
  19. import java.nio.ByteBuffer;
  20. import java.nio.channels.SocketChannel;
  21. /**
  22. * Handler for TCP stream channels.
  23. */
  24. public class StreamHandler extends AbstractHandler<SocketChannel>
  25. {
  26. /**
  27. * Constructs the StreamHandler.
  28. * @param channel
  29. * @param wantsConnect
  30. * @throws IOException
  31. */
  32. public StreamHandler( SocketChannel channel, boolean wantsConnect )
  33. throws IOException
  34. {
  35. super( channel );
  36. this.wantsConnect = wantsConnect;
  37. if (!wantsConnect)
  38. connected();
  39. }
  40. private boolean wantsConnect;
  41. @Override
  42. protected final boolean canConnect()
  43. {
  44. return wantsConnect;
  45. }
  46. protected final void doConnect() throws IOException
  47. {
  48. wantsConnect = !channel().finishConnect();
  49. if (!wantsConnect)
  50. connected();
  51. }
  52. protected void connected() throws IOException
  53. {
  54. // nothing to do.
  55. }
  56. /**
  57. * Reads from the channel of this handler.
  58. * @param rbuf the ByteBuffer to read into.
  59. * @return the count of bytes read.
  60. * @throws IOException
  61. */
  62. public int read( ByteBuffer rbuf ) throws IOException
  63. {
  64. return channel().read( rbuf );
  65. }
  66. /**
  67. * Writes to the channel of this handler.
  68. * @param wbuf the ByteBuffer to write from. It is flipped first, and
  69. * compacted after.
  70. * @return the count of bytes written.
  71. * @throws IOException
  72. */
  73. public int write( ByteBuffer wbuf ) throws IOException
  74. {
  75. try
  76. {
  77. wbuf.flip();
  78. return wbuf.hasRemaining() ? channel().write( wbuf ) : 0;
  79. }
  80. finally
  81. {
  82. wbuf.compact();
  83. }
  84. }
  85. /**
  86. * Shuts down output on the channel of this handler.
  87. * @throws IOException
  88. */
  89. public void shutdownOutput() throws IOException
  90. {
  91. channel().socket().shutdownOutput();
  92. }
  93. }