/* * Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
// Substitute a native buffer int pos = src.position(); int lim = src.limit(); assert (pos <= lim); int rem = (pos <= lim ? lim - pos : 0);
ByteBuffer bb; if (directIO) {
Util.checkRemainingBufferSizeAligned(rem, alignment);
bb = Util.getTemporaryAlignedDirectBuffer(rem, alignment);
} else {
bb = Util.getTemporaryDirectBuffer(rem);
} try {
bb.put(src);
bb.flip(); // Do not update src until we see how many bytes were written
src.position(pos);
int n = writeFromNativeBuffer(fd, bb, position, directIO, async, alignment, nd); if (n > 0) { // now update src
src.position(pos + n);
} return n;
} finally {
Util.offerFirstTemporaryDirectBuffer(bb);
}
}
privatestaticint writeFromNativeBuffer(FileDescriptor fd, ByteBuffer bb, long position, boolean directIO, boolean async, int alignment,
NativeDispatcher nd) throws IOException
{ int pos = bb.position(); int lim = bb.limit(); assert (pos <= lim); int rem = (pos <= lim ? lim - pos : 0);
if (directIO) {
Util.checkBufferPositionAligned(bb, pos, alignment);
Util.checkRemainingBufferSizeAligned(rem, alignment);
}
int written = 0; if (rem == 0) return 0;
acquireScope(bb, async); try { if (position != -1) {
written = nd.pwrite(fd, bufferAddress(bb) + pos, rem, position);
} else {
written = nd.write(fd, bufferAddress(bb) + pos, rem);
}
} finally {
releaseScope(bb);
} if (written > 0)
bb.position(pos + written); return written;
}
long bytesWritten = nd.writev(fd, vec.address, iov_len);
// Notify the buffers how many bytes were taken long left = bytesWritten; for (int j=0; j<iov_len; j++) { if (left > 0) {
ByteBuffer buf = vec.getBuffer(j); int pos = vec.getPosition(j); int rem = vec.getRemaining(j); int n = (left > rem) ? rem : (int)left;
buf.position(pos + n);
left -= n;
} // return shadow buffers to buffer pool
ByteBuffer shadow = vec.getShadow(j); if (shadow != null)
Util.offerLastTemporaryDirectBuffer(shadow);
vec.clearRefs(j);
}
completed = true; return bytesWritten;
} finally {
releaseScopes(handleReleasers); // if an error occurred then clear refs to buffers and return any shadow // buffers to cache if (!completed) { for (int j=0; j<iov_len; j++) {
ByteBuffer shadow = vec.getShadow(j); if (shadow != null)
Util.offerLastTemporaryDirectBuffer(shadow);
vec.clearRefs(j);
}
}
}
}
long bytesRead = nd.readv(fd, vec.address, iov_len);
// Notify the buffers how many bytes were read long left = bytesRead; for (int j=0; j<iov_len; j++) {
ByteBuffer shadow = vec.getShadow(j); if (left > 0) {
ByteBuffer buf = vec.getBuffer(j); int rem = vec.getRemaining(j); int n = (left > rem) ? rem : (int)left; if (shadow == null) { int pos = vec.getPosition(j);
buf.position(pos + n);
} else {
shadow.limit(shadow.position() + n);
buf.put(shadow);
}
left -= n;
} if (shadow != null)
Util.offerLastTemporaryDirectBuffer(shadow);
vec.clearRefs(j);
}
completed = true; return bytesRead;
} finally {
releaseScopes(handleReleasers); // if an error occurred then clear refs to buffers and return any shadow // buffers to cache if (!completed) { for (int j=0; j<iov_len; j++) {
ByteBuffer shadow = vec.getShadow(j); if (shadow != null)
Util.offerLastTemporaryDirectBuffer(shadow);
vec.clearRefs(j);
}
}
}
}
/** * Returns two file descriptors for a pipe encoded in a long. * The read end of the pipe is returned in the high 32 bits, * while the write end is returned in the low 32 bits.
*/ staticnativelong makePipe(boolean blocking) throws IOException;
staticnativeint write1(int fd, byte b) throws IOException;
/** * Read and discard all bytes.
*/ staticnativeboolean drain(int fd) throws IOException;
/** * Read and discard at most one byte * @return the number of bytes read or IOS_INTERRUPTED
*/ staticnativeint drain1(int fd) throws IOException;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.