|
16 | 16 |
|
17 | 17 | package android.os; |
18 | 18 |
|
| 19 | +import libcore.io.ErrnoException; |
| 20 | +import libcore.io.Libcore; |
| 21 | +import libcore.io.StructStatFs; |
| 22 | + |
19 | 23 | /** |
20 | | - * Retrieve overall information about the space on a filesystem. This is a |
21 | | - * Wrapper for Unix statfs(). |
| 24 | + * Retrieve overall information about the space on a filesystem. This is a |
| 25 | + * wrapper for Unix statfs(). |
22 | 26 | */ |
23 | 27 | public class StatFs { |
| 28 | + private StructStatFs mStat; |
| 29 | + |
24 | 30 | /** |
25 | | - * Construct a new StatFs for looking at the stats of the |
26 | | - * filesystem at <var>path</var>. Upon construction, the stat of |
27 | | - * the file system will be performed, and the values retrieved available |
28 | | - * from the methods on this class. |
29 | | - * |
30 | | - * @param path A path in the desired file system to state. |
| 31 | + * Construct a new StatFs for looking at the stats of the filesystem at |
| 32 | + * {@code path}. Upon construction, the stat of the file system will be |
| 33 | + * performed, and the values retrieved available from the methods on this |
| 34 | + * class. |
| 35 | + * |
| 36 | + * @param path path in the desired file system to stat. |
31 | 37 | */ |
32 | | - public StatFs(String path) { native_setup(path); } |
33 | | - |
| 38 | + public StatFs(String path) { |
| 39 | + mStat = doStat(path); |
| 40 | + } |
| 41 | + |
| 42 | + private static StructStatFs doStat(String path) { |
| 43 | + try { |
| 44 | + return Libcore.os.statfs(path); |
| 45 | + } catch (ErrnoException e) { |
| 46 | + throw new IllegalArgumentException("Invalid path: " + path, e); |
| 47 | + } |
| 48 | + } |
| 49 | + |
34 | 50 | /** |
35 | | - * Perform a restat of the file system referenced by this object. This |
36 | | - * is the same as re-constructing the object with the same file system |
37 | | - * path, and the new stat values are available upon return. |
| 51 | + * Perform a restat of the file system referenced by this object. This is |
| 52 | + * the same as re-constructing the object with the same file system path, |
| 53 | + * and the new stat values are available upon return. |
38 | 54 | */ |
39 | | - public void restat(String path) { native_restat(path); } |
40 | | - |
41 | | - @Override |
42 | | - protected void finalize() { native_finalize(); } |
| 55 | + public void restat(String path) { |
| 56 | + mStat = doStat(path); |
| 57 | + } |
43 | 58 |
|
44 | 59 | /** |
45 | | - * The size, in bytes, of a block on the file system. This corresponds |
46 | | - * to the Unix statfs.f_bsize field. |
| 60 | + * The size, in bytes, of a block on the file system. This corresponds to |
| 61 | + * the Unix {@code statfs.f_bsize} field. |
47 | 62 | */ |
48 | | - public native int getBlockSize(); |
| 63 | + public int getBlockSize() { |
| 64 | + return (int) mStat.f_bsize; |
| 65 | + } |
49 | 66 |
|
50 | 67 | /** |
51 | | - * The total number of blocks on the file system. This corresponds |
52 | | - * to the Unix statfs.f_blocks field. |
| 68 | + * The total number of blocks on the file system. This corresponds to the |
| 69 | + * Unix {@code statfs.f_blocks} field. |
53 | 70 | */ |
54 | | - public native int getBlockCount(); |
| 71 | + public int getBlockCount() { |
| 72 | + return (int) mStat.f_blocks; |
| 73 | + } |
55 | 74 |
|
56 | 75 | /** |
57 | 76 | * The total number of blocks that are free on the file system, including |
58 | | - * reserved blocks (that are not available to normal applications). This |
59 | | - * corresponds to the Unix statfs.f_bfree field. Most applications will |
60 | | - * want to use {@link #getAvailableBlocks()} instead. |
| 77 | + * reserved blocks (that are not available to normal applications). This |
| 78 | + * corresponds to the Unix {@code statfs.f_bfree} field. Most applications |
| 79 | + * will want to use {@link #getAvailableBlocks()} instead. |
61 | 80 | */ |
62 | | - public native int getFreeBlocks(); |
| 81 | + public int getFreeBlocks() { |
| 82 | + return (int) mStat.f_bfree; |
| 83 | + } |
63 | 84 |
|
64 | 85 | /** |
65 | 86 | * The number of blocks that are free on the file system and available to |
66 | | - * applications. This corresponds to the Unix statfs.f_bavail field. |
| 87 | + * applications. This corresponds to the Unix {@code statfs.f_bavail} field. |
67 | 88 | */ |
68 | | - public native int getAvailableBlocks(); |
69 | | - |
70 | | - private int mNativeContext; |
71 | | - private native void native_restat(String path); |
72 | | - private native void native_setup(String path); |
73 | | - private native void native_finalize(); |
| 89 | + public int getAvailableBlocks() { |
| 90 | + return (int) mStat.f_bavail; |
| 91 | + } |
74 | 92 | } |
0 commit comments