| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.os; |
| 18 |
|
| 19 |
import android.compat.annotation.UnsupportedAppUsage; |
| 20 |
import android.system.ErrnoException; |
| 21 |
|
| 22 |
import java.io.FileDescriptor; |
| 23 |
import java.io.IOException; |
| 24 |
import java.io.InputStream; |
| 25 |
import java.io.OutputStream; |
| 26 |
import java.nio.ByteBuffer; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public class MemoryFile { |
| 41 |
private static String TAG = "MemoryFile"; |
| 42 |
|
| 43 |
|
| 44 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 45 |
private static native boolean native_pin(FileDescriptor fd, boolean pin) throws IOException; |
| 46 |
@UnsupportedAppUsage |
| 47 |
private static native int native_get_size(FileDescriptor fd) throws IOException; |
| 48 |
|
| 49 |
private SharedMemory mSharedMemory; |
| 50 |
private ByteBuffer mMapping; |
| 51 |
private boolean mAllowPurging = false; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public MemoryFile(String name, int length) throws IOException { |
| 61 |
try { |
| 62 |
mSharedMemory = SharedMemory.create(name, length); |
| 63 |
mMapping = mSharedMemory.mapReadWrite(); |
| 64 |
} catch (ErrnoException ex) { |
| 65 |
ex.rethrowAsIOException(); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public void close() { |
| 74 |
deactivate(); |
| 75 |
mSharedMemory.close(); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
@UnsupportedAppUsage |
| 86 |
void deactivate() { |
| 87 |
if (mMapping != null) { |
| 88 |
SharedMemory.unmap(mMapping); |
| 89 |
mMapping = null; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
private void checkActive() throws IOException { |
| 94 |
if (mMapping == null) { |
| 95 |
throw new IOException("MemoryFile has been deactivated"); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
private void beginAccess() throws IOException { |
| 100 |
checkActive(); |
| 101 |
if (mAllowPurging) { |
| 102 |
if (native_pin(mSharedMemory.getFileDescriptor(), true)) { |
| 103 |
throw new IOException("MemoryFile has been purged"); |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
private void endAccess() throws IOException { |
| 109 |
if (mAllowPurging) { |
| 110 |
native_pin(mSharedMemory.getFileDescriptor(), false); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public int length() { |
| 120 |
return mSharedMemory.getSize(); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
@Deprecated |
| 133 |
public boolean isPurgingAllowed() { |
| 134 |
return mAllowPurging; |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
@Deprecated |
| 149 |
synchronized public boolean allowPurging(boolean allowPurging) throws IOException { |
| 150 |
boolean oldValue = mAllowPurging; |
| 151 |
if (oldValue != allowPurging) { |
| 152 |
native_pin(mSharedMemory.getFileDescriptor(), !allowPurging); |
| 153 |
mAllowPurging = allowPurging; |
| 154 |
} |
| 155 |
return oldValue; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
public InputStream getInputStream() { |
| 164 |
return new MemoryInputStream(); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
public OutputStream getOutputStream() { |
| 173 |
return new MemoryOutputStream(); |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
public int readBytes(byte[] buffer, int srcOffset, int destOffset, int count) |
| 188 |
throws IOException { |
| 189 |
beginAccess(); |
| 190 |
try { |
| 191 |
mMapping.position(srcOffset); |
| 192 |
mMapping.get(buffer, destOffset, count); |
| 193 |
} finally { |
| 194 |
endAccess(); |
| 195 |
} |
| 196 |
return count; |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
public void writeBytes(byte[] buffer, int srcOffset, int destOffset, int count) |
| 210 |
throws IOException { |
| 211 |
beginAccess(); |
| 212 |
try { |
| 213 |
mMapping.position(destOffset); |
| 214 |
mMapping.put(buffer, srcOffset, count); |
| 215 |
} finally { |
| 216 |
endAccess(); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
@UnsupportedAppUsage |
| 230 |
public FileDescriptor getFileDescriptor() throws IOException { |
| 231 |
return mSharedMemory.getFileDescriptor(); |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
@UnsupportedAppUsage |
| 243 |
public static int getSize(FileDescriptor fd) throws IOException { |
| 244 |
return native_get_size(fd); |
| 245 |
} |
| 246 |
|
| 247 |
private class MemoryInputStream extends InputStream { |
| 248 |
|
| 249 |
private int mMark = 0; |
| 250 |
private int mOffset = 0; |
| 251 |
private byte[] mSingleByte; |
| 252 |
|
| 253 |
@Override |
| 254 |
public int available() throws IOException { |
| 255 |
if (mOffset >= mSharedMemory.getSize()) { |
| 256 |
return 0; |
| 257 |
} |
| 258 |
return mSharedMemory.getSize() - mOffset; |
| 259 |
} |
| 260 |
|
| 261 |
@Override |
| 262 |
public boolean markSupported() { |
| 263 |
return true; |
| 264 |
} |
| 265 |
|
| 266 |
@Override |
| 267 |
public void mark(int readlimit) { |
| 268 |
mMark = mOffset; |
| 269 |
} |
| 270 |
|
| 271 |
@Override |
| 272 |
public void reset() throws IOException { |
| 273 |
mOffset = mMark; |
| 274 |
} |
| 275 |
|
| 276 |
@Override |
| 277 |
public int read() throws IOException { |
| 278 |
if (mSingleByte == null) { |
| 279 |
mSingleByte = new byte[1]; |
| 280 |
} |
| 281 |
int result = read(mSingleByte, 0, 1); |
| 282 |
if (result != 1) { |
| 283 |
return -1; |
| 284 |
} |
| 285 |
return mSingleByte[0]; |
| 286 |
} |
| 287 |
|
| 288 |
@Override |
| 289 |
public int read(byte buffer[], int offset, int count) throws IOException { |
| 290 |
if (offset < 0 || count < 0 || offset + count > buffer.length) { |
| 291 |
|
| 292 |
|
| 293 |
throw new IndexOutOfBoundsException(); |
| 294 |
} |
| 295 |
count = Math.min(count, available()); |
| 296 |
if (count < 1) { |
| 297 |
return -1; |
| 298 |
} |
| 299 |
int result = readBytes(buffer, mOffset, offset, count); |
| 300 |
if (result > 0) { |
| 301 |
mOffset += result; |
| 302 |
} |
| 303 |
return result; |
| 304 |
} |
| 305 |
|
| 306 |
@Override |
| 307 |
public long skip(long n) throws IOException { |
| 308 |
if (mOffset + n > mSharedMemory.getSize()) { |
| 309 |
n = mSharedMemory.getSize() - mOffset; |
| 310 |
} |
| 311 |
mOffset += n; |
| 312 |
return n; |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
private class MemoryOutputStream extends OutputStream { |
| 317 |
|
| 318 |
private int mOffset = 0; |
| 319 |
private byte[] mSingleByte; |
| 320 |
|
| 321 |
@Override |
| 322 |
public void write(byte buffer[], int offset, int count) throws IOException { |
| 323 |
writeBytes(buffer, offset, mOffset, count); |
| 324 |
mOffset += count; |
| 325 |
} |
| 326 |
|
| 327 |
@Override |
| 328 |
public void write(int oneByte) throws IOException { |
| 329 |
if (mSingleByte == null) { |
| 330 |
mSingleByte = new byte[1]; |
| 331 |
} |
| 332 |
mSingleByte[0] = (byte)oneByte; |
| 333 |
write(mSingleByte, 0, 1); |
| 334 |
} |
| 335 |
} |
| 336 |
} |