Source Code Cross Referenced for SinkChannelImpl.java in  » 6.0-JDK-Platform » solaris » sun » nio » ch » 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 Platform » solaris » sun.nio.ch 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2000-2006 Sun Microsystems, Inc.  All Rights Reserved.
003:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004:         *
005:         * This code is free software; you can redistribute it and/or modify it
006:         * under the terms of the GNU General Public License version 2 only, as
007:         * published by the Free Software Foundation.  Sun designates this
008:         * particular file as subject to the "Classpath" exception as provided
009:         * by Sun in the LICENSE file that accompanied this code.
010:         *
011:         * This code is distributed in the hope that it will be useful, but WITHOUT
012:         * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013:         * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014:         * version 2 for more details (a copy is included in the LICENSE file that
015:         * accompanied this code).
016:         *
017:         * You should have received a copy of the GNU General Public License version
018:         * 2 along with this work; if not, write to the Free Software Foundation,
019:         * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020:         *
021:         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022:         * CA 95054 USA or visit www.sun.com if you need additional information or
023:         * have any questions.
024:         */
025:
026:        package sun.nio.ch;
027:
028:        import java.io.*;
029:        import java.nio.ByteBuffer;
030:        import java.nio.channels.*;
031:        import java.nio.channels.spi.*;
032:
033:        class SinkChannelImpl extends Pipe.SinkChannel implements  SelChImpl {
034:
035:            // Used to make native read and write calls
036:            private static NativeDispatcher nd;
037:
038:            // The file descriptor associated with this channel
039:            FileDescriptor fd;
040:
041:            // fd value needed for dev/poll. This value will remain valid
042:            // even after the value in the file descriptor object has been set to -1
043:            int fdVal;
044:
045:            // ID of native thread doing write, for signalling
046:            private volatile long thread = 0;
047:
048:            // Lock held by current reading thread
049:            private final Object lock = new Object();
050:
051:            // Lock held by any thread that modifies the state fields declared below
052:            // DO NOT invoke a blocking I/O operation while holding this lock!
053:            private final Object stateLock = new Object();
054:
055:            // -- The following fields are protected by stateLock
056:
057:            // Channel state
058:            private static final int ST_UNINITIALIZED = -1;
059:            private static final int ST_INUSE = 0;
060:            private static final int ST_KILLED = 1;
061:            private volatile int state = ST_UNINITIALIZED;
062:
063:            // -- End of fields protected by stateLock
064:
065:            public FileDescriptor getFD() {
066:                return fd;
067:            }
068:
069:            public int getFDVal() {
070:                return fdVal;
071:            }
072:
073:            SinkChannelImpl(SelectorProvider sp, FileDescriptor fd) {
074:                super (sp);
075:                this .fd = fd;
076:                this .fdVal = IOUtil.fdVal(fd);
077:                this .state = ST_INUSE;
078:            }
079:
080:            protected void implCloseSelectableChannel() throws IOException {
081:                synchronized (stateLock) {
082:                    nd.preClose(fd);
083:                    long th = thread;
084:                    if (th != 0)
085:                        NativeThread.signal(th);
086:                    if (!isRegistered())
087:                        kill();
088:                }
089:            }
090:
091:            public void kill() throws IOException {
092:                synchronized (stateLock) {
093:                    if (state == ST_KILLED)
094:                        return;
095:                    if (state == ST_UNINITIALIZED) {
096:                        state = ST_KILLED;
097:                        return;
098:                    }
099:                    assert !isOpen() && !isRegistered();
100:                    nd.close(fd);
101:                    state = ST_KILLED;
102:                }
103:            }
104:
105:            protected void implConfigureBlocking(boolean block)
106:                    throws IOException {
107:                IOUtil.configureBlocking(fd, block);
108:            }
109:
110:            public boolean translateReadyOps(int ops, int initialOps,
111:                    SelectionKeyImpl sk) {
112:                int intOps = sk.nioInterestOps();// Do this just once, it synchronizes
113:                int oldOps = sk.nioReadyOps();
114:                int newOps = initialOps;
115:
116:                if ((ops & PollArrayWrapper.POLLNVAL) != 0)
117:                    throw new Error("POLLNVAL detected");
118:
119:                if ((ops & (PollArrayWrapper.POLLERR | PollArrayWrapper.POLLHUP)) != 0) {
120:                    newOps = intOps;
121:                    sk.nioReadyOps(newOps);
122:                    return (newOps & ~oldOps) != 0;
123:                }
124:
125:                if (((ops & PollArrayWrapper.POLLOUT) != 0)
126:                        && ((intOps & SelectionKey.OP_WRITE) != 0))
127:                    newOps |= SelectionKey.OP_WRITE;
128:
129:                sk.nioReadyOps(newOps);
130:                return (newOps & ~oldOps) != 0;
131:            }
132:
133:            public boolean translateAndUpdateReadyOps(int ops,
134:                    SelectionKeyImpl sk) {
135:                return translateReadyOps(ops, sk.nioReadyOps(), sk);
136:            }
137:
138:            public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl sk) {
139:                return translateReadyOps(ops, 0, sk);
140:            }
141:
142:            public void translateAndSetInterestOps(int ops, SelectionKeyImpl sk) {
143:                if (ops == SelectionKey.OP_WRITE)
144:                    ops = PollArrayWrapper.POLLOUT;
145:                sk.selector.putEventOps(sk, ops);
146:            }
147:
148:            private void ensureOpen() throws IOException {
149:                if (!isOpen())
150:                    throw new ClosedChannelException();
151:            }
152:
153:            public int write(ByteBuffer src) throws IOException {
154:                ensureOpen();
155:                synchronized (lock) {
156:                    int n = 0;
157:                    try {
158:                        begin();
159:                        if (!isOpen())
160:                            return 0;
161:                        thread = NativeThread.current();
162:                        do {
163:                            n = IOUtil.write(fd, src, -1, nd, lock);
164:                        } while ((n == IOStatus.INTERRUPTED) && isOpen());
165:                        return IOStatus.normalize(n);
166:                    } finally {
167:                        thread = 0;
168:                        end((n > 0) || (n == IOStatus.UNAVAILABLE));
169:                        assert IOStatus.check(n);
170:                    }
171:                }
172:            }
173:
174:            public long write(ByteBuffer[] srcs) throws IOException {
175:                if (srcs == null)
176:                    throw new NullPointerException();
177:                ensureOpen();
178:                synchronized (lock) {
179:                    long n = 0;
180:                    try {
181:                        begin();
182:                        if (!isOpen())
183:                            return 0;
184:                        thread = NativeThread.current();
185:                        do {
186:                            n = IOUtil.write(fd, srcs, nd);
187:                        } while ((n == IOStatus.INTERRUPTED) && isOpen());
188:                        return IOStatus.normalize(n);
189:                    } finally {
190:                        thread = 0;
191:                        end((n > 0) || (n == IOStatus.UNAVAILABLE));
192:                        assert IOStatus.check(n);
193:                    }
194:                }
195:            }
196:
197:            public long write(ByteBuffer[] srcs, int offset, int length)
198:                    throws IOException {
199:                if ((offset < 0) || (length < 0)
200:                        || (offset > srcs.length - length))
201:                    throw new IndexOutOfBoundsException();
202:                return write(Util.subsequence(srcs, offset, length));
203:            }
204:
205:            static {
206:                Util.load();
207:                nd = new FileDispatcher();
208:            }
209:
210:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.