diff options
Diffstat (limited to 'common/device/com/android/net/module/util/async')
8 files changed, 0 insertions, 896 deletions
diff --git a/common/device/com/android/net/module/util/async/Assertions.java b/common/device/com/android/net/module/util/async/Assertions.java deleted file mode 100644 index ce701d05..00000000 --- a/common/device/com/android/net/module/util/async/Assertions.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (C) 2022 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.net.module.util.async; - -import android.os.Build; - -/** - * Implements basic assert functions for runtime error-checking. - * - * @hide - */ -public final class Assertions { - public static final boolean IS_USER_BUILD = "user".equals(Build.TYPE); - - public static void throwsIfOutOfBounds(int totalLength, int pos, int len) { - if (!IS_USER_BUILD && ((totalLength | pos | len) < 0 || pos > totalLength - len)) { - throw new ArrayIndexOutOfBoundsException( - "length=" + totalLength + "; regionStart=" + pos + "; regionLength=" + len); - } - } - - public static void throwsIfOutOfBounds(byte[] buffer, int pos, int len) { - throwsIfOutOfBounds(buffer != null ? buffer.length : 0, pos, len); - } - - private Assertions() {} -} diff --git a/common/device/com/android/net/module/util/async/AsyncFile.java b/common/device/com/android/net/module/util/async/AsyncFile.java deleted file mode 100644 index 2a3231b7..00000000 --- a/common/device/com/android/net/module/util/async/AsyncFile.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Copyright (C) 2022 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.net.module.util.async; - -import java.io.IOException; - -/** - * Represents an EventManager-managed file with Async IO semantics. - * - * Implements level-based Asyn IO semantics. This means that: - * - onReadReady() callback would keep happening as long as there's any remaining - * data to read, or the user calls enableReadEvents(false) - * - onWriteReady() callback would keep happening as long as there's remaining space - * to write to, or the user calls enableWriteEvents(false) - * - * All operations except close() must be called on the EventManager thread. - * - * @hide - */ -public interface AsyncFile { - /** - * Receives notifications when file readability or writeability changes. - * @hide - */ - public interface Listener { - /** Invoked after the underlying file has been closed. */ - void onClosed(AsyncFile file); - - /** Invoked while the file has readable data and read notifications are enabled. */ - void onReadReady(AsyncFile file); - - /** Invoked while the file has writeable space and write notifications are enabled. */ - void onWriteReady(AsyncFile file); - } - - /** Requests this file to be closed. */ - void close(); - - /** Enables or disables onReadReady() events. */ - void enableReadEvents(boolean enable); - - /** Enables or disables onWriteReady() events. */ - void enableWriteEvents(boolean enable); - - /** Returns true if the input stream has reached its end, or has been closed. */ - boolean reachedEndOfFile(); - - /** - * Reads available data from the given non-blocking file descriptor. - * - * Returns zero if there's no data to read at this moment. - * Returns -1 if the file has reached its end or the input stream has been closed. - * Otherwise returns the number of bytes read. - */ - int read(byte[] buffer, int pos, int len) throws IOException; - - /** - * Writes data into the given non-blocking file descriptor. - * - * Returns zero if there's no buffer space to write to at this moment. - * Otherwise returns the number of bytes written. - */ - int write(byte[] buffer, int pos, int len) throws IOException; -} diff --git a/common/device/com/android/net/module/util/async/BufferedFile.java b/common/device/com/android/net/module/util/async/BufferedFile.java deleted file mode 100644 index bb5736b0..00000000 --- a/common/device/com/android/net/module/util/async/BufferedFile.java +++ /dev/null @@ -1,292 +0,0 @@ -/* - * Copyright (C) 2023 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.net.module.util.async; - -import java.io.IOException; -import java.util.concurrent.atomic.AtomicLong; - -/** - * Buffers inbound and outbound file data within given strict limits. - * - * Automatically manages all readability and writeability events in EventManager: - * - When read buffer has more space - asks EventManager to notify on more data - * - When write buffer has more space - asks the user to provide more data - * - When underlying file cannot accept more data - registers EventManager callback - * - * @hide - */ -public final class BufferedFile implements AsyncFile.Listener { - /** - * Receives notifications when new data or output space is available. - * @hide - */ - public interface Listener { - /** Invoked after the underlying file has been closed. */ - void onBufferedFileClosed(); - - /** Invoked when there's new data in the inbound buffer. */ - void onBufferedFileInboundData(int readByteCount); - - /** Notifies on data being flushed from output buffer. */ - void onBufferedFileOutboundSpace(); - - /** Notifies on unrecoverable error in file access. */ - void onBufferedFileIoError(String message); - } - - private final Listener mListener; - private final EventManager mEventManager; - private AsyncFile mFile; - - private final CircularByteBuffer mInboundBuffer; - private final AtomicLong mTotalBytesRead = new AtomicLong(); - private boolean mIsReadingShutdown; - - private final CircularByteBuffer mOutboundBuffer; - private final AtomicLong mTotalBytesWritten = new AtomicLong(); - - /** Creates BufferedFile based on the given file descriptor. */ - public static BufferedFile create( - EventManager eventManager, - FileHandle fileHandle, - Listener listener, - int inboundBufferSize, - int outboundBufferSize) throws IOException { - if (fileHandle == null) { - throw new NullPointerException(); - } - BufferedFile file = new BufferedFile( - eventManager, listener, inboundBufferSize, outboundBufferSize); - file.mFile = eventManager.registerFile(fileHandle, file); - return file; - } - - private BufferedFile( - EventManager eventManager, - Listener listener, - int inboundBufferSize, - int outboundBufferSize) { - if (eventManager == null || listener == null) { - throw new NullPointerException(); - } - mEventManager = eventManager; - mListener = listener; - - mInboundBuffer = new CircularByteBuffer(inboundBufferSize); - mOutboundBuffer = new CircularByteBuffer(outboundBufferSize); - } - - /** Requests this file to be closed. */ - public void close() { - mFile.close(); - } - - @Override - public void onClosed(AsyncFile file) { - mListener.onBufferedFileClosed(); - } - - /////////////////////////////////////////////////////////////////////////// - // READ PATH - /////////////////////////////////////////////////////////////////////////// - - /** Returns buffer that is automatically filled with inbound data. */ - public ReadableByteBuffer getInboundBuffer() { - return mInboundBuffer; - } - - public int getInboundBufferFreeSizeForTest() { - return mInboundBuffer.freeSize(); - } - - /** Permanently disables reading of this file, and clears all buffered data. */ - public void shutdownReading() { - mIsReadingShutdown = true; - mInboundBuffer.clear(); - mFile.enableReadEvents(false); - } - - /** Returns true after shutdownReading() has been called. */ - public boolean isReadingShutdown() { - return mIsReadingShutdown; - } - - /** Starts or resumes async read operations on this file. */ - public void continueReading() { - if (!mIsReadingShutdown && mInboundBuffer.freeSize() > 0) { - mFile.enableReadEvents(true); - } - } - - @Override - public void onReadReady(AsyncFile file) { - if (mIsReadingShutdown) { - return; - } - - int readByteCount; - try { - readByteCount = bufferInputData(); - } catch (IOException e) { - mListener.onBufferedFileIoError("IOException while reading: " + e.toString()); - return; - } - - if (readByteCount > 0) { - mListener.onBufferedFileInboundData(readByteCount); - } - - continueReading(); - } - - private int bufferInputData() throws IOException { - int totalReadCount = 0; - while (true) { - final int maxReadCount = mInboundBuffer.getDirectWriteSize(); - if (maxReadCount == 0) { - mFile.enableReadEvents(false); - break; - } - - final int bufferOffset = mInboundBuffer.getDirectWritePos(); - final byte[] buffer = mInboundBuffer.getDirectWriteBuffer(); - - final int readCount = mFile.read(buffer, bufferOffset, maxReadCount); - if (readCount <= 0) { - break; - } - - mInboundBuffer.accountForDirectWrite(readCount); - totalReadCount += readCount; - } - - mTotalBytesRead.addAndGet(totalReadCount); - return totalReadCount; - } - - /////////////////////////////////////////////////////////////////////////// - // WRITE PATH - /////////////////////////////////////////////////////////////////////////// - - /** Returns the number of bytes currently buffered for output. */ - public int getOutboundBufferSize() { - return mOutboundBuffer.size(); - } - - /** Returns the number of bytes currently available for buffering for output. */ - public int getOutboundBufferFreeSize() { - return mOutboundBuffer.freeSize(); - } - - /** - * Queues the given data for output. - * Throws runtime exception if there is not enough space. - */ - public boolean enqueueOutboundData(byte[] data, int pos, int len) { - return enqueueOutboundData(data, pos, len, null, 0, 0); - } - - /** - * Queues data1, then data2 for output. - * Throws runtime exception if there is not enough space. - */ - public boolean enqueueOutboundData( - byte[] data1, int pos1, int len1, - byte[] buffer2, int pos2, int len2) { - Assertions.throwsIfOutOfBounds(data1, pos1, len1); - Assertions.throwsIfOutOfBounds(buffer2, pos2, len2); - - final int totalLen = len1 + len2; - - if (totalLen > mOutboundBuffer.freeSize()) { - flushOutboundBuffer(); - - if (totalLen > mOutboundBuffer.freeSize()) { - return false; - } - } - - mOutboundBuffer.writeBytes(data1, pos1, len1); - - if (buffer2 != null) { - mOutboundBuffer.writeBytes(buffer2, pos2, len2); - } - - flushOutboundBuffer(); - - return true; - } - - private void flushOutboundBuffer() { - try { - while (mOutboundBuffer.getDirectReadSize() > 0) { - final int maxReadSize = mOutboundBuffer.getDirectReadSize(); - final int writeCount = mFile.write( - mOutboundBuffer.getDirectReadBuffer(), - mOutboundBuffer.getDirectReadPos(), - maxReadSize); - - if (writeCount == 0) { - mFile.enableWriteEvents(true); - break; - } - - if (writeCount > maxReadSize) { - throw new IllegalArgumentException( - "Write count " + writeCount + " above max " + maxReadSize); - } - - mOutboundBuffer.accountForDirectRead(writeCount); - } - } catch (IOException e) { - scheduleOnIoError("IOException while writing: " + e.toString()); - } - } - - private void scheduleOnIoError(String message) { - mEventManager.execute(() -> { - mListener.onBufferedFileIoError(message); - }); - } - - @Override - public void onWriteReady(AsyncFile file) { - mFile.enableWriteEvents(false); - flushOutboundBuffer(); - mListener.onBufferedFileOutboundSpace(); - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append("file={"); - sb.append(mFile); - sb.append("}"); - if (mIsReadingShutdown) { - sb.append(", readingShutdown"); - } - sb.append("}, inboundBuffer={"); - sb.append(mInboundBuffer); - sb.append("}, outboundBuffer={"); - sb.append(mOutboundBuffer); - sb.append("}, totalBytesRead="); - sb.append(mTotalBytesRead); - sb.append(", totalBytesWritten="); - sb.append(mTotalBytesWritten); - return sb.toString(); - } -} diff --git a/common/device/com/android/net/module/util/async/CircularByteBuffer.java b/common/device/com/android/net/module/util/async/CircularByteBuffer.java deleted file mode 100644 index 92daa08f..00000000 --- a/common/device/com/android/net/module/util/async/CircularByteBuffer.java +++ /dev/null @@ -1,210 +0,0 @@ -/* - * Copyright (C) 2022 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.net.module.util.async; - -import java.util.Arrays; - -/** - * Implements a circular read-write byte buffer. - * - * @hide - */ -public final class CircularByteBuffer implements ReadableByteBuffer { - private final byte[] mBuffer; - private final int mCapacity; - private int mReadPos; - private int mWritePos; - private int mSize; - - public CircularByteBuffer(int capacity) { - mCapacity = capacity; - mBuffer = new byte[mCapacity]; - } - - @Override - public void clear() { - mReadPos = 0; - mWritePos = 0; - mSize = 0; - Arrays.fill(mBuffer, (byte) 0); - } - - @Override - public int capacity() { - return mCapacity; - } - - @Override - public int size() { - return mSize; - } - - /** Returns the amount of remaining writeable space in this buffer. */ - public int freeSize() { - return mCapacity - mSize; - } - - @Override - public byte peek(int offset) { - if (offset < 0 || offset >= size()) { - throw new IllegalArgumentException("Invalid offset=" + offset + ", size=" + size()); - } - - return mBuffer[(mReadPos + offset) % mCapacity]; - } - - @Override - public void readBytes(byte[] dst, int dstPos, int dstLen) { - if (dst != null) { - Assertions.throwsIfOutOfBounds(dst, dstPos, dstLen); - } - if (dstLen > size()) { - throw new IllegalArgumentException("Invalid len=" + dstLen + ", size=" + size()); - } - - while (dstLen > 0) { - final int copyLen = getCopyLen(mReadPos, mWritePos, dstLen); - if (dst != null) { - System.arraycopy(mBuffer, mReadPos, dst, dstPos, copyLen); - } - dstPos += copyLen; - dstLen -= copyLen; - mSize -= copyLen; - mReadPos = (mReadPos + copyLen) % mCapacity; - } - - if (mSize == 0) { - // Reset to the beginning for better contiguous access. - mReadPos = 0; - mWritePos = 0; - } - } - - @Override - public void peekBytes(int offset, byte[] dst, int dstPos, int dstLen) { - Assertions.throwsIfOutOfBounds(dst, dstPos, dstLen); - if (offset + dstLen > size()) { - throw new IllegalArgumentException("Invalid len=" + dstLen - + ", offset=" + offset + ", size=" + size()); - } - - int tmpReadPos = (mReadPos + offset) % mCapacity; - while (dstLen > 0) { - final int copyLen = getCopyLen(tmpReadPos, mWritePos, dstLen); - System.arraycopy(mBuffer, tmpReadPos, dst, dstPos, copyLen); - dstPos += copyLen; - dstLen -= copyLen; - tmpReadPos = (tmpReadPos + copyLen) % mCapacity; - } - } - - @Override - public int getDirectReadSize() { - if (size() == 0) { - return 0; - } - return (mReadPos < mWritePos ? (mWritePos - mReadPos) : (mCapacity - mReadPos)); - } - - @Override - public int getDirectReadPos() { - return mReadPos; - } - - @Override - public byte[] getDirectReadBuffer() { - return mBuffer; - } - - @Override - public void accountForDirectRead(int len) { - if (len < 0 || len > size()) { - throw new IllegalArgumentException("Invalid len=" + len + ", size=" + size()); - } - - mSize -= len; - mReadPos = (mReadPos + len) % mCapacity; - } - - /** Copies given data to the end of the buffer. */ - public void writeBytes(byte[] buffer, int pos, int len) { - Assertions.throwsIfOutOfBounds(buffer, pos, len); - if (len > freeSize()) { - throw new IllegalArgumentException("Invalid len=" + len + ", size=" + freeSize()); - } - - while (len > 0) { - final int copyLen = getCopyLen(mWritePos, mReadPos,len); - System.arraycopy(buffer, pos, mBuffer, mWritePos, copyLen); - pos += copyLen; - len -= copyLen; - mSize += copyLen; - mWritePos = (mWritePos + copyLen) % mCapacity; - } - } - - private int getCopyLen(int startPos, int endPos, int len) { - if (startPos < endPos) { - return Math.min(len, endPos - startPos); - } else { - return Math.min(len, mCapacity - startPos); - } - } - - /** Returns the amount of contiguous writeable space. */ - public int getDirectWriteSize() { - if (freeSize() == 0) { - return 0; // Return zero in case buffer is full. - } - return (mWritePos < mReadPos ? (mReadPos - mWritePos) : (mCapacity - mWritePos)); - } - - /** Returns the position of contiguous writeable space. */ - public int getDirectWritePos() { - return mWritePos; - } - - /** Returns the buffer reference for direct write operation. */ - public byte[] getDirectWriteBuffer() { - return mBuffer; - } - - /** Must be called after performing a direct write using getDirectWriteBuffer(). */ - public void accountForDirectWrite(int len) { - if (len < 0 || len > freeSize()) { - throw new IllegalArgumentException("Invalid len=" + len + ", size=" + freeSize()); - } - - mSize += len; - mWritePos = (mWritePos + len) % mCapacity; - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append("CircularByteBuffer{c="); - sb.append(mCapacity); - sb.append(",s="); - sb.append(mSize); - sb.append(",r="); - sb.append(mReadPos); - sb.append(",w="); - sb.append(mWritePos); - sb.append('}'); - return sb.toString(); - } -} diff --git a/common/device/com/android/net/module/util/async/EventManager.java b/common/device/com/android/net/module/util/async/EventManager.java deleted file mode 100644 index 4ed4a704..00000000 --- a/common/device/com/android/net/module/util/async/EventManager.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * Copyright (C) 2022 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.net.module.util.async; - -import java.io.IOException; -import java.util.concurrent.Executor; - -/** - * Manages Async IO files and scheduled alarms, and executes all related callbacks - * in its own thread. - * - * All callbacks of AsyncFile, Alarm and EventManager will execute on EventManager's thread. - * - * Methods of this interface can be called from any thread. - * - * @hide - */ -public interface EventManager extends Executor { - /** - * Represents a scheduled alarm, allowing caller to attempt to cancel that alarm - * before it executes. - * - * @hide - */ - public interface Alarm { - /** @hide */ - public interface Listener { - void onAlarm(Alarm alarm, long elapsedTimeMs); - void onAlarmCancelled(Alarm alarm); - } - - /** - * Attempts to cancel this alarm. Note that this request is inherently - * racy if executed close to the alarm's expiration time. - */ - void cancel(); - } - - /** - * Requests EventManager to manage the given file. - * - * The file descriptors are not cloned, and EventManager takes ownership of all files passed. - * - * No event callbacks are enabled by this method. - */ - AsyncFile registerFile(FileHandle fileHandle, AsyncFile.Listener listener) throws IOException; - - /** - * Schedules Alarm with the given timeout. - * - * Timeout of zero can be used for immediate execution. - */ - Alarm scheduleAlarm(long timeout, Alarm.Listener callback); - - /** Schedules Runnable for immediate execution. */ - @Override - void execute(Runnable callback); - - /** Throws a runtime exception if the caller is not executing on this EventManager's thread. */ - void assertInThread(); -} diff --git a/common/device/com/android/net/module/util/async/FileHandle.java b/common/device/com/android/net/module/util/async/FileHandle.java deleted file mode 100644 index 9f7942d4..00000000 --- a/common/device/com/android/net/module/util/async/FileHandle.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright (C) 2022 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.net.module.util.async; - -import android.os.ParcelFileDescriptor; - -import java.io.Closeable; -import java.io.InputStream; -import java.io.OutputStream; - -/** - * Represents an file descriptor or another way to access a file. - * - * @hide - */ -public final class FileHandle { - private final ParcelFileDescriptor mFd; - private final Closeable mCloseable; - private final InputStream mInputStream; - private final OutputStream mOutputStream; - - public static FileHandle fromFileDescriptor(ParcelFileDescriptor fd) { - if (fd == null) { - throw new NullPointerException(); - } - return new FileHandle(fd, null, null, null); - } - - public static FileHandle fromBlockingStream( - Closeable closeable, InputStream is, OutputStream os) { - if (closeable == null || is == null || os == null) { - throw new NullPointerException(); - } - return new FileHandle(null, closeable, is, os); - } - - private FileHandle(ParcelFileDescriptor fd, Closeable closeable, - InputStream is, OutputStream os) { - mFd = fd; - mCloseable = closeable; - mInputStream = is; - mOutputStream = os; - } - - ParcelFileDescriptor getFileDescriptor() { - return mFd; - } - - Closeable getCloseable() { - return mCloseable; - } - - InputStream getInputStream() { - return mInputStream; - } - - OutputStream getOutputStream() { - return mOutputStream; - } -} diff --git a/common/device/com/android/net/module/util/async/OsAccess.java b/common/device/com/android/net/module/util/async/OsAccess.java deleted file mode 100644 index df0ded21..00000000 --- a/common/device/com/android/net/module/util/async/OsAccess.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright (C) 2023 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.net.module.util.async; - -import android.os.ParcelFileDescriptor; -import android.system.StructPollfd; - -import java.io.FileDescriptor; -import java.io.IOException; - -/** - * Provides access to all relevant OS functions.. - * - * @hide - */ -public abstract class OsAccess { - /** Closes the given file, suppressing IO exceptions. */ - public abstract void close(ParcelFileDescriptor fd); - - /** Returns file name for debugging purposes. */ - public abstract String getFileDebugName(ParcelFileDescriptor fd); - - /** Returns inner FileDescriptor instance. */ - public abstract FileDescriptor getInnerFileDescriptor(ParcelFileDescriptor fd); - - /** - * Reads available data from the given non-blocking file descriptor. - * - * Returns zero if there's no data to read at this moment. - * Returns -1 if the file has reached its end or the input stream has been closed. - * Otherwise returns the number of bytes read. - */ - public abstract int read(FileDescriptor fd, byte[] buffer, int pos, int len) - throws IOException; - - /** - * Writes data into the given non-blocking file descriptor. - * - * Returns zero if there's no buffer space to write to at this moment. - * Otherwise returns the number of bytes written. - */ - public abstract int write(FileDescriptor fd, byte[] buffer, int pos, int len) - throws IOException; - - public abstract long monotonicTimeMillis(); - public abstract void setNonBlocking(FileDescriptor fd) throws IOException; - public abstract ParcelFileDescriptor[] pipe() throws IOException; - - public abstract int poll(StructPollfd[] fds, int timeoutMs) throws IOException; - public abstract short getPollInMask(); - public abstract short getPollOutMask(); -} diff --git a/common/device/com/android/net/module/util/async/ReadableByteBuffer.java b/common/device/com/android/net/module/util/async/ReadableByteBuffer.java deleted file mode 100644 index 7f824049..00000000 --- a/common/device/com/android/net/module/util/async/ReadableByteBuffer.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright (C) 2022 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.net.module.util.async; - -/** - * Allows reading from a buffer of bytes. The data can be read and thus removed, - * or peeked at without disturbing the buffer state. - * - * @hide - */ -public interface ReadableByteBuffer { - /** Returns the size of the buffered data. */ - int size(); - - /** - * Returns the maximum amount of the buffered data. - * - * The caller may use this method in combination with peekBytes() - * to estimate when the buffer needs to be emptied using readData(). - */ - int capacity(); - - /** Clears all buffered data. */ - void clear(); - - /** Returns a single byte at the given offset. */ - byte peek(int offset); - - /** Copies an array of bytes from the given offset to "dst". */ - void peekBytes(int offset, byte[] dst, int dstPos, int dstLen); - - /** Reads and removes an array of bytes from the head of the buffer. */ - void readBytes(byte[] dst, int dstPos, int dstLen); - - /** Returns the amount of contiguous readable data. */ - int getDirectReadSize(); - - /** Returns the position of contiguous readable data. */ - int getDirectReadPos(); - - /** Returns the buffer reference for direct read operation. */ - byte[] getDirectReadBuffer(); - - /** Must be called after performing a direct read using getDirectReadBuffer(). */ - void accountForDirectRead(int len); -} |