/** * Writes all the remaining bytes of the {@code src} byte buffer to this buffer's current * position, and increases both buffers' position by the number of bytes copied. * * @param src the source byte buffer. * @return {@code this} * @throws BufferOverflowException if {@code src.remaining()} is greater than this buffer's {@code * remaining()}. * @throws IllegalArgumentException if {@code src} is this buffer. * @throws ReadOnlyBufferException if no changes may be made to the contents of this buffer. */ public ByteBuffer put(ByteBuffer src) { if (isReadOnly()) { throw new ReadOnlyBufferException(); } if (src == this) { throw new IllegalArgumentException("src == this"); } int srcByteCount = src.remaining(); if (srcByteCount > remaining()) { throw new BufferOverflowException(); } Object srcObject = src.isDirect() ? src : NioUtils.unsafeArray(src); int srcOffset = src.position(); if (!src.isDirect()) { srcOffset += NioUtils.unsafeArrayOffset(src); } ByteBuffer dst = this; Object dstObject = dst.isDirect() ? dst : NioUtils.unsafeArray(dst); int dstOffset = dst.position(); if (!dst.isDirect()) { dstOffset += NioUtils.unsafeArrayOffset(dst); } Memory.memmove(dstObject, dstOffset, srcObject, srcOffset, srcByteCount); src.position(src.limit()); dst.position(dst.position() + srcByteCount); return this; }
@Override public ByteBuffer compact() { if (isReadOnly) { throw new ReadOnlyBufferException(); } Memory.memmove(this, 0, this, position, remaining()); position = limit - position; limit = capacity; mark = UNSET_MARK; return this; }
private void writeCommStatusAndClose(int status, String msg) { if (this.mCommFd != null) { if (status == MODE_WORLD_WRITEABLE) { Log.w(TAG, "Peer expected signal when closed; unable to deliver after detach"); } if (status == -1) { IoUtils.closeQuietly(this.mCommFd); this.mCommFd = null; return; } try { this.mStatus = readCommStatus(this.mCommFd, getOrCreateStatusBuffer()); if (this.mStatus != null) { IoUtils.closeQuietly(this.mCommFd); this.mCommFd = null; return; } byte[] buf = getOrCreateStatusBuffer(); Memory.pokeInt(buf, 0, status, ByteOrder.BIG_ENDIAN); int writePtr = 0 + 4; if (msg != null) { byte[] rawMsg = msg.getBytes(); int len = Math.min(rawMsg.length, buf.length - 4); System.arraycopy(rawMsg, 0, buf, writePtr, len); writePtr = len + 4; } Os.write(this.mCommFd, buf, 0, writePtr); IoUtils.closeQuietly(this.mCommFd); this.mCommFd = null; } catch (ErrnoException e) { Log.w(TAG, "Failed to report status: " + e); } catch (InterruptedIOException e2) { Log.w(TAG, "Failed to report status: " + e2); } catch (Throwable th) { IoUtils.closeQuietly(this.mCommFd); this.mCommFd = null; } } else if (msg != null) { Log.w(TAG, "Unable to inform peer: " + msg); } }
private static Status readCommStatus(FileDescriptor comm, byte[] buf) { try { int n = Os.read(comm, buf, 0, buf.length); if (n == 0) { return new Status(-2); } int status = Memory.peekInt(buf, 0, ByteOrder.BIG_ENDIAN); if (status == MODE_WORLD_READABLE) { return new Status(status, new String(buf, 4, n - 4)); } return new Status(status); } catch (ErrnoException e) { if (e.errno == OsConstants.EAGAIN) { return null; } Log.d(TAG, "Failed to read status; assuming dead: " + e); return new Status(-2); } catch (ErrnoException e2) { Log.d(TAG, "Failed to read status; assuming dead: " + e2); return new Status(-2); } }