| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.util; |
| 18 |
|
| 19 |
import android.annotation.Nullable; |
| 20 |
import android.os.Parcel; |
| 21 |
import android.os.ParcelFileDescriptor; |
| 22 |
import android.os.Parcelable; |
| 23 |
|
| 24 |
import dalvik.system.CloseGuard; |
| 25 |
|
| 26 |
import libcore.io.IoUtils; |
| 27 |
|
| 28 |
import java.io.Closeable; |
| 29 |
import java.io.IOException; |
| 30 |
import java.util.UUID; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public final class MemoryIntArray implements Parcelable, Closeable { |
| 53 |
private static final String TAG = "MemoryIntArray"; |
| 54 |
|
| 55 |
private static final int MAX_SIZE = 1024; |
| 56 |
|
| 57 |
private final CloseGuard mCloseGuard = CloseGuard.get(); |
| 58 |
|
| 59 |
private final boolean mIsOwner; |
| 60 |
private final long mMemoryAddr; |
| 61 |
private int mFd = -1; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public MemoryIntArray(int size) throws IOException { |
| 71 |
if (size > MAX_SIZE) { |
| 72 |
throw new IllegalArgumentException("Max size is " + MAX_SIZE); |
| 73 |
} |
| 74 |
mIsOwner = true; |
| 75 |
final String name = UUID.randomUUID().toString(); |
| 76 |
mFd = nativeCreate(name, size); |
| 77 |
mMemoryAddr = nativeOpen(mFd, mIsOwner); |
| 78 |
mCloseGuard.open("MemoryIntArray.close"); |
| 79 |
} |
| 80 |
|
| 81 |
private MemoryIntArray(Parcel parcel) throws IOException { |
| 82 |
mIsOwner = false; |
| 83 |
ParcelFileDescriptor pfd = parcel.readParcelable(null, android.os.ParcelFileDescriptor.class); |
| 84 |
if (pfd == null) { |
| 85 |
throw new IOException("No backing file descriptor"); |
| 86 |
} |
| 87 |
mFd = pfd.detachFd(); |
| 88 |
mMemoryAddr = nativeOpen(mFd, mIsOwner); |
| 89 |
mCloseGuard.open("MemoryIntArray.close"); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public boolean isWritable() { |
| 96 |
enforceNotClosed(); |
| 97 |
return mIsOwner; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public int get(int index) throws IOException { |
| 108 |
enforceNotClosed(); |
| 109 |
enforceValidIndex(index); |
| 110 |
return nativeGet(mFd, mMemoryAddr, index); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public void set(int index, int value) throws IOException { |
| 123 |
enforceNotClosed(); |
| 124 |
enforceWritable(); |
| 125 |
enforceValidIndex(index); |
| 126 |
nativeSet(mFd, mMemoryAddr, index, value); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public int size() throws IOException { |
| 135 |
enforceNotClosed(); |
| 136 |
return nativeSize(mFd); |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
@Override |
| 145 |
public void close() throws IOException { |
| 146 |
if (!isClosed()) { |
| 147 |
nativeClose(mFd, mMemoryAddr, mIsOwner); |
| 148 |
mFd = -1; |
| 149 |
mCloseGuard.close(); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
public boolean isClosed() { |
| 157 |
return mFd == -1; |
| 158 |
} |
| 159 |
|
| 160 |
@Override |
| 161 |
protected void finalize() throws Throwable { |
| 162 |
try { |
| 163 |
if (mCloseGuard != null) { |
| 164 |
mCloseGuard.warnIfOpen(); |
| 165 |
} |
| 166 |
|
| 167 |
IoUtils.closeQuietly(this); |
| 168 |
} finally { |
| 169 |
super.finalize(); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
@Override |
| 174 |
public int describeContents() { |
| 175 |
return CONTENTS_FILE_DESCRIPTOR; |
| 176 |
} |
| 177 |
|
| 178 |
@Override |
| 179 |
public void writeToParcel(Parcel parcel, int flags) { |
| 180 |
try (ParcelFileDescriptor pfd = ParcelFileDescriptor.fromFd(mFd)) { |
| 181 |
parcel.writeParcelable(pfd, flags); |
| 182 |
} catch (IOException ex) { |
| 183 |
throw new RuntimeException(ex); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
@Override |
| 188 |
public boolean equals(@Nullable Object obj) { |
| 189 |
if (obj == null) { |
| 190 |
return false; |
| 191 |
} |
| 192 |
if (this == obj) { |
| 193 |
return true; |
| 194 |
} |
| 195 |
if (getClass() != obj.getClass()) { |
| 196 |
return false; |
| 197 |
} |
| 198 |
MemoryIntArray other = (MemoryIntArray) obj; |
| 199 |
return mFd == other.mFd; |
| 200 |
} |
| 201 |
|
| 202 |
@Override |
| 203 |
public int hashCode() { |
| 204 |
return mFd; |
| 205 |
} |
| 206 |
|
| 207 |
private void enforceNotClosed() { |
| 208 |
if (isClosed()) { |
| 209 |
throw new IllegalStateException("cannot interact with a closed instance"); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
private void enforceValidIndex(int index) throws IOException { |
| 214 |
final int size = size(); |
| 215 |
if (index < 0 || index > size - 1) { |
| 216 |
throw new IndexOutOfBoundsException( |
| 217 |
index + " not between 0 and " + (size - 1)); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
private void enforceWritable() { |
| 222 |
if (!isWritable()) { |
| 223 |
throw new UnsupportedOperationException("array is not writable"); |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
private native int nativeCreate(String name, int size); |
| 228 |
private native long nativeOpen(int fd, boolean owner); |
| 229 |
private native void nativeClose(int fd, long memoryAddr, boolean owner); |
| 230 |
private native int nativeGet(int fd, long memoryAddr, int index); |
| 231 |
private native void nativeSet(int fd, long memoryAddr, int index, int value); |
| 232 |
private native int nativeSize(int fd); |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
public static int getMaxSize() { |
| 238 |
return MAX_SIZE; |
| 239 |
} |
| 240 |
|
| 241 |
public static final @android.annotation.NonNull Parcelable.Creator<MemoryIntArray> CREATOR = |
| 242 |
new Parcelable.Creator<MemoryIntArray>() { |
| 243 |
@Override |
| 244 |
public MemoryIntArray createFromParcel(Parcel parcel) { |
| 245 |
try { |
| 246 |
return new MemoryIntArray(parcel); |
| 247 |
} catch (IOException ioe) { |
| 248 |
throw new IllegalArgumentException("Error unparceling MemoryIntArray"); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
@Override |
| 253 |
public MemoryIntArray[] newArray(int size) { |
| 254 |
return new MemoryIntArray[size]; |
| 255 |
} |
| 256 |
}; |
| 257 |
} |