• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 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.mail.photomanager;
18 
19 import android.os.StrictMode;
20 
21 import java.io.FileInputStream;
22 
23 public class MemInfoReader {
24     byte[] mBuffer = new byte[1024];
25 
26     private long mTotalSize;
27     private long mFreeSize;
28     private long mCachedSize;
29 
matchText(byte[] buffer, int index, String text)30     private static boolean matchText(byte[] buffer, int index, String text) {
31         int N = text.length();
32         if ((index + N) >= buffer.length) {
33             return false;
34         }
35         for (int i = 0; i < N; i++) {
36             if (buffer[index + i] != text.charAt(i)) {
37                 return false;
38             }
39         }
40         return true;
41     }
42 
extractMemValue(byte[] buffer, int index)43     private static long extractMemValue(byte[] buffer, int index) {
44         while (index < buffer.length && buffer[index] != '\n') {
45             if (buffer[index] >= '0' && buffer[index] <= '9') {
46                 int start = index;
47                 index++;
48                 while (index < buffer.length && buffer[index] >= '0' && buffer[index] <= '9') {
49                     index++;
50                 }
51                 String str = new String(buffer, 0, start, index - start);
52                 return ((long) Integer.parseInt(str)) * 1024;
53             }
54             index++;
55         }
56         return 0;
57     }
58 
readMemInfo()59     public void readMemInfo() {
60         // Permit disk reads here, as /proc/meminfo isn't really "on
61         // disk" and should be fast. TODO: make BlockGuard ignore
62         // /proc/ and /sys/ files perhaps?
63         StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskReads();
64         try {
65             mTotalSize = 0;
66             mFreeSize = 0;
67             mCachedSize = 0;
68             FileInputStream is = new FileInputStream("/proc/meminfo");
69             int len = is.read(mBuffer);
70             is.close();
71             final int BUFLEN = mBuffer.length;
72             int count = 0;
73             for (int i = 0; i < len && count < 3; i++) {
74                 if (matchText(mBuffer, i, "MemTotal")) {
75                     i += 8;
76                     mTotalSize = extractMemValue(mBuffer, i);
77                     count++;
78                 } else if (matchText(mBuffer, i, "MemFree")) {
79                     i += 7;
80                     mFreeSize = extractMemValue(mBuffer, i);
81                     count++;
82                 } else if (matchText(mBuffer, i, "Cached")) {
83                     i += 6;
84                     mCachedSize = extractMemValue(mBuffer, i);
85                     count++;
86                 }
87                 while (i < BUFLEN && mBuffer[i] != '\n') {
88                     i++;
89                 }
90             }
91         } catch (java.io.FileNotFoundException e) {
92         } catch (java.io.IOException e) {
93         } finally {
94             StrictMode.setThreadPolicy(savedPolicy);
95         }
96     }
97 
getTotalSize()98     public long getTotalSize() {
99         return mTotalSize;
100     }
101 
getFreeSize()102     public long getFreeSize() {
103         return mFreeSize;
104     }
105 
getCachedSize()106     public long getCachedSize() {
107         return mCachedSize;
108     }
109 }
110