MCPcopy Create free account
hub / github.com/apache/cloudstack / doWrite

Method doWrite

utils/src/main/java/com/cloud/utils/nio/Link.java:108–160  ·  view source on GitHub ↗
(SocketChannel ch, ByteBuffer[] buffers, SSLEngine sslEngine)

Source from the content-addressed store, hash-verified

106 }
107
108 private static void doWrite(SocketChannel ch, ByteBuffer[] buffers, SSLEngine sslEngine) throws IOException {
109 SSLSession sslSession = sslEngine.getSession();
110 ByteBuffer pkgBuf = ByteBuffer.allocate(sslSession.getPacketBufferSize() + 40);
111 SSLEngineResult engResult;
112
113 ByteBuffer headBuf = ByteBuffer.allocate(4);
114
115 int totalLen = 0;
116 for (ByteBuffer buffer : buffers) {
117 totalLen += buffer.limit();
118 }
119
120 int processedLen = 0;
121 while (processedLen < totalLen) {
122 headBuf.clear();
123 pkgBuf.clear();
124 engResult = sslEngine.wrap(buffers, pkgBuf);
125 if (engResult.getHandshakeStatus() != HandshakeStatus.FINISHED && engResult.getHandshakeStatus() != HandshakeStatus.NOT_HANDSHAKING &&
126 engResult.getStatus() != SSLEngineResult.Status.OK) {
127 throw new IOException("SSL: SSLEngine return bad result! " + engResult);
128 }
129
130 processedLen = 0;
131 for (ByteBuffer buffer : buffers) {
132 processedLen += buffer.position();
133 }
134
135 int dataRemaining = pkgBuf.position();
136 int header = dataRemaining;
137 int headRemaining = 4;
138 pkgBuf.flip();
139 if (processedLen < totalLen) {
140 header = header | HEADER_FLAG_FOLLOWING;
141 }
142 headBuf.putInt(header);
143 headBuf.flip();
144
145 while (headRemaining > 0) {
146 if (LOGGER.isTraceEnabled()) {
147 LOGGER.trace("Writing Header " + headRemaining);
148 }
149 long count = ch.write(headBuf);
150 headRemaining -= count;
151 }
152 while (dataRemaining > 0) {
153 if (LOGGER.isTraceEnabled()) {
154 LOGGER.trace("Writing Data " + dataRemaining);
155 }
156 long count = ch.write(pkgBuf);
157 dataRemaining -= count;
158 }
159 }
160 }
161
162 /**
163 * write method to write to a socket. This method writes to completion so

Callers 1

writeMethod · 0.95

Calls 6

flipMethod · 0.80
allocateMethod · 0.65
clearMethod · 0.65
getStatusMethod · 0.65
writeMethod · 0.65
getSessionMethod · 0.45

Tested by

no test coverage detected