402
403 private int receiveIntoNativeBuffer(FileDescriptor fd, ByteBuffer bb,
404 int rem, int pos)
405 throws IOException
406 {
407 int n = receive0(fd, ((DirectBuffer)bb).address() + pos, rem,
408 isConnected());
409 if (n > 0)
410 bb.position(pos + n);
411 return n;
412 }
413
414 public int send(ByteBuffer src, SocketAddress target)
415 throws IOException
416 {
417 if (src == null)
418 throw new NullPointerException();
419
420 synchronized (writeLock) {
421 ensureOpen();
422 InetSocketAddress isa = (InetSocketAddress)target;
423 InetAddress ia = isa.getAddress();
424 if (ia == null)
425 throw new IOException("Target address not resolved");
426 synchronized (stateLock) {
427 if (!isConnected()) {
428 if (target == null)
429 throw new NullPointerException();
430 SecurityManager sm = System.getSecurityManager();
431 if (sm != null) {
432 if (ia.isMulticastAddress()) {
433 sm.checkMulticast(isa.getAddress());
434 } else {
435 sm.checkConnect(isa.getAddress().getHostAddress(),
436 isa.getPort());
437 }
438 }
439 } else { // Connected case; Check address then write
440 if (!target.equals(remoteAddress)) {
441 throw new IllegalArgumentException(
442 "Connected address not equal to target address");
443 }
444 return write(src);
445 }
446 }
447
448 int n = 0;
449 try {
450 begin();
451 if (!isOpen())
452 return 0;
453 writerThread = NativeThread.current();
454 do {
455 n = send(fd, src, target);
456 } while ((n == IOStatus.INTERRUPTED) && isOpen());
457
458 synchronized (stateLock) {
459 if (isOpen() && (localAddress == null)) {
460 localAddress = Net.localAddress(fd);
461 }
462 }
463 return IOStatus.normalize(n);
464 } finally {
465 writerThread = 0;
466 end((n > 0) || (n == IOStatus.UNAVAILABLE));
467 assert IOStatus.check(n);
468 }
469 }
470 }
471
472 private int send(FileDescriptor fd, ByteBuffer src, SocketAddress target)
473 throws IOException
474 {
475 if (src instanceof DirectBuffer)
476 return sendFromNativeBuffer(fd, src, target);
477
478 // Substitute a native buffer
479 int pos = src.position();
480 int lim = src.limit();
481 assert (pos <= lim);
482 int rem = (pos <= lim ? lim - pos : 0);
483
484 ByteBuffer bb = Util.getTemporaryDirectBuffer(rem);
485 try {
486 bb.put(src);
487 bb.flip();
488 // Do not update src until we see how many bytes were written
489 src.position(pos);
490
491 int n = sendFromNativeBuffer(fd, bb, target);
492 if (n > 0) {
493 // now update src
494 src.position(pos + n);
495 }
496 return n;
497 } finally {
498 Util.releaseTemporaryDirectBuffer(bb);
499 }
500 }
501
502 private int sendFromNativeBuffer(FileDescriptor fd, ByteBuffer bb,
503 SocketAddress target)
504 throws IOException
505 {
506 int pos = bb.position();
507 int lim = bb.limit();
508 assert (pos <= lim);
509 int rem = (pos <= lim ? lim - pos : 0);
510
511 boolean preferIPv6 = (family != StandardProtocolFamily.INET);
512 int written;
513 try {
514 written = send0(preferIPv6, fd, ((DirectBuffer)bb).address() + pos,
515 rem, target);
516 } catch (PortUnreachableException pue) {
517 if (isConnected())
518 throw pue;
519 written = rem;
520 }
521 if (written > 0)
522 bb.position(pos + written);
523 return written;
524 }
525
526 public int read(ByteBuffer buf) throws IOException {
527 if (buf == null)
528 throw new NullPointerException();
529 synchronized (readLock) {
530 synchronized (stateLock) {
531 ensureOpen();
532 if (!isConnected())
533 throw new NotYetConnectedException();
534 }
535 int n = 0;
1075 public FileDescriptor getFD() {
1076 return fd;
1077 }
1078
1079 public int getFDVal() {
1080 return fdVal;
1081 }
1082
1083
1084 // -- Native methods --
1085
1086 private static native void initIDs();
1087
1088 private static native void disconnect0(FileDescriptor fd, boolean isIPv6)
1089 throws IOException;
1090
1091 private native int receive0(FileDescriptor fd, long address, int len,
1092 boolean connected)
1093 throws IOException;
1094
1095 private native int send0(boolean preferIPv6, FileDescriptor fd, long address, int len,
1096 SocketAddress sa)
1097 throws IOException;
1098
1099 static {
1100 Util.load();
1101 initIDs();
1102 }
1103
1104 }
|
402
403 private int receiveIntoNativeBuffer(FileDescriptor fd, ByteBuffer bb,
404 int rem, int pos)
405 throws IOException
406 {
407 int n = receive0(fd, ((DirectBuffer)bb).address() + pos, rem,
408 isConnected());
409 if (n > 0)
410 bb.position(pos + n);
411 return n;
412 }
413
414 public int send(ByteBuffer src, SocketAddress target)
415 throws IOException
416 {
417 if (src == null)
418 throw new NullPointerException();
419
420 synchronized (writeLock) {
421 ensureOpen();
422 InetSocketAddress isa = Net.checkAddress(target);
423 InetAddress ia = isa.getAddress();
424 if (ia == null)
425 throw new IOException("Target address not resolved");
426 synchronized (stateLock) {
427 if (!isConnected()) {
428 if (target == null)
429 throw new NullPointerException();
430 SecurityManager sm = System.getSecurityManager();
431 if (sm != null) {
432 if (ia.isMulticastAddress()) {
433 sm.checkMulticast(ia);
434 } else {
435 sm.checkConnect(ia.getHostAddress(),
436 isa.getPort());
437 }
438 }
439 } else { // Connected case; Check address then write
440 if (!target.equals(remoteAddress)) {
441 throw new IllegalArgumentException(
442 "Connected address not equal to target address");
443 }
444 return write(src);
445 }
446 }
447
448 int n = 0;
449 try {
450 begin();
451 if (!isOpen())
452 return 0;
453 writerThread = NativeThread.current();
454 do {
455 n = send(fd, src, isa);
456 } while ((n == IOStatus.INTERRUPTED) && isOpen());
457
458 synchronized (stateLock) {
459 if (isOpen() && (localAddress == null)) {
460 localAddress = Net.localAddress(fd);
461 }
462 }
463 return IOStatus.normalize(n);
464 } finally {
465 writerThread = 0;
466 end((n > 0) || (n == IOStatus.UNAVAILABLE));
467 assert IOStatus.check(n);
468 }
469 }
470 }
471
472 private int send(FileDescriptor fd, ByteBuffer src, InetSocketAddress target)
473 throws IOException
474 {
475 if (src instanceof DirectBuffer)
476 return sendFromNativeBuffer(fd, src, target);
477
478 // Substitute a native buffer
479 int pos = src.position();
480 int lim = src.limit();
481 assert (pos <= lim);
482 int rem = (pos <= lim ? lim - pos : 0);
483
484 ByteBuffer bb = Util.getTemporaryDirectBuffer(rem);
485 try {
486 bb.put(src);
487 bb.flip();
488 // Do not update src until we see how many bytes were written
489 src.position(pos);
490
491 int n = sendFromNativeBuffer(fd, bb, target);
492 if (n > 0) {
493 // now update src
494 src.position(pos + n);
495 }
496 return n;
497 } finally {
498 Util.releaseTemporaryDirectBuffer(bb);
499 }
500 }
501
502 private int sendFromNativeBuffer(FileDescriptor fd, ByteBuffer bb,
503 InetSocketAddress target)
504 throws IOException
505 {
506 int pos = bb.position();
507 int lim = bb.limit();
508 assert (pos <= lim);
509 int rem = (pos <= lim ? lim - pos : 0);
510
511 boolean preferIPv6 = (family != StandardProtocolFamily.INET);
512 int written;
513 try {
514 written = send0(preferIPv6, fd, ((DirectBuffer)bb).address() + pos,
515 rem, target.getAddress(), target.getPort());
516 } catch (PortUnreachableException pue) {
517 if (isConnected())
518 throw pue;
519 written = rem;
520 }
521 if (written > 0)
522 bb.position(pos + written);
523 return written;
524 }
525
526 public int read(ByteBuffer buf) throws IOException {
527 if (buf == null)
528 throw new NullPointerException();
529 synchronized (readLock) {
530 synchronized (stateLock) {
531 ensureOpen();
532 if (!isConnected())
533 throw new NotYetConnectedException();
534 }
535 int n = 0;
1075 public FileDescriptor getFD() {
1076 return fd;
1077 }
1078
1079 public int getFDVal() {
1080 return fdVal;
1081 }
1082
1083
1084 // -- Native methods --
1085
1086 private static native void initIDs();
1087
1088 private static native void disconnect0(FileDescriptor fd, boolean isIPv6)
1089 throws IOException;
1090
1091 private native int receive0(FileDescriptor fd, long address, int len,
1092 boolean connected)
1093 throws IOException;
1094
1095 private native int send0(boolean preferIPv6, FileDescriptor fd, long address,
1096 int len, InetAddress addr, int port)
1097 throws IOException;
1098
1099 static {
1100 Util.load();
1101 initIDs();
1102 }
1103
1104 }
|