1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.net.module.util.async; 18 19 import java.io.IOException; 20 21 /** 22 * Represents an EventManager-managed file with Async IO semantics. 23 * 24 * Implements level-based Asyn IO semantics. This means that: 25 * - onReadReady() callback would keep happening as long as there's any remaining 26 * data to read, or the user calls enableReadEvents(false) 27 * - onWriteReady() callback would keep happening as long as there's remaining space 28 * to write to, or the user calls enableWriteEvents(false) 29 * 30 * All operations except close() must be called on the EventManager thread. 31 * 32 * @hide 33 */ 34 public interface AsyncFile { 35 /** 36 * Receives notifications when file readability or writeability changes. 37 * @hide 38 */ 39 public interface Listener { 40 /** Invoked after the underlying file has been closed. */ onClosed(AsyncFile file)41 void onClosed(AsyncFile file); 42 43 /** Invoked while the file has readable data and read notifications are enabled. */ onReadReady(AsyncFile file)44 void onReadReady(AsyncFile file); 45 46 /** Invoked while the file has writeable space and write notifications are enabled. */ onWriteReady(AsyncFile file)47 void onWriteReady(AsyncFile file); 48 } 49 50 /** Requests this file to be closed. */ close()51 void close(); 52 53 /** Enables or disables onReadReady() events. */ enableReadEvents(boolean enable)54 void enableReadEvents(boolean enable); 55 56 /** Enables or disables onWriteReady() events. */ enableWriteEvents(boolean enable)57 void enableWriteEvents(boolean enable); 58 59 /** Returns true if the input stream has reached its end, or has been closed. */ reachedEndOfFile()60 boolean reachedEndOfFile(); 61 62 /** 63 * Reads available data from the given non-blocking file descriptor. 64 * 65 * Returns zero if there's no data to read at this moment. 66 * Returns -1 if the file has reached its end or the input stream has been closed. 67 * Otherwise returns the number of bytes read. 68 */ read(byte[] buffer, int pos, int len)69 int read(byte[] buffer, int pos, int len) throws IOException; 70 71 /** 72 * Writes data into the given non-blocking file descriptor. 73 * 74 * Returns zero if there's no buffer space to write to at this moment. 75 * Otherwise returns the number of bytes written. 76 */ write(byte[] buffer, int pos, int len)77 int write(byte[] buffer, int pos, int len) throws IOException; 78 } 79