This class is used for buffered reading of lines. For purposes of this class, a line ends
30 | * with "\n" or "\r\n". End of input is reported by throwing {@code EOFException}. Unterminated
31 | * line at end of input is invalid and will be ignored, the caller may use {@code
32 | * hasUnterminatedLine()} to detect it after catching the {@code EOFException}.
33 | *
34 | *
This class is intended for reading input that strictly consists of lines, such as line-based
35 | * cache entries or cache journal. Unlike the {@link java.io.BufferedReader} which in conjunction
36 | * with {@link java.io.InputStreamReader} provides similar functionality, this class uses different
37 | * end-of-input reporting and a more restrictive definition of a line.
38 | *
39 | *
This class supports only charsets that encode '\r' and '\n' as a single byte with value 13
40 | * and 10, respectively, and the representation of no other character contains these values.
41 | * We currently check in constructor that the charset is one of US-ASCII, UTF-8 and ISO-8859-1.
42 | * The default charset is US_ASCII.
43 | */
44 | class StrictLineReader implements Closeable {
45 | private static final byte CR = (byte) '\r';
46 | private static final byte LF = (byte) '\n';
47 |
48 | private final InputStream in;
49 | private final Charset charset;
50 |
51 | /*
52 | * Buffered data is stored in {@code buf}. As long as no exception occurs, 0 <= pos <= end
53 | * and the data in the range [pos, end) is buffered for reading. At end of input, if there is
54 | * an unterminated line, we set end == -1, otherwise end == pos. If the underlying
55 | * {@code InputStream} throws an {@code IOException}, end may remain as either pos or -1.
56 | */
57 | private byte[] buf;
58 | private int pos;
59 | private int end;
60 |
61 | /**
62 | * Constructs a new {@code LineReader} with the specified charset and the default capacity.
63 | *
64 | * @param in the {@code InputStream} to read data from.
65 | * @param charset the charset used to decode data. Only US-ASCII, UTF-8 and ISO-8859-1 are
66 | * supported.
67 | * @throws NullPointerException if {@code in} or {@code charset} is null.
68 | * @throws IllegalArgumentException if the specified charset is not supported.
69 | */
70 | public StrictLineReader(InputStream in, Charset charset) {
71 | this(in, 8192, charset);
72 | }
73 |
74 | /**
75 | * Constructs a new {@code LineReader} with the specified capacity and charset.
76 | *
77 | * @param in the {@code InputStream} to read data from.
78 | * @param capacity the capacity of the buffer.
79 | * @param charset the charset used to decode data. Only US-ASCII, UTF-8 and ISO-8859-1 are
80 | * supported.
81 | * @throws NullPointerException if {@code in} or {@code charset} is null.
82 | * @throws IllegalArgumentException if {@code capacity} is negative or zero
83 | * or the specified charset is not supported.
84 | */
85 | public StrictLineReader(InputStream in, int capacity, Charset charset) {
86 | if (in == null || charset == null) {
87 | throw new NullPointerException();
88 | }
89 | if (capacity < 0) {
90 | throw new IllegalArgumentException("capacity <= 0");
91 | }
92 | if (!(charset.equals(Util.US_ASCII))) {
93 | throw new IllegalArgumentException("Unsupported encoding");
94 | }
95 |
96 | this.in = in;
97 | this.charset = charset;
98 | buf = new byte[capacity];
99 | }
100 |
101 | /**
102 | * Closes the reader by closing the underlying {@code InputStream} and
103 | * marking this reader as closed.
104 | *
105 | * @throws IOException for errors when closing the underlying {@code InputStream}.
106 | */
107 | public void close() throws IOException {
108 | synchronized (in) {
109 | if (buf != null) {
110 | buf = null;
111 | in.close();
112 | }
113 | }
114 | }
115 |
116 | /**
117 | * Reads the next line. A line ends with {@code "\n"} or {@code "\r\n"},
118 | * this end of line marker is not included in the result.
119 | *
120 | * @return the next line from the input.
121 | * @throws IOException for underlying {@code InputStream} errors.
122 | * @throws EOFException for the end of source stream.
123 | */
124 | public String readLine() throws IOException {
125 | synchronized (in) {
126 | if (buf == null) {
127 | throw new IOException("LineReader is closed");
128 | }
129 |
130 | // Read more data if we are at the end of the buffered data.
131 | // Though it's an error to read after an exception, we will let {@code fillBuf()}
132 | // throw again if that happens; thus we need to handle end == -1 as well as end == pos.
133 | if (pos >= end) {
134 | fillBuf();
135 | }
136 | // Try to find LF in the buffered data and return the line if successful.
137 | for (int i = pos; i != end; ++i) {
138 | if (buf[i] == LF) {
139 | int lineEnd = (i != pos && buf[i - 1] == CR) ? i - 1 : i;
140 | String res = new String(buf, pos, lineEnd - pos, charset.name());
141 | pos = i + 1;
142 | return res;
143 | }
144 | }
145 |
146 | // Let's anticipate up to 80 characters on top of those already read.
147 | ByteArrayOutputStream out = new ByteArrayOutputStream(end - pos + 80) {
148 | @Override
149 | public String toString() {
150 | int length = (count > 0 && buf[count - 1] == CR) ? count - 1 : count;
151 | try {
152 | return new String(buf, 0, length, charset.name());
153 | } catch (UnsupportedEncodingException e) {
154 | throw new AssertionError(e); // Since we control the charset this will never happen.
155 | }
156 | }
157 | };
158 |
159 | while (true) {
160 | out.write(buf, pos, end - pos);
161 | // Mark unterminated line in case fillBuf throws EOFException or IOException.
162 | end = -1;
163 | fillBuf();
164 | // Try to find LF in the buffered data and return the line if successful.
165 | for (int i = pos; i != end; ++i) {
166 | if (buf[i] == LF) {
167 | if (i != pos) {
168 | out.write(buf, pos, i - pos);
169 | }
170 | pos = i + 1;
171 | return out.toString();
172 | }
173 | }
174 | }
175 | }
176 | }
177 |
178 | /**
179 | * Reads new input data into the buffer. Call only with pos == end or end == -1,
180 | * depending on the desired outcome if the function throws.
181 | */
182 | private void fillBuf() throws IOException {
183 | int result = in.read(buf, 0, buf.length);
184 | if (result == -1) {
185 | throw new EOFException();
186 | }
187 | pos = 0;
188 | end = result;
189 | }
190 | }
191 |
192 |
--------------------------------------------------------------------------------
/LazyCache/src/main/java/com/robin/lazy/cache/disk/impl/ext/Util.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2010 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 | package com.robin.lazy.cache.disk.impl.ext;
17 |
18 | import java.io.Closeable;
19 | import java.io.File;
20 | import java.io.IOException;
21 | import java.io.Reader;
22 | import java.io.StringWriter;
23 | import java.nio.charset.Charset;
24 |
25 | /** Junk drawer of utility methods. */
26 | final class Util {
27 | static final Charset US_ASCII = Charset.forName("US-ASCII");
28 | static final Charset UTF_8 = Charset.forName("UTF-8");
29 |
30 | private Util() {
31 | }
32 |
33 | static String readFully(Reader reader) throws IOException {
34 | try {
35 | StringWriter writer = new StringWriter();
36 | char[] buffer = new char[1024];
37 | int count;
38 | while ((count = reader.read(buffer)) != -1) {
39 | writer.write(buffer, 0, count);
40 | }
41 | return writer.toString();
42 | } finally {
43 | reader.close();
44 | }
45 | }
46 |
47 | /**
48 | * Deletes the contents of {@code dir}. Throws an IOException if any file
49 | * could not be deleted, or if {@code dir} is not a readable directory.
50 | */
51 | static void deleteContents(File dir) throws IOException {
52 | File[] files = dir.listFiles();
53 | if (files == null) {
54 | throw new IOException("not a readable directory: " + dir);
55 | }
56 | for (File file : files) {
57 | if (file.isDirectory()) {
58 | deleteContents(file);
59 | }
60 | if (!file.delete()) {
61 | throw new IOException("failed to delete file: " + file);
62 | }
63 | }
64 | }
65 |
66 | static void closeQuietly(/*Auto*/Closeable closeable) {
67 | if (closeable != null) {
68 | try {
69 | closeable.close();
70 | } catch (RuntimeException rethrown) {
71 | throw rethrown;
72 | } catch (Exception ignored) {
73 | }
74 | }
75 | }
76 | }
--------------------------------------------------------------------------------
/LazyCache/src/main/java/com/robin/lazy/cache/disk/naming/FileNameGenerator.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright 2011-2013 Sergey Tarasevich
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 | package com.robin.lazy.cache.disk.naming;
17 |
18 | /**
19 | * 名称转换器
20 | *
21 | * @author jiangyufeng
22 | * @version [版本号, 2015年12月15日]
23 | * @see [相关类/方法]
24 | * @since [产品/模块版本]
25 | */
26 | public interface FileNameGenerator {
27 |
28 | /**
29 | * 转换名称
30 | * @param keyName
31 | * @return
32 | */
33 | String generate(String keyName);
34 | }
35 |
--------------------------------------------------------------------------------
/LazyCache/src/main/java/com/robin/lazy/cache/disk/naming/HashCodeFileNameGenerator.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright 2011-2013 Sergey Tarasevich
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 | package com.robin.lazy.cache.disk.naming;
17 |
18 | /**
19 | * Names image file as image URI {@linkplain String#hashCode() hashcode}
20 | *
21 | * @author
22 | * @since
23 | */
24 | public class HashCodeFileNameGenerator implements FileNameGenerator {
25 | @Override
26 | public String generate(String imageUri) {
27 | return String.valueOf(imageUri.hashCode());
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/LazyCache/src/main/java/com/robin/lazy/cache/disk/naming/Md5FileNameGenerator.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright 2011-2013 Sergey Tarasevich
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 | package com.robin.lazy.cache.disk.naming;
17 |
18 | import com.robin.lazy.cache.util.log.CacheLog;
19 |
20 | import java.math.BigInteger;
21 | import java.security.MessageDigest;
22 | import java.security.NoSuchAlgorithmException;
23 |
24 | /**
25 | * Names image file as MD5 hash of image URI
26 | *
27 | * @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
28 | * @since 1.4.0
29 | */
30 | public class Md5FileNameGenerator implements FileNameGenerator {
31 |
32 | private final static String LOG_TAG=Md5FileNameGenerator.class.getSimpleName();
33 |
34 | private static final String HASH_ALGORITHM = "MD5";
35 | private static final int RADIX = 10 + 26; // 10 digits + 26 letters
36 |
37 | @Override
38 | public String generate(String imageUri) {
39 | byte[] md5 = getMD5(imageUri.getBytes());
40 | BigInteger bi = new BigInteger(md5).abs();
41 | return bi.toString(RADIX);
42 | }
43 |
44 | private byte[] getMD5(byte[] data) {
45 | byte[] hash = null;
46 | try {
47 | MessageDigest digest = MessageDigest.getInstance(HASH_ALGORITHM);
48 | digest.update(data);
49 | hash = digest.digest();
50 | } catch (NoSuchAlgorithmException e) {
51 | CacheLog.e(LOG_TAG, e.getMessage(),e);
52 | }
53 | return hash;
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/LazyCache/src/main/java/com/robin/lazy/cache/disk/read/BitmapReadFromDisk.java:
--------------------------------------------------------------------------------
1 | /*
2 | * 文 件 名: BitmapReadFromDisk.java
3 | * 版 权: Co., Ltd. Copyright YYYY-YYYY, All rights reserved
4 | * 描 述: <描述>
5 | * 修 改 人: jiangyufeng
6 | * 修改时间: 2015年12月11日
7 | * 跟踪单号: <跟踪单号>
8 | * 修改单号: <修改单号>
9 | * 修改内容: <修改内容>
10 | */
11 |
12 | package com.robin.lazy.cache.disk.read;
13 |
14 | import android.graphics.Bitmap;
15 | import android.graphics.BitmapFactory;
16 | import android.graphics.BitmapFactory.Options;
17 | import android.graphics.Matrix;
18 | import android.media.ExifInterface;
19 |
20 | import com.robin.lazy.cache.util.log.CacheLog;
21 | import com.robin.lazy.util.IoUtils;
22 | import com.robin.lazy.util.bitmap.ImageDecodingInfo;
23 | import com.robin.lazy.util.bitmap.ImageScaleType;
24 | import com.robin.lazy.util.bitmap.ImageSize;
25 | import com.robin.lazy.util.bitmap.ImageSizeUtils;
26 |
27 | import java.io.File;
28 | import java.io.FileInputStream;
29 | import java.io.IOException;
30 | import java.io.InputStream;
31 |
32 | /**
33 | * 从文件中读取图片
34 | *
35 | * @author jiangyufeng
36 | * @version [版本号, 2015年12月11日]
37 | * @see [相关类/方法]
38 | * @since [产品/模块版本]
39 | */
40 | public class BitmapReadFromDisk implements ReadFromDisk {
41 |
42 | private final static String LOG_TAG=BitmapReadFromDisk.class.getSimpleName();
43 |
44 | private ImageDecodingInfo imDecodeInfor;
45 |
46 | public BitmapReadFromDisk(ImageDecodingInfo imDecodeInfor) {
47 | this.imDecodeInfor = imDecodeInfor;
48 | }
49 |
50 | @Override
51 | public Bitmap readOut(File file) {
52 | Bitmap decodedBitmap = null;
53 | ImageFileInfo imageInfo = null;
54 | InputStream imageStream = null;
55 | try {
56 | imageStream = getImageStream(file);
57 | imageInfo = defineImageSizeAndRotation(file.getAbsolutePath(),
58 | imageStream, imDecodeInfor);
59 | imageStream = resetStream(file, imageStream, imDecodeInfor);
60 | Options decodingOptions = prepareDecodingOptions(
61 | imageInfo.imageSize, imDecodeInfor);
62 | decodedBitmap = BitmapFactory.decodeStream(imageStream, null,
63 | decodingOptions);
64 | } catch (IOException e) {
65 | } finally {
66 | IoUtils.closeSilently(imageStream);
67 | }
68 | if (decodedBitmap == null || imageInfo == null) {
69 | CacheLog.e(LOG_TAG,"Image can't be decoded [%s]");
70 | } else {
71 | decodedBitmap = considerExactScaleAndOrientatiton(decodedBitmap,
72 | imDecodeInfor, imageInfo.exif.rotation,
73 | imageInfo.exif.flipHorizontal);
74 | }
75 | return decodedBitmap;
76 | }
77 |
78 | /***
79 | * 获取文件流
80 | *
81 | * @param file
82 | * @return
83 | * @throws IOException
84 | * InputStream
85 | * @throws
86 | * @see [类、类#方法、类#成员]
87 | */
88 | private InputStream getImageStream(File file) throws IOException {
89 | return new FileInputStream(file);
90 | }
91 |
92 | protected Options prepareDecodingOptions(ImageSize imageSize,
93 | ImageDecodingInfo decodingInfo) {
94 | ImageScaleType scaleType = decodingInfo.getImageScaleType();
95 | int scale;
96 | if (scaleType == ImageScaleType.NONE) {
97 | scale = 1;
98 | } else if (scaleType == ImageScaleType.NONE_SAFE) {
99 | scale = ImageSizeUtils.computeMinImageSampleSize(imageSize);
100 | } else {
101 | ImageSize targetSize = decodingInfo.getTargetSize();
102 | boolean powerOf2 = scaleType == ImageScaleType.IN_SAMPLE_POWER_OF_2;
103 | scale = ImageSizeUtils.computeImageSampleSize(imageSize,
104 | targetSize, decodingInfo.getViewScaleType(), powerOf2);
105 | }
106 | Options decodingOptions = decodingInfo.getDecodingOptions();
107 | decodingOptions.inSampleSize = scale;
108 | return decodingOptions;
109 | }
110 |
111 | private InputStream resetStream(File fileName, InputStream imageStream,
112 | ImageDecodingInfo decodingInfo) throws IOException {
113 | try {
114 | imageStream.reset();
115 | } catch (IOException e) {
116 | IoUtils.closeSilently(imageStream);
117 | imageStream = getImageStream(fileName);
118 | }
119 | return imageStream;
120 | }
121 |
122 | private boolean canDefineExifParams(String mimeType) {
123 | return "image/jpeg".equalsIgnoreCase(mimeType);
124 | }
125 |
126 | private ExifInfo defineExifOrientation(String filePath) {
127 | int rotation = 0;
128 | boolean flip = false;
129 | try {
130 | ExifInterface exif = new ExifInterface(filePath);
131 | int exifOrientation = exif.getAttributeInt(
132 | ExifInterface.TAG_ORIENTATION,
133 | ExifInterface.ORIENTATION_NORMAL);
134 | switch (exifOrientation) {
135 | case ExifInterface.ORIENTATION_FLIP_HORIZONTAL :
136 | flip = true;
137 | case ExifInterface.ORIENTATION_NORMAL :
138 | rotation = 0;
139 | break;
140 | case ExifInterface.ORIENTATION_TRANSVERSE :
141 | flip = true;
142 | case ExifInterface.ORIENTATION_ROTATE_90 :
143 | rotation = 90;
144 | break;
145 | case ExifInterface.ORIENTATION_FLIP_VERTICAL :
146 | flip = true;
147 | case ExifInterface.ORIENTATION_ROTATE_180 :
148 | rotation = 180;
149 | break;
150 | case ExifInterface.ORIENTATION_TRANSPOSE :
151 | flip = true;
152 | case ExifInterface.ORIENTATION_ROTATE_270 :
153 | rotation = 270;
154 | break;
155 | }
156 | } catch (IOException e) {
157 | CacheLog.e("e", "Can't read EXIF tags from file [%s] \n"+filePath);
158 | }
159 | return new ExifInfo(rotation, flip);
160 | }
161 |
162 | private ImageFileInfo defineImageSizeAndRotation(String filePath,
163 | InputStream imageStream, ImageDecodingInfo decodingInfo)
164 | throws IOException {
165 | Options options = new Options();
166 | options.inJustDecodeBounds = true;
167 | BitmapFactory.decodeStream(imageStream, null, options);
168 |
169 | ExifInfo exif;
170 | if (decodingInfo.shouldConsiderExifParams()
171 | && canDefineExifParams(options.outMimeType)) {
172 | exif = defineExifOrientation(filePath);
173 | } else {
174 | exif = new ExifInfo();
175 | }
176 | return new ImageFileInfo(new ImageSize(options.outWidth,
177 | options.outHeight, exif.rotation), exif);
178 | }
179 |
180 | private Bitmap considerExactScaleAndOrientatiton(Bitmap subsampledBitmap,
181 | ImageDecodingInfo decodingInfo, int rotation, boolean flipHorizontal) {
182 | Matrix m = new Matrix();
183 | // Scale to exact size if need
184 | ImageScaleType scaleType = decodingInfo.getImageScaleType();
185 | if (scaleType == ImageScaleType.EXACTLY
186 | || scaleType == ImageScaleType.EXACTLY_STRETCHED) {
187 | ImageSize srcSize = new ImageSize(subsampledBitmap.getWidth(),
188 | subsampledBitmap.getHeight(), rotation);
189 | float scale = ImageSizeUtils.computeImageScale(srcSize,
190 | decodingInfo.getTargetSize(),
191 | decodingInfo.getViewScaleType(),
192 | scaleType == ImageScaleType.EXACTLY_STRETCHED);
193 | if (Float.compare(scale, 1f) != 0) {
194 | m.setScale(scale, scale);
195 | }
196 | }
197 | // Flip bitmap if need
198 | if (flipHorizontal) {
199 | m.postScale(-1, 1);
200 | }
201 | // Rotate bitmap if need
202 | if (rotation != 0) {
203 | m.postRotate(rotation);
204 | }
205 |
206 | Bitmap finalBitmap = Bitmap.createBitmap(subsampledBitmap, 0, 0,
207 | subsampledBitmap.getWidth(), subsampledBitmap.getHeight(), m,
208 | true);
209 | if (finalBitmap != subsampledBitmap) {
210 | subsampledBitmap.recycle();
211 | }
212 | return finalBitmap;
213 | }
214 |
215 | private static class ExifInfo {
216 |
217 | public final int rotation;
218 | public final boolean flipHorizontal;
219 |
220 | private ExifInfo() {
221 | this.rotation = 0;
222 | this.flipHorizontal = false;
223 | }
224 |
225 | private ExifInfo(int rotation, boolean flipHorizontal) {
226 | this.rotation = rotation;
227 | this.flipHorizontal = flipHorizontal;
228 | }
229 | }
230 |
231 | private static class ImageFileInfo {
232 |
233 | public final ImageSize imageSize;
234 | public final ExifInfo exif;
235 |
236 | private ImageFileInfo(ImageSize imageSize, ExifInfo exif) {
237 | this.imageSize = imageSize;
238 | this.exif = exif;
239 | }
240 | }
241 | }
242 |
--------------------------------------------------------------------------------
/LazyCache/src/main/java/com/robin/lazy/cache/disk/read/BytesReadFromDisk.java:
--------------------------------------------------------------------------------
1 | /*
2 | * 文 件 名: BytesReadFromDisk.java
3 | * 版 权: Co., Ltd. Copyright YYYY-YYYY, All rights reserved
4 | * 描 述: <描述>
5 | * 修 改 人: jiangyufeng
6 | * 修改时间: 2015年12月16日
7 | * 跟踪单号: <跟踪单号>
8 | * 修改单号: <修改单号>
9 | * 修改内容: <修改内容>
10 | */
11 |
12 | package com.robin.lazy.cache.disk.read;
13 |
14 | import com.robin.lazy.cache.util.log.CacheLog;
15 | import com.robin.lazy.util.IoUtils;
16 |
17 | import java.io.ByteArrayOutputStream;
18 | import java.io.File;
19 | import java.io.FileInputStream;
20 | import java.io.FileNotFoundException;
21 | import java.io.IOException;
22 | import java.io.InputStream;
23 |
24 | /**
25 | * 从文件中读取byte数组
26 | *
27 | * @author jiangyufeng
28 | * @version [版本号, 2015年12月16日]
29 | * @see [相关类/方法]
30 | * @since [产品/模块版本]
31 | */
32 | public class BytesReadFromDisk implements ReadFromDisk {
33 |
34 | private final static String LOG_TAG=BytesReadFromDisk.class.getSimpleName();
35 |
36 | private static final int DEFAULT_BUFFER_SIZE = 1 * 1024; // 4 Kb
37 |
38 | /** 缓冲流大小 */
39 | private int bufferSize = DEFAULT_BUFFER_SIZE;
40 |
41 | @Override
42 | public byte[] readOut(File file) {
43 | byte[] bytes = null;
44 | InputStream input = null;
45 | ByteArrayOutputStream baos = null;
46 | try {
47 | byte[] tmp = new byte[bufferSize];
48 | input = new FileInputStream(file);
49 | baos = new ByteArrayOutputStream();
50 | int size = 0;
51 | while ((size = input.read(tmp)) != -1) {
52 | baos.write(tmp, 0, size);
53 | }
54 | bytes = baos.toByteArray();
55 | } catch (FileNotFoundException e) {
56 | CacheLog.e(LOG_TAG,"从文件读取byte字节数组失败,没有找到文件",e);
57 | } catch (IOException e) {
58 | CacheLog.e(LOG_TAG, "从文件读取byte字节数组失败",e);
59 | } catch (Exception e) {
60 | CacheLog.e(LOG_TAG, "从文件读取byte字节数组失败",e);
61 | } finally {
62 | IoUtils.closeSilently(input);
63 | IoUtils.closeSilently(baos);
64 | }
65 | return bytes;
66 | }
67 |
68 | /**
69 | * 设置缓冲流大小
70 | *
71 | * @param bufferSize
72 | * void
73 | * @throws
74 | * @see [类、类#方法、类#成员]
75 | */
76 | public void setBufferSize(int bufferSize) {
77 | this.bufferSize = bufferSize;
78 | }
79 |
80 | }
81 |
--------------------------------------------------------------------------------
/LazyCache/src/main/java/com/robin/lazy/cache/disk/read/InputStreamReadFormDisk.java:
--------------------------------------------------------------------------------
1 | /*
2 | * 文 件 名: InputStreamReadFormDisk.java
3 | * 版 权: Co., Ltd. Copyright YYYY-YYYY, All rights reserved
4 | * 描 述: <描述>
5 | * 修 改 人: jiangyufeng
6 | * 修改时间: 2015年12月11日
7 | * 跟踪单号: <跟踪单号>
8 | * 修改单号: <修改单号>
9 | * 修改内容: <修改内容>
10 | */
11 |
12 | package com.robin.lazy.cache.disk.read;
13 |
14 | import com.robin.lazy.cache.util.log.CacheLog;
15 |
16 | import java.io.File;
17 | import java.io.FileInputStream;
18 | import java.io.FileNotFoundException;
19 | import java.io.InputStream;
20 |
21 | /**
22 | * 读取文件流
23 | *
24 | * @author jiangyufeng
25 | * @version [版本号, 2015年12月11日]
26 | * @see [相关类/方法]
27 | * @since [产品/模块版本]
28 | */
29 | public class InputStreamReadFormDisk implements ReadFromDisk {
30 |
31 | private final static String LOG_TAG=InputStreamReadFormDisk.class.getSimpleName();
32 |
33 | @Override
34 | public InputStream readOut(File file) {
35 | InputStream input = null;
36 | try {
37 | input = new FileInputStream(file);
38 | } catch (FileNotFoundException e) {
39 | CacheLog.e(LOG_TAG, "读取InputStream错误",e);
40 | } catch (Exception e) {
41 | CacheLog.e(LOG_TAG, "读取InputStream错误",e);
42 | }
43 | return input;
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/LazyCache/src/main/java/com/robin/lazy/cache/disk/read/ReadFromDisk.java:
--------------------------------------------------------------------------------
1 | /*
2 | * 文 件 名: DiskFileDecode.java
3 | * 版 权: Co., Ltd. Copyright YYYY-YYYY, All rights reserved
4 | * 描 述: <描述>
5 | * 修 改 人: jiangyufeng
6 | * 修改时间: 2015年12月11日
7 | * 跟踪单号: <跟踪单号>
8 | * 修改单号: <修改单号>
9 | * 修改内容: <修改内容>
10 | */
11 |
12 | package com.robin.lazy.cache.disk.read;
13 |
14 | import java.io.File;
15 |
16 | /**
17 | * 读取磁盘缓存方法接口
18 | *
19 | * @author jiangyufeng
20 | * @version [版本号, 2015年12月11日]
21 | * @param
22 | * @see [相关类/方法]
23 | * @since [产品/模块版本]
24 | */
25 | public interface ReadFromDisk {
26 |
27 | /***
28 | * 读文文件中内容
29 | * @param file
30 | * @return
31 | * V
32 | * @throws
33 | * @see [类、类#方法、类#成员]
34 | */
35 | V readOut(File file);
36 | }
37 |
--------------------------------------------------------------------------------
/LazyCache/src/main/java/com/robin/lazy/cache/disk/read/SerializableReadFromDisk.java:
--------------------------------------------------------------------------------
1 | /*
2 | * 文 件 名: SerialzableReadFromDisk.java
3 | * 版 权: Co., Ltd. Copyright YYYY-YYYY, All rights reserved
4 | * 描 述: <描述>
5 | * 修 改 人: jiangyufeng
6 | * 修改时间: 2015年12月11日
7 | * 跟踪单号: <跟踪单号>
8 | * 修改单号: <修改单号>
9 | * 修改内容: <修改内容>
10 | */
11 |
12 | package com.robin.lazy.cache.disk.read;
13 |
14 | import com.robin.lazy.cache.CacheLoaderManager;
15 | import com.robin.lazy.cache.util.log.CacheLog;
16 | import com.robin.lazy.util.IoUtils;
17 |
18 | import java.io.File;
19 | import java.io.FileInputStream;
20 | import java.io.IOException;
21 | import java.io.ObjectInputStream;
22 | import java.io.Serializable;
23 | import java.io.StreamCorruptedException;
24 |
25 | /**
26 | * 从文件中读取对象
27 | *
28 | * @author jiangyufeng
29 | * @version [版本号, 2015年12月11日]
30 | * @see [相关类/方法]
31 | * @since [产品/模块版本]
32 | */
33 | public class SerializableReadFromDisk implements ReadFromDisk {
34 |
35 | private final static String LOG_TAG=SerializableReadFromDisk.class.getSimpleName();
36 |
37 | @SuppressWarnings("unchecked")
38 | @Override
39 | public V readOut(File file) {
40 | V result = null;
41 | ObjectInputStream read = null;
42 | try {
43 | read = new ObjectInputStream(new FileInputStream(file));
44 | result=(V)read.readObject();
45 | } catch (StreamCorruptedException e) {
46 | CacheLog.e(LOG_TAG,"读取Serialzable错误",e);
47 | } catch (IOException e) {
48 | CacheLog.e(LOG_TAG,"读取Serialzable错误",e);
49 | } catch (ClassNotFoundException e) {
50 | CacheLog.e(LOG_TAG, "读取Serialzable错误",e);
51 | }finally{
52 | IoUtils.closeSilently(read);
53 | }
54 | return result;
55 | }
56 |
57 | }
58 |
--------------------------------------------------------------------------------
/LazyCache/src/main/java/com/robin/lazy/cache/disk/read/StringReadFromDisk.java:
--------------------------------------------------------------------------------
1 | /*
2 | * 文 件 名: StringDiskFileDecode.java
3 | * 版 权: Co., Ltd. Copyright YYYY-YYYY, All rights reserved
4 | * 描 述: <描述>
5 | * 修 改 人: jiangyufeng
6 | * 修改时间: 2015年12月11日
7 | * 跟踪单号: <跟踪单号>
8 | * 修改单号: <修改单号>
9 | * 修改内容: <修改内容>
10 | */
11 |
12 | package com.robin.lazy.cache.disk.read;
13 |
14 | import android.util.Log;
15 |
16 | import com.robin.lazy.cache.util.log.CacheLog;
17 | import com.robin.lazy.util.IoUtils;
18 |
19 | import java.io.BufferedReader;
20 | import java.io.File;
21 | import java.io.FileInputStream;
22 | import java.io.IOException;
23 | import java.io.InputStreamReader;
24 | import java.io.StreamCorruptedException;
25 |
26 | /**
27 | * 把本地文件解码成String
28 | *
29 | * @author jiangyufeng
30 | * @version [版本号, 2015年12月11日]
31 | * @see [相关类/方法]
32 | * @since [产品/模块版本]
33 | */
34 | public class StringReadFromDisk implements ReadFromDisk {
35 | private final static String LOG_TAG=StringReadFromDisk.class.getSimpleName();
36 |
37 | private final String DEFAULT_CHARSET = "UTF-8";
38 |
39 | /**
40 | * 编码类型
41 | */
42 | private String responseCharset = DEFAULT_CHARSET;
43 |
44 | @Override
45 | public String readOut(File File) {
46 | String s = null;
47 | BufferedReader read = null;
48 | try {
49 | // 创建字符流缓冲区
50 | read = new BufferedReader(new InputStreamReader(
51 | new FileInputStream(File), responseCharset));// 缓冲
52 | StringBuffer stBuffer = new StringBuffer();
53 | String temp = null;
54 | while ((temp = read.readLine()) != null) {
55 | stBuffer.append(temp);
56 | }
57 | s = stBuffer.toString();
58 | } catch (StreamCorruptedException e) {
59 | CacheLog.e(LOG_TAG, "读取String错误",e);
60 | } catch (IOException e) {
61 | CacheLog.e(LOG_TAG, "读取String错误",e);
62 | } finally {
63 | IoUtils.closeSilently(read);
64 | }
65 | return s;
66 | }
67 | /***
68 | * 设置String的编码类型
69 | *
70 | * @param responseCharset
71 | * void
72 | * @throws
73 | * @see [类、类#方法、类#成员]
74 | */
75 | public void setResponseCharset(String responseCharset) {
76 | this.responseCharset = responseCharset;
77 | }
78 |
79 | }
80 |
--------------------------------------------------------------------------------
/LazyCache/src/main/java/com/robin/lazy/cache/disk/write/BitmapWriteInDisk.java:
--------------------------------------------------------------------------------
1 | /*
2 | * 文 件 名: BitmapWriteInCache.java
3 | * 版 权: Co., Ltd. Copyright YYYY-YYYY, All rights reserved
4 | * 描 述: <描述>
5 | * 修 改 人: jiangyufeng
6 | * 修改时间: 2015年12月9日
7 | * 跟踪单号: <跟踪单号>
8 | * 修改单号: <修改单号>
9 | * 修改内容: <修改内容>
10 | */
11 |
12 | package com.robin.lazy.cache.disk.write;
13 |
14 | import android.graphics.Bitmap;
15 |
16 | import com.robin.lazy.cache.util.log.CacheLog;
17 | import com.robin.lazy.util.IoUtils;
18 |
19 | import java.io.BufferedOutputStream;
20 | import java.io.OutputStream;
21 |
22 | /**
23 | * 把图片写入缓存
24 | *
25 | * @author jiangyufeng
26 | * @version [版本号, 2015年12月9日]
27 | * @see [相关类/方法]
28 | * @since [产品/模块版本]
29 | */
30 | public class BitmapWriteInDisk extends WriteInDisk {
31 |
32 | private final static String LOG_TAG=BitmapWriteInDisk.class.getSimpleName();
33 |
34 | private static final int DEFAULT_BUFFER_SIZE = 32 * 1024; // 32 Kb
35 |
36 | private static final Bitmap.CompressFormat DEFAULT_COMPRESS_FORMAT = Bitmap.CompressFormat.PNG;
37 |
38 | private static final int DEFAULT_COMPRESS_QUALITY = 100;
39 |
40 | /** 图片格式 */
41 | private Bitmap.CompressFormat compressFormat = DEFAULT_COMPRESS_FORMAT;
42 |
43 | /** 缓冲流大小 */
44 | private int bufferSize = DEFAULT_BUFFER_SIZE;
45 |
46 | /** 压缩质量 */
47 | private int compressQuality = DEFAULT_COMPRESS_QUALITY;
48 |
49 | /** 是否回收 */
50 | private boolean isRecycle;
51 |
52 | public BitmapWriteInDisk(boolean isRecycle) {
53 | this.isRecycle = isRecycle;
54 | }
55 |
56 | @Override
57 | public boolean writeIn(OutputStream out,Bitmap values) {
58 | OutputStream os = new BufferedOutputStream(out, bufferSize);
59 | boolean isSuccess = false;
60 | try {
61 | isSuccess = values.compress(compressFormat, compressQuality,
62 | os);
63 | os.flush();
64 | } catch (Exception e) {
65 | CacheLog.e(LOG_TAG,"Bitmap写入缓存错误",e);
66 | } finally {
67 | IoUtils.closeSilently(os);
68 | if (isRecycle) {
69 | values.recycle();
70 | }
71 | }
72 | return isSuccess;
73 | }
74 |
75 | /**
76 | * 设置图片格式
77 | *
78 | * @param compressFormat
79 | * 格式 void
80 | * @throws
81 | * @see [类、类#方法、类#成员]
82 | */
83 | public void setCompressFormat(Bitmap.CompressFormat compressFormat) {
84 | this.compressFormat = compressFormat;
85 | }
86 |
87 | /**
88 | * 设置缓冲流大小
89 | *
90 | * @param bufferSize
91 | * void
92 | * @throws
93 | * @see [类、类#方法、类#成员]
94 | */
95 | public void setBufferSize(int bufferSize) {
96 | this.bufferSize = bufferSize;
97 | }
98 |
99 | /***
100 | * 设置图片质量
101 | *
102 | * @param compressQuality
103 | * void
104 | * @throws
105 | * @see [类、类#方法、类#成员]
106 | */
107 | public void setCompressQuality(int compressQuality) {
108 | this.compressQuality = compressQuality;
109 | }
110 |
111 | }
112 |
--------------------------------------------------------------------------------
/LazyCache/src/main/java/com/robin/lazy/cache/disk/write/BytesWriteInDisk.java:
--------------------------------------------------------------------------------
1 | /*
2 | * 文 件 名: BytesWriteInDisk.java
3 | * 版 权: Co., Ltd. Copyright YYYY-YYYY, All rights reserved
4 | * 描 述: <描述>
5 | * 修 改 人: jiangyufeng
6 | * 修改时间: 2015年12月16日
7 | * 跟踪单号: <跟踪单号>
8 | * 修改单号: <修改单号>
9 | * 修改内容: <修改内容>
10 | */
11 |
12 | package com.robin.lazy.cache.disk.write;
13 |
14 |
15 | import com.robin.lazy.cache.util.log.CacheLog;
16 | import com.robin.lazy.util.IoUtils;
17 |
18 | import java.io.IOException;
19 | import java.io.OutputStream;
20 |
21 | /**
22 | * byte数组写入文件
23 | *
24 | * @author jiangyufeng
25 | * @version [版本号, 2015年12月16日]
26 | * @see [相关类/方法]
27 | * @since [产品/模块版本]
28 | */
29 | public class BytesWriteInDisk extends WriteInDisk {
30 |
31 | private final static String LOG_TAG=BytesWriteInDisk.class.getSimpleName();
32 |
33 | @Override
34 | public boolean writeIn(OutputStream out,byte[] values) {
35 | boolean isSucess = false;
36 | try {
37 | out.write(values);
38 | out.flush();
39 | } catch (IOException e) {
40 | CacheLog.e(LOG_TAG,"byte数组写入文件错误",e);
41 | } catch (Exception e) {
42 | CacheLog.e(LOG_TAG, "byte数组写入文件错误",e);
43 | }
44 | IoUtils.closeSilently(out);
45 | return isSucess;
46 | }
47 |
48 | }
49 |
--------------------------------------------------------------------------------
/LazyCache/src/main/java/com/robin/lazy/cache/disk/write/InputStreamWriteInDisk.java:
--------------------------------------------------------------------------------
1 | /*
2 | * 文 件 名: InputStreamWriteInCache.java
3 | * 版 权: Co., Ltd. Copyright YYYY-YYYY, All rights reserved
4 | * 描 述: <描述>
5 | * 修 改 人: jiangyufeng
6 | * 修改时间: 2015年12月9日
7 | * 跟踪单号: <跟踪单号>
8 | * 修改单号: <修改单号>
9 | * 修改内容: <修改内容>
10 | */
11 |
12 | package com.robin.lazy.cache.disk.write;
13 |
14 |
15 | import com.robin.lazy.cache.util.log.CacheLog;
16 | import com.robin.lazy.util.IoUtils;
17 |
18 | import java.io.IOException;
19 | import java.io.InputStream;
20 | import java.io.OutputStream;
21 |
22 | /**
23 | * InputStream写入缓存
24 | *
25 | * @author jiangyufeng
26 | * @version [版本号, 2015年12月9日]
27 | * @see [相关类/方法]
28 | * @since [产品/模块版本]
29 | */
30 | public class InputStreamWriteInDisk extends WriteInDisk {
31 |
32 | private final static String LOG_TAG=InputStreamWriteInDisk.class.getSimpleName();
33 |
34 | private static final int DEFAULT_BUFFER_SIZE = 4 * 1024; // 4 Kb
35 |
36 | /** 缓冲流大小 */
37 | private int bufferSize = DEFAULT_BUFFER_SIZE;
38 |
39 | private IoUtils.CopyListener mListener;
40 |
41 | public InputStreamWriteInDisk(IoUtils.CopyListener listener) {
42 | this.mListener = listener;
43 | }
44 |
45 | @Override
46 | public boolean writeIn(OutputStream out,InputStream values) {
47 | boolean isSucess = false;
48 | try {
49 | isSucess = IoUtils.copyStream(values, out, mListener,
50 | bufferSize);
51 | out.flush();
52 | } catch (IOException e) {
53 | CacheLog.e(LOG_TAG, "InputStream写入缓存错误",e);
54 | } catch (Exception e) {
55 | CacheLog.e(LOG_TAG, "InputStream写入缓存错误",e);
56 | } finally {
57 | IoUtils.closeSilently(out);
58 | }
59 | return isSucess;
60 |
61 | }
62 |
63 | /**
64 | * 设置缓冲流大小
65 | *
66 | * @param bufferSize
67 | * void
68 | * @throws
69 | * @see [类、类#方法、类#成员]
70 | */
71 | public void setBufferSize(int bufferSize) {
72 | this.bufferSize = bufferSize;
73 | }
74 |
75 | }
76 |
--------------------------------------------------------------------------------
/LazyCache/src/main/java/com/robin/lazy/cache/disk/write/SerializableWriteInDisk.java:
--------------------------------------------------------------------------------
1 | /*
2 | * 文 件 名: StrigWriteInCache.java
3 | * 版 权: Co., Ltd. Copyright YYYY-YYYY, All rights reserved
4 | * 描 述: <描述>
5 | * 修 改 人: jiangyufeng
6 | * 修改时间: 2015年12月9日
7 | * 跟踪单号: <跟踪单号>
8 | * 修改单号: <修改单号>
9 | * 修改内容: <修改内容>
10 | */
11 |
12 | package com.robin.lazy.cache.disk.write;
13 |
14 |
15 | import com.robin.lazy.cache.disk.read.InputStreamReadFormDisk;
16 | import com.robin.lazy.cache.util.log.CacheLog;
17 | import com.robin.lazy.util.IoUtils;
18 |
19 | import java.io.IOException;
20 | import java.io.ObjectOutputStream;
21 | import java.io.OutputStream;
22 | import java.io.Serializable;
23 |
24 | /**
25 | * 把string 写入OutputStream中
26 | *
27 | * @author jiangyufeng
28 | * @version [版本号, 2015年12月9日]
29 | * @see [相关类/方法]
30 | * @since [产品/模块版本]
31 | */
32 | public class SerializableWriteInDisk extends WriteInDisk {
33 |
34 | private final static String LOG_TAG=SerializableWriteInDisk.class.getSimpleName();
35 |
36 | @Override
37 | public boolean writeIn(OutputStream out,V values){
38 | ObjectOutputStream objectOut = null;
39 | boolean isSucce = false;
40 | try {
41 | objectOut = new ObjectOutputStream(out);
42 | objectOut.writeObject(values);
43 | objectOut.flush();
44 | isSucce=true;
45 | } catch (IOException e) {
46 | CacheLog.e(LOG_TAG, "Serialzable写入缓存错误",e);
47 | }catch (Exception e) {
48 | CacheLog.e(LOG_TAG, "Serialzable写入缓存错误",e);
49 | }finally{
50 | IoUtils.closeSilently(objectOut);
51 | }
52 | return isSucce;
53 | }
54 |
55 | }
56 |
--------------------------------------------------------------------------------
/LazyCache/src/main/java/com/robin/lazy/cache/disk/write/StringWriteInDisk.java:
--------------------------------------------------------------------------------
1 | /*
2 | * 文 件 名: StrigWriteInCache.java
3 | * 版 权: Co., Ltd. Copyright YYYY-YYYY, All rights reserved
4 | * 描 述: <描述>
5 | * 修 改 人: jiangyufeng
6 | * 修改时间: 2015年12月9日
7 | * 跟踪单号: <跟踪单号>
8 | * 修改单号: <修改单号>
9 | * 修改内容: <修改内容>
10 | */
11 |
12 | package com.robin.lazy.cache.disk.write;
13 |
14 |
15 | import com.robin.lazy.cache.util.log.CacheLog;
16 | import com.robin.lazy.util.IoUtils;
17 |
18 | import java.io.IOException;
19 | import java.io.OutputStream;
20 | import java.io.OutputStreamWriter;
21 | import java.io.Writer;
22 |
23 | /**
24 | * 把string 写入OutputStream中
25 | *
26 | * @author jiangyufeng
27 | * @version [版本号, 2015年12月9日]
28 | * @see [相关类/方法]
29 | * @since [产品/模块版本]
30 | */
31 | public class StringWriteInDisk extends WriteInDisk {
32 |
33 | private final static String LOG_TAG=StringWriteInDisk.class.getSimpleName();
34 |
35 | private final String DEFAULT_CHARSET = "UTF-8";
36 |
37 | /**
38 | * 编码类型
39 | */
40 | private String responseCharset = DEFAULT_CHARSET;
41 |
42 | @Override
43 | public boolean writeIn(OutputStream out,String values) {
44 | Writer writer = null;
45 | boolean isSucce = false;
46 | try {
47 | writer = new OutputStreamWriter(out, responseCharset);
48 | writer.write(values);
49 | writer.flush();
50 | isSucce = true;
51 | } catch (IOException e) {
52 | CacheLog.e(LOG_TAG, "String写入缓存错误",e);
53 | } catch (Exception e) {
54 | CacheLog.e(LOG_TAG, "String写入缓存错误",e);
55 | }finally {
56 | IoUtils.closeSilently(writer);
57 | }
58 | return isSucce;
59 | }
60 |
61 | /***
62 | * 设置String的编码类型
63 | * @param responseCharset
64 | * void
65 | * @throws
66 | * @see [类、类#方法、类#成员]
67 | */
68 | public void setResponseCharset(String responseCharset) {
69 | this.responseCharset = responseCharset;
70 | }
71 |
72 | }
73 |
--------------------------------------------------------------------------------
/LazyCache/src/main/java/com/robin/lazy/cache/disk/write/WriteInDisk.java:
--------------------------------------------------------------------------------
1 | /*
2 | * 文 件 名: WriteInCacheBase.java
3 | * 版 权: Co., Ltd. Copyright YYYY-YYYY, All rights reserved
4 | * 描 述: <描述>
5 | * 修 改 人: jiangyufeng
6 | * 修改时间: 2015年12月9日
7 | * 跟踪单号: <跟踪单号>
8 | * 修改单号: <修改单号>
9 | * 修改内容: <修改内容>
10 | */
11 |
12 | package com.robin.lazy.cache.disk.write;
13 |
14 | import java.io.OutputStream;
15 |
16 | /**
17 | * 数据写入磁盘缓存方法类
18 | *
19 | * @author jiangyufeng
20 | * @version [版本号, 2015年12月9日]
21 | * @param
22 | * @see [相关类/方法]
23 | * @since [产品/模块版本]
24 | */
25 | public abstract class WriteInDisk {
26 |
27 | /***
28 | * 把数据写入OutputStream中
29 | * @param value 要存入磁盘中的值
30 | * @param out
31 | * @return boolean
32 | * @throws
33 | * @see [类、类#方法、类#成员]
34 | */
35 | public abstract boolean writeIn(OutputStream out,V value);
36 | }
37 |
--------------------------------------------------------------------------------
/LazyCache/src/main/java/com/robin/lazy/cache/entity/CacheGetEntity.java:
--------------------------------------------------------------------------------
1 | /*
2 | * 文 件 名: CacheGetEntity.java
3 | * 版 权: Co., Ltd. Copyright YYYY-YYYY, All rights reserved
4 | * 描 述: <描述>
5 | * 修 改 人: jiangyufeng
6 | * 修改时间: 2015年12月16日
7 | * 跟踪单号: <跟踪单号>
8 | * 修改单号: <修改单号>
9 | * 修改内容: <修改内容>
10 | */
11 |
12 | package com.robin.lazy.cache.entity;
13 |
14 | import com.robin.lazy.cache.disk.read.ReadFromDisk;
15 |
16 | /**
17 | * 缓存请求参数实体
18 | *
19 | * @author jiangyufeng
20 | * @version [版本号, 2015年12月16日]
21 | * @param
22 | * @see [相关类/方法]
23 | * @since [产品/模块版本]
24 | */
25 | public class CacheGetEntity {
26 | /***
27 | * 磁盘缓存读取
28 | */
29 | private ReadFromDisk readFromDisk;
30 |
31 | /**
32 | * <默认构造函数>
33 | */
34 | public CacheGetEntity(ReadFromDisk readFromDisk) {
35 | this.readFromDisk = readFromDisk;
36 | }
37 |
38 | public ReadFromDisk getReadFromDisk() {
39 | return readFromDisk;
40 | }
41 |
42 | public void setReadFromDisk(ReadFromDisk readFromDisk) {
43 | this.readFromDisk = readFromDisk;
44 | }
45 |
46 |
47 | }
48 |
--------------------------------------------------------------------------------
/LazyCache/src/main/java/com/robin/lazy/cache/entity/CachePutEntity.java:
--------------------------------------------------------------------------------
1 | /*
2 | * 文 件 名: InsertCacheEntity.java
3 | * 版 权: Co., Ltd. Copyright YYYY-YYYY, All rights reserved
4 | * 描 述: <描述>
5 | * 修 改 人: jiangyufeng
6 | * 修改时间: 2015年12月14日
7 | * 跟踪单号: <跟踪单号>
8 | * 修改单号: <修改单号>
9 | * 修改内容: <修改内容>
10 | */
11 |
12 | package com.robin.lazy.cache.entity;
13 |
14 | import com.robin.lazy.cache.disk.write.WriteInDisk;
15 |
16 | /**
17 | * 缓存存储参数实体
18 | *
19 | * @author jiangyufeng
20 | * @version [版本号, 2015年12月14日]
21 | * @param
22 | * @see [相关类/方法]
23 | * @since [产品/模块版本]
24 | */
25 | public class CachePutEntity {
26 |
27 | private WriteInDisk writeInDisk;
28 |
29 | public CachePutEntity(WriteInDisk writeInDisk) {
30 | this.writeInDisk = writeInDisk;
31 | }
32 |
33 | /**
34 | * 获取磁盘缓存写入方式接口
35 | * @return
36 | * WriteInDisk
37 | * @throws
38 | * @see [类、类#方法、类#成员]
39 | */
40 | public WriteInDisk getWriteInDisk() {
41 | return writeInDisk;
42 | }
43 |
44 | /***
45 | * 设置磁盘缓存写入方式接口
46 | * @param writeInDisk
47 | * void
48 | * @throws
49 | * @see [类、类#方法、类#成员]
50 | */
51 | public void setWriteInDisk(WriteInDisk writeInDisk) {
52 | this.writeInDisk = writeInDisk;
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/LazyCache/src/main/java/com/robin/lazy/cache/memory/EntryRemovedProcess.java:
--------------------------------------------------------------------------------
1 | /*
2 | * 文 件 名: EntryRemovedProcess.java
3 | * 版 权: Co., Ltd. Copyright YYYY-YYYY, All rights reserved
4 | * 描 述: <描述>
5 | * 修 改 人: jiangyufeng
6 | * 修改时间: 2016年1月13日
7 | * 跟踪单号: <跟踪单号>
8 | * 修改单号: <修改单号>
9 | * 修改内容: <修改内容>
10 | */
11 |
12 | package com.robin.lazy.cache.memory;
13 | /**
14 | * 数据被删除后进行垃圾回收的一些处理
15 | *
16 | * @author jiangyufeng
17 | * @version [版本号, 2016年1月13日]
18 | * @param
19 | * @see [相关类/方法]
20 | * @since [产品/模块版本]
21 | */
22 | public class EntryRemovedProcess {
23 |
24 | /***
25 | * 当item被回收或者删掉时调用。改方法当value被回收释放存储空间时被remove调用,
26 | * 或者替换item值时put调用,默认实现什么都没做。
27 | *
28 | * @param evicted 是否释放被删除的空间
29 | * @param key
30 | * @param oldValue 老的数据
31 | * @param newValue 新数据 void
32 | * @throws
33 | * @see [类、类#方法、类#成员]
34 | */
35 | public void entryRemoved(boolean evicted, String key, V oldValue,
36 | V newValue) {
37 | oldValue = null;
38 | }
39 | }
40 |
41 |
--------------------------------------------------------------------------------
/LazyCache/src/main/java/com/robin/lazy/cache/memory/MemoryCache.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright 2014 Sergey Tarasevich
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 | package com.robin.lazy.cache.memory;
17 |
18 | import com.robin.lazy.cache.Cache;
19 |
20 | import java.util.Collection;
21 | import java.util.Map;
22 |
23 | /**
24 | * 内存缓存操作接口
25 | *
26 | * @author jiangyufeng
27 | * @version [版本号, 2015年12月15日]
28 | * @see [相关类/方法]
29 | * @since [产品/模块版本]
30 | */
31 | public interface MemoryCache extends Cache {
32 |
33 | /**
34 | * 获取所有的key
35 | * @return
36 | */
37 | Collection keys();
38 |
39 | /**
40 | * 获取缓存数据集合
41 | * @return
42 | */
43 | Map snapshot();
44 |
45 | }
46 |
--------------------------------------------------------------------------------
/LazyCache/src/main/java/com/robin/lazy/cache/memory/SizeOfCacheCalculator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * 文 件 名: SizeOfCacheCalculator.java
3 | * 版 权: Co., Ltd. Copyright YYYY-YYYY, All rights reserved
4 | * 描 述: <描述>
5 | * 修 改 人: jiangyufeng
6 | * 修改时间: 2015年12月17日
7 | * 跟踪单号: <跟踪单号>
8 | * 修改单号: <修改单号>
9 | * 修改内容: <修改内容>
10 | */
11 |
12 | package com.robin.lazy.cache.memory;
13 |
14 | import android.graphics.Bitmap;
15 |
16 | /**
17 | * 内存大小计算器
18 | *
19 | * @author jiangyufeng
20 | * @version [版本号, 2015年12月17日]
21 | * @param
22 | * @see [相关类/方法]
23 | * @since [产品/模块版本]
24 | */
25 | public class SizeOfCacheCalculator {
26 |
27 | /**
28 | * 计算一个缓存数据的大小,默认是1(就是一个数据)
29 | *
30 | * @param key
31 | * @param value
32 | * @return int
33 | * @throws
34 | * @see [类、类#方法、类#成员]
35 | */
36 | public int sizeOf(String key, V value) {
37 | if (value instanceof Bitmap) {
38 | Bitmap bitmap = (Bitmap) value;
39 | return bitmap.getRowBytes() * bitmap.getHeight();
40 | } else if (value instanceof byte[]) {
41 | return ((byte[]) value).length;
42 | }
43 | return 1;
44 | }
45 | }
46 |
47 |
--------------------------------------------------------------------------------
/LazyCache/src/main/java/com/robin/lazy/cache/memory/impl/EntryRemovedProcessMemoryCache.java:
--------------------------------------------------------------------------------
1 | /*
2 | * 文 件 名: EntryRemovedProcessMemoryCache.java
3 | * 版 权: Co., Ltd. Copyright YYYY-YYYY, All rights reserved
4 | * 描 述: <描述>
5 | * 修 改 人: jiangyufeng
6 | * 修改时间: 2016年1月13日
7 | * 跟踪单号: <跟踪单号>
8 | * 修改单号: <修改单号>
9 | * 修改内容: <修改内容>
10 | */
11 |
12 | package com.robin.lazy.cache.memory.impl;
13 |
14 | import com.robin.lazy.cache.memory.EntryRemovedProcess;
15 |
16 | /**
17 | *可以自定义数据被删除后的垃圾回收处理的缓存
18 | *
19 | * @author jiangyufeng
20 | * @version [版本号, 2016年1月13日]
21 | * @see [相关类/方法]
22 | * @since [产品/模块版本]
23 | */
24 | public class EntryRemovedProcessMemoryCache extends LruMemoryCache {
25 |
26 | /**
27 | * 垃圾回收处理者
28 | */
29 | @SuppressWarnings("rawtypes")
30 | private EntryRemovedProcess mEntryRemovedProcess;
31 |
32 | public EntryRemovedProcessMemoryCache(int maxSize,
33 | EntryRemovedProcess> entryRemovedProcess) {
34 | super(maxSize);
35 | this.mEntryRemovedProcess = entryRemovedProcess;
36 | }
37 |
38 | @SuppressWarnings("unchecked")
39 | @Override
40 | protected void entryRemoved(boolean evicted, String key, V oldValue,
41 | V newValue) {
42 | if (mEntryRemovedProcess != null) {
43 | mEntryRemovedProcess.entryRemoved(evicted, key, oldValue, newValue);
44 | } else {
45 | super.entryRemoved(evicted, key, oldValue, newValue);
46 | }
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/LazyCache/src/main/java/com/robin/lazy/cache/memory/impl/FuzzyKeyMemoryCache.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright 2011-2014 Sergey Tarasevich
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 | package com.robin.lazy.cache.memory.impl;
17 |
18 |
19 | import com.robin.lazy.cache.memory.MemoryCache;
20 | import com.robin.lazy.cache.util.log.CacheLog;
21 |
22 | import java.util.Collection;
23 | import java.util.Comparator;
24 | import java.util.Map;
25 |
26 | /**
27 | * Decorator for {@link MemoryCache}. Provides special feature for cache: some
28 | * different keys are considered as equals (using {@link Comparator comparator}
29 | * ). And when you try to put some value into cache by key so entries with
30 | * "equals" keys will be removed from cache before.
31 | * NOTE: Used for internal needs. Normally you don't need to use this
32 | * class.
33 | *
34 | * @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
35 | * @since 1.0.0
36 | */
37 | public class FuzzyKeyMemoryCache implements MemoryCache {
38 |
39 | private final static String LOG_TAG=FuzzyKeyMemoryCache.class.getSimpleName();
40 |
41 | private final MemoryCache cache;
42 | private final Comparator keyComparator;
43 |
44 | public FuzzyKeyMemoryCache(MemoryCache cache,
45 | Comparator keyComparator) {
46 | this.cache = cache;
47 | this.keyComparator = keyComparator;
48 | }
49 |
50 | @Override
51 | public boolean put(String key, V value) {
52 | if (cache == null) {
53 | CacheLog.e(LOG_TAG, "MemoryCache缓存操作对象为空",new NullPointerException());
54 | return false;
55 | }
56 | // Search equal key and remove this entry
57 | synchronized (cache) {
58 | String keyToRemove = null;
59 | for (String cacheKey : cache.keys()) {
60 | if (keyComparator.compare(key, cacheKey) == 0) {
61 | keyToRemove = cacheKey;
62 | break;
63 | }
64 | }
65 | if (keyToRemove != null) {
66 | cache.remove(keyToRemove);
67 | }
68 | }
69 | return cache.put(key, value);
70 | }
71 |
72 | @Override
73 | public boolean put(String key, V value, long maxLimitTime) {
74 | if (cache == null) {
75 | CacheLog.e(LOG_TAG, "MemoryCache缓存操作对象为空",new NullPointerException());
76 | return false;
77 | }
78 | synchronized (cache) {
79 | String keyToRemove = null;
80 | for (String cacheKey : cache.keys()) {
81 | if (keyComparator.compare(key, cacheKey) == 0) {
82 | keyToRemove = cacheKey;
83 | break;
84 | }
85 | }
86 | if (keyToRemove != null) {
87 | cache.remove(keyToRemove);
88 | }
89 | }
90 | return cache.put(key, value, maxLimitTime);
91 | }
92 |
93 | @Override
94 | public V get(String key) {
95 | if (cache == null) {
96 | CacheLog.e(LOG_TAG, "MemoryCache缓存操作对象为空",new NullPointerException());
97 | return null;
98 | }
99 | return cache.get(key);
100 | }
101 |
102 | @Override
103 | public boolean remove(String key) {
104 | if (cache == null) {
105 | CacheLog.e(LOG_TAG ,"MemoryCache缓存操作对象为空",new NullPointerException());
106 | return false;
107 | }
108 | return cache.remove(key);
109 | }
110 |
111 | @Override
112 | public Collection keys() {
113 | if (cache == null) {
114 | CacheLog.e(LOG_TAG, "MemoryCache缓存操作对象为空",new NullPointerException());
115 | return null;
116 | }
117 | return cache.keys();
118 | }
119 |
120 | @Override
121 | public Map snapshot() {
122 | if (cache == null) {
123 | CacheLog.e(LOG_TAG, "MemoryCache缓存操作对象为空",new NullPointerException());
124 | return null;
125 | }
126 | return cache.snapshot();
127 | }
128 |
129 | @Override
130 | public void resize(int maxSize) {
131 | if (cache == null) {
132 | CacheLog.e(LOG_TAG, "MemoryCache缓存操作对象为空",new NullPointerException());
133 | return;
134 | }
135 | cache.resize(maxSize);
136 | }
137 |
138 | @Override
139 | public void clear() {
140 | if (cache == null) {
141 | CacheLog.e(LOG_TAG, "MemoryCache缓存操作对象为空",new NullPointerException());
142 | return;
143 | }
144 | cache.clear();
145 | }
146 |
147 | @Override
148 | public void close() {
149 | if (cache == null) {
150 | CacheLog.e(LOG_TAG, "MemoryCache缓存操作对象为空",new NullPointerException());
151 | return;
152 | }
153 | cache.close();
154 | }
155 | }
156 |
--------------------------------------------------------------------------------
/LazyCache/src/main/java/com/robin/lazy/cache/memory/impl/LimitedAgeMemoryCache.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright 2011-2014 Sergey Tarasevich
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 | package com.robin.lazy.cache.memory.impl;
17 |
18 | import android.icu.util.TimeUnit;
19 | import android.util.TimeUtils;
20 |
21 | import com.robin.lazy.cache.LimitedAge;
22 | import com.robin.lazy.cache.memory.MemoryCache;
23 | import com.robin.lazy.cache.util.log.CacheLog;
24 |
25 | import java.sql.Timestamp;
26 | import java.util.Collection;
27 | import java.util.Collections;
28 | import java.util.HashMap;
29 | import java.util.Map;
30 |
31 | /**
32 | * Decorator for {@link MemoryCache}. Provides special feature for cache: if
33 | * some cached object age exceeds defined value then this object will be removed
34 | * from cache.
35 | *
36 | * @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
37 | * @see MemoryCache
38 | * @since 1.3.1
39 | */
40 | public class LimitedAgeMemoryCache implements MemoryCache {
41 |
42 | private final static String LOG_TAG=LimitedAgeMemoryCache.class.getSimpleName();
43 |
44 | private final MemoryCache cache;
45 |
46 | /**缓存数据最大保存时间(单位秒,小于等于0时代表长期有效)*/
47 | private final long maxAge;
48 | private final Map loadingDates = Collections
49 | .synchronizedMap(new HashMap());
50 |
51 | /**
52 | * @param cache
53 | * Wrapped memory cache
54 | * @param maxAge
55 | * Max object age (in seconds). If object age will exceed
56 | * this value then it'll be removed from cache on next treatment
57 | * (and therefore be reloaded).
58 | */
59 | public LimitedAgeMemoryCache(MemoryCache cache, long maxAge) {
60 | this.cache = cache;
61 | this.maxAge = maxAge;
62 | }
63 |
64 | @Override
65 | public boolean put(String key, V value) {
66 | if (cache == null) {
67 | CacheLog.e(LOG_TAG, "MemoryCache缓存操作对象为空",new NullPointerException());
68 | return false;
69 | }
70 | boolean putSuccesfully = cache.put(key, value);
71 | if (putSuccesfully) {
72 | loadingDates.put(key, new LimitedAge(System.currentTimeMillis(),
73 | maxAge));
74 | }
75 | return putSuccesfully;
76 | }
77 |
78 | @Override
79 | public boolean put(String key, V value, long maxLimitTime) {
80 | if (cache == null) {
81 | CacheLog.e(LOG_TAG, "MemoryCache缓存操作对象为空",new NullPointerException());
82 | return false;
83 | }
84 | boolean putSuccesfully = cache.put(key, value);
85 | if (putSuccesfully) {
86 | loadingDates.put(key, new LimitedAge(System.currentTimeMillis(),
87 | maxLimitTime));
88 | }
89 | return putSuccesfully;
90 | }
91 |
92 | @Override
93 | public V get(String key) {
94 | if (cache == null) {
95 | CacheLog.e(LOG_TAG,"MemoryCache缓存操作对象为空",new NullPointerException());
96 | return null;
97 | }
98 | LimitedAge loadingDate = loadingDates.get(key);
99 | if (loadingDate != null && loadingDate.checkExpire()) {
100 | cache.remove(key);
101 | loadingDates.remove(key);
102 | }
103 |
104 | return cache.get(key);
105 | }
106 |
107 | @Override
108 | public boolean remove(String key) {
109 | if (cache == null) {
110 | CacheLog.e(LOG_TAG,"MemoryCache缓存操作对象为空",new NullPointerException());
111 | return false;
112 | }
113 | loadingDates.remove(key);
114 | return cache.remove(key);
115 | }
116 |
117 | @Override
118 | public Collection keys() {
119 | if (cache == null) {
120 | CacheLog.e(LOG_TAG, "MemoryCache缓存操作对象为空",new NullPointerException());
121 | return null;
122 | }
123 | return cache.keys();
124 | }
125 |
126 | @Override
127 | public Map snapshot() {
128 | if (cache == null) {
129 | CacheLog.e(LOG_TAG, "MemoryCache缓存操作对象为空",new NullPointerException());
130 | return null;
131 | }
132 | return cache.snapshot();
133 | }
134 |
135 | @Override
136 | public void resize(int maxSize) {
137 | if (cache == null) {
138 | CacheLog.e(LOG_TAG, "MemoryCache缓存操作对象为空",new NullPointerException());
139 | return;
140 | }
141 | cache.resize(maxSize);
142 | }
143 |
144 | @Override
145 | public void clear() {
146 | if (cache == null) {
147 | CacheLog.e(LOG_TAG, "MemoryCache缓存操作对象为空",new NullPointerException());
148 | return;
149 | }
150 | loadingDates.clear();
151 | cache.clear();
152 | }
153 |
154 | @Override
155 | public void close() {
156 | if (cache == null) {
157 | CacheLog.e(LOG_TAG, "MemoryCache缓存操作对象为空",new NullPointerException());
158 | return;
159 | }
160 | loadingDates.clear();
161 | cache.close();
162 | }
163 |
164 | }
165 |
--------------------------------------------------------------------------------
/LazyCache/src/main/java/com/robin/lazy/cache/memory/impl/LruMemoryCache.java:
--------------------------------------------------------------------------------
1 | /*
2 | * 文 件 名: LruMemoryCache.java
3 | * 版 权: Co., Ltd. Copyright YYYY-YYYY, All rights reserved
4 | * 描 述: <描述>
5 | * 修 改 人: jiangyufeng
6 | * 修改时间: 2015年12月14日
7 | * 跟踪单号: <跟踪单号>
8 | * 修改单号: <修改单号>
9 | * 修改内容: <修改内容>
10 | */
11 |
12 | package com.robin.lazy.cache.memory.impl;
13 |
14 | import com.robin.lazy.cache.memory.MemoryCache;
15 | import com.robin.lazy.cache.memory.impl.ext.LruCache;
16 | import com.robin.lazy.cache.util.log.CacheLog;
17 |
18 | import java.util.Collection;
19 | import java.util.HashSet;
20 | import java.util.Map;
21 |
22 | /**
23 | * Lru算法的内存缓存
24 | *
25 | * @author jiangyufeng
26 | * @version [版本号, 2015年12月14日]
27 | * @see [相关类/方法]
28 | * @since [产品/模块版本]
29 | */
30 | public class LruMemoryCache implements MemoryCache {
31 |
32 | private final static String LOG_TAG=LruMemoryCache.class.getSimpleName();
33 |
34 | /** 内存缓存存储类 */
35 | private LruCache lruCache;
36 |
37 | /***
38 | * @param maxSize
39 | * 内存最大限度
40 | */
41 | public LruMemoryCache(int maxSize) {
42 | if (maxSize <= 0) {
43 | throw new IllegalArgumentException("maxSize <= 0");
44 | }
45 | lruCache = new LruCache(maxSize) {
46 | @Override
47 | protected int sizeOf(String key, Object value) {
48 | return LruMemoryCache.this.sizeOf(key, value);
49 | }
50 |
51 | protected void entryRemoved(boolean evicted, String key,
52 | Object oldValue, Object newValue) {
53 | LruMemoryCache.this.entryRemoved(evicted, key, oldValue,
54 | newValue);
55 | };
56 | };
57 | }
58 |
59 | @Override
60 | public boolean put(String key, V value) {
61 | if (key == null || value == null) {
62 | throw new NullPointerException("key == null || value == null");
63 | }
64 | if (lruCache != null) {
65 | try {
66 | lruCache.put(key, value);
67 | return true;
68 | } catch (NullPointerException e) {
69 | CacheLog.e(LOG_TAG, "put to menory fail",e);
70 | } catch (Exception e) {
71 | CacheLog.e(LOG_TAG, "put to menory fail",e);
72 | }
73 | }
74 | return false;
75 | }
76 |
77 | @Override
78 | public boolean put(String key, V value, long maxLimitTime) {
79 | return put(key, value);
80 | }
81 |
82 | @SuppressWarnings("unchecked")
83 | @Override
84 | public V get(String key) {
85 | if (key == null) {
86 | throw new NullPointerException("key == null");
87 | }
88 | if (lruCache != null) {
89 | V values = null;
90 | try {
91 | values = (V) lruCache.get(key);
92 | } catch (NullPointerException e) {
93 | CacheLog.e(LOG_TAG, "缓存数据不存在,不能强制类型转换",e);
94 | } catch (ClassCastException e) {
95 | CacheLog.e(LOG_TAG, "强制类型转换错误,不符合的类型",e);
96 | } catch (Exception e) {
97 | CacheLog.e(LOG_TAG, e.getMessage(),e);
98 | }
99 | return values;
100 | }
101 | return null;
102 | }
103 |
104 | @Override
105 | public boolean remove(String key) {
106 | if (key == null) {
107 | throw new NullPointerException("key == null");
108 | }
109 | if (lruCache != null && lruCache.remove(key) != null) {
110 | return true;
111 | }
112 | return false;
113 | }
114 |
115 | @Override
116 | public Map snapshot() {
117 | if (lruCache != null) {
118 | return lruCache.snapshot();
119 | }
120 | return null;
121 | }
122 |
123 | @Override
124 | public Collection keys() {
125 | Map map = snapshot();
126 | if (map != null) {
127 | return new HashSet(map.keySet());
128 | }
129 | return null;
130 |
131 | }
132 |
133 | @Override
134 | public void resize(int maxSize) {
135 | if (maxSize <= 0) {
136 | throw new IllegalArgumentException("maxSize <= 0");
137 | }
138 | if (lruCache != null) {
139 | lruCache.resize(maxSize);
140 | }
141 | }
142 |
143 | @Override
144 | public void clear() {
145 | if (lruCache != null) {
146 | lruCache.evictAll();
147 | }
148 | }
149 |
150 | @Override
151 | public void close() {
152 | clear();
153 | if (lruCache != null) {
154 | lruCache = null;
155 | }
156 | }
157 |
158 | /***
159 | * 当item被回收或者删掉时调用。改方法当value被回收释放存储空间时被remove调用,
160 | * 或者替换item值时put调用,默认实现什么都没做。
161 | *
162 | * @param
163 | * @param evicted 是否释放被删除的空间
164 | * @param key
165 | * @param oldValue 老的数据
166 | * @param newValue 新数据 void
167 | * @throws
168 | * @see [类、类#方法、类#成员]
169 | */
170 | protected void entryRemoved(boolean evicted, String key, V oldValue,
171 | V newValue) {
172 | oldValue = null;
173 | };
174 |
175 | /**
176 | * 计算一个缓存数据的大小,默认是1(就是一个数据)
177 | *
178 | * @param
179 | *
180 | * @param key
181 | * @param value
182 | * @return int
183 | * @throws
184 | * @see [类、类#方法、类#成员]
185 | */
186 | protected int sizeOf(String key, V value) {
187 | return 1;
188 | }
189 |
190 | @Override
191 | public final String toString() {
192 | if (lruCache != null) {
193 | return lruCache.toString();
194 | }
195 | return String.format("LruCache[maxSize=%d]", 0);
196 | }
197 |
198 | }
199 |
--------------------------------------------------------------------------------
/LazyCache/src/main/java/com/robin/lazy/cache/memory/impl/SizeOfMemoryCache.java:
--------------------------------------------------------------------------------
1 | /*
2 | * 文 件 名: SizeOfMemoryCache.java
3 | * 版 权: Co., Ltd. Copyright YYYY-YYYY, All rights reserved
4 | * 描 述: <描述>
5 | * 修 改 人: jiangyufeng
6 | * 修改时间: 2015年12月17日
7 | * 跟踪单号: <跟踪单号>
8 | * 修改单号: <修改单号>
9 | * 修改内容: <修改内容>
10 | */
11 |
12 | package com.robin.lazy.cache.memory.impl;
13 |
14 | import com.robin.lazy.cache.memory.SizeOfCacheCalculator;
15 |
16 | /**
17 | * 重新计算每个缓存数据占的大小的缓存类
18 | *
19 | * @author jiangyufeng
20 | * @version [版本号, 2015年12月17日]
21 | * @see [相关类/方法]
22 | * @since [产品/模块版本]
23 | */
24 | public class SizeOfMemoryCache extends LruMemoryCache {
25 |
26 | /** 一个缓存占用的内存计算器 */
27 | @SuppressWarnings("rawtypes")
28 | private SizeOfCacheCalculator cacheCalculator;
29 |
30 | public SizeOfMemoryCache(int maxSize, SizeOfCacheCalculator> cacheCalculator) {
31 | super(maxSize);
32 | this.cacheCalculator = cacheCalculator;
33 | }
34 |
35 | @SuppressWarnings("unchecked")
36 | @Override
37 | protected int sizeOf(String key, V value) {
38 | if (key != null && value == null) {
39 | return super.sizeOf(key, value);
40 | } else if (key == null && value == null) {
41 | return 0;
42 | }
43 | if (cacheCalculator != null) {
44 | return cacheCalculator.sizeOf(key, value);
45 | }
46 | return super.sizeOf(key, value);
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/LazyCache/src/main/java/com/robin/lazy/cache/memory/impl/ext/LruCache.java:
--------------------------------------------------------------------------------
1 | package com.robin.lazy.cache.memory.impl.ext;
2 |
3 | import java.util.LinkedHashMap;
4 | import java.util.Map;
5 |
6 | /**
7 | * Static library version of {@link android.util.LruCache}. Used to write apps
8 | * that run on API levels prior to 12. When running on API level 12 or above,
9 | * this implementation is still used; it does not try to switch to the
10 | * framework's implementation. See the framework SDK documentation for a class
11 | * overview.
12 | */
13 |
14 | public class LruCache {
15 | private final LinkedHashMap map;
16 |
17 | /** Size of this cache in units. Not necessarily the number of elements. */
18 | private int size;
19 | private int maxSize;
20 |
21 | private int putCount;
22 | private int createCount;
23 | private int evictionCount;
24 | private int hitCount;
25 | private int missCount;
26 |
27 | /**
28 | * @param maxSize for caches that do not override {@link #sizeOf}, this is
29 | * the maximum number of entries in the cache. For all other caches,
30 | * this is the maximum sum of the sizes of the entries in this cache.
31 | */
32 | public LruCache(int maxSize) {
33 | if (maxSize <= 0) {
34 | throw new IllegalArgumentException("maxSize <= 0");
35 | }
36 | this.maxSize = maxSize;
37 | this.map = new LinkedHashMap(0, 0.75f, true);
38 | }
39 |
40 | /**
41 | * Sets the size of the cache.
42 | *
43 | * @param maxSize The new maximum size.
44 | */
45 | public void resize(int maxSize) {
46 | if (maxSize <= 0) {
47 | throw new IllegalArgumentException("maxSize <= 0");
48 | }
49 |
50 | synchronized (this) {
51 | this.maxSize = maxSize;
52 | }
53 | trimToSize(maxSize);
54 | }
55 |
56 | /**
57 | * Returns the value for {@code key} if it exists in the cache or can be
58 | * created by {@code #create}. If a value was returned, it is moved to the
59 | * head of the queue. This returns null if a value is not cached and cannot
60 | * be created.
61 | */
62 | public final V get(K key) {
63 | if (key == null) {
64 | throw new NullPointerException("key == null");
65 | }
66 |
67 | V mapValue;
68 | synchronized (this) {
69 | mapValue = map.get(key);
70 | if (mapValue != null) {
71 | hitCount++;
72 | return mapValue;
73 | }
74 | missCount++;
75 | }
76 |
77 | /*
78 | * Attempt to create a value. This may take a long time, and the map
79 | * may be different when create() returns. If a conflicting value was
80 | * added to the map while create() was working, we leave that value in
81 | * the map and release the created value.
82 | */
83 |
84 | V createdValue = create(key);
85 | if (createdValue == null) {
86 | return null;
87 | }
88 |
89 | synchronized (this) {
90 | createCount++;
91 | mapValue = map.put(key, createdValue);
92 |
93 | if (mapValue != null) {
94 | // There was a conflict so undo that last put
95 | map.put(key, mapValue);
96 | } else {
97 | size += safeSizeOf(key, createdValue);
98 | }
99 | }
100 |
101 | if (mapValue != null) {
102 | entryRemoved(false, key, createdValue, mapValue);
103 | return mapValue;
104 | } else {
105 | trimToSize(maxSize);
106 | return createdValue;
107 | }
108 | }
109 |
110 | /**
111 | * Caches {@code value} for {@code key}. The value is moved to the head of
112 | * the queue.
113 | *
114 | * @return the previous value mapped by {@code key}.
115 | */
116 | public final V put(K key, V value) {
117 | if (key == null || value == null) {
118 | throw new NullPointerException("key == null || value == null");
119 | }
120 |
121 | V previous;
122 | synchronized (this) {
123 | putCount++;
124 | size += safeSizeOf(key, value);
125 | previous = map.put(key, value);
126 | if (previous != null) {
127 | size -= safeSizeOf(key, previous);
128 | }
129 | }
130 |
131 | if (previous != null) {
132 | entryRemoved(false, key, previous, value);
133 | }
134 |
135 | trimToSize(maxSize);
136 | return previous;
137 | }
138 |
139 | /**
140 | * Remove the eldest entries until the total of remaining entries is at or
141 | * below the requested size.
142 | *
143 | * @param maxSize the maximum size of the cache before returning. May be -1
144 | * to evict even 0-sized elements.
145 | */
146 | public void trimToSize(int maxSize) {
147 | while (true) {
148 | K key;
149 | V value;
150 | synchronized (this) {
151 | if (size < 0 || (map.isEmpty() && size != 0)) {
152 | throw new IllegalStateException(getClass().getName()
153 | + ".sizeOf() is reporting inconsistent results!");
154 | }
155 |
156 | if (size <= maxSize || map.isEmpty()) {
157 | break;
158 | }
159 |
160 | Map.Entry toEvict = map.entrySet().iterator().next();
161 | key = toEvict.getKey();
162 | value = toEvict.getValue();
163 | map.remove(key);
164 | size -= safeSizeOf(key, value);
165 | evictionCount++;
166 | }
167 |
168 | entryRemoved(true, key, value, null);
169 | }
170 | }
171 |
172 | /**
173 | * Removes the entry for {@code key} if it exists.
174 | *
175 | * @return the previous value mapped by {@code key}.
176 | */
177 | public final V remove(K key) {
178 | if (key == null) {
179 | throw new NullPointerException("key == null");
180 | }
181 |
182 | V previous;
183 | synchronized (this) {
184 | previous = map.remove(key);
185 | if (previous != null) {
186 | size -= safeSizeOf(key, previous);
187 | }
188 | }
189 |
190 | if (previous != null) {
191 | entryRemoved(false, key, previous, null);
192 | }
193 |
194 | return previous;
195 | }
196 |
197 | /**
198 | * Called for entries that have been evicted or removed. This method is
199 | * invoked when a value is evicted to make space, removed by a call to
200 | * {@link #remove}, or replaced by a call to {@link #put}. The default
201 | * implementation does nothing.
202 | *
203 | *
The method is called without synchronization: other threads may
204 | * access the cache while this method is executing.
205 | *
206 | * @param evicted true if the entry is being removed to make space, false
207 | * if the removal was caused by a {@link #put} or {@link #remove}.
208 | * @param newValue the new value for {@code key}, if it exists. If non-null,
209 | * this removal was caused by a {@link #put}. Otherwise it was caused by
210 | * an eviction or a {@link #remove}.
211 | */
212 | protected void entryRemoved(boolean evicted, K key, V oldValue, V newValue) {}
213 |
214 | /**
215 | * Called after a cache miss to compute a value for the corresponding key.
216 | * Returns the computed value or null if no value can be computed. The
217 | * default implementation returns null.
218 | *
219 | *
The method is called without synchronization: other threads may
220 | * access the cache while this method is executing.
221 | *
222 | *
If a value for {@code key} exists in the cache when this method
223 | * returns, the created value will be released with {@link #entryRemoved}
224 | * and discarded. This can occur when multiple threads request the same key
225 | * at the same time (causing multiple values to be created), or when one
226 | * thread calls {@link #put} while another is creating a value for the same
227 | * key.
228 | */
229 | protected V create(K key) {
230 | return null;
231 | }
232 |
233 | private int safeSizeOf(K key, V value) {
234 | int result = sizeOf(key, value);
235 | if (result < 0) {
236 | throw new IllegalStateException("Negative size: " + key + "=" + value);
237 | }
238 | return result;
239 | }
240 |
241 | /**
242 | * Returns the size of the entry for {@code key} and {@code value} in
243 | * user-defined units. The default implementation returns 1 so that size
244 | * is the number of entries and max size is the maximum number of entries.
245 | *
246 | *
An entry's size must not change while it is in the cache.
247 | */
248 | protected int sizeOf(K key, V value) {
249 | return 1;
250 | }
251 |
252 | /**
253 | * Clear the cache, calling {@link #entryRemoved} on each removed entry.
254 | */
255 | public final void evictAll() {
256 | trimToSize(-1); // -1 will evict 0-sized elements
257 | }
258 |
259 | /**
260 | * For caches that do not override {@link #sizeOf}, this returns the number
261 | * of entries in the cache. For all other caches, this returns the sum of
262 | * the sizes of the entries in this cache.
263 | */
264 | public synchronized final int size() {
265 | return size;
266 | }
267 |
268 | /**
269 | * For caches that do not override {@link #sizeOf}, this returns the maximum
270 | * number of entries in the cache. For all other caches, this returns the
271 | * maximum sum of the sizes of the entries in this cache.
272 | */
273 | public synchronized final int maxSize() {
274 | return maxSize;
275 | }
276 |
277 | /**
278 | * Returns the number of times {@link #get} returned a value that was
279 | * already present in the cache.
280 | */
281 | public synchronized final int hitCount() {
282 | return hitCount;
283 | }
284 |
285 | /**
286 | * Returns the number of times {@link #get} returned null or required a new
287 | * value to be created.
288 | */
289 | public synchronized final int missCount() {
290 | return missCount;
291 | }
292 |
293 | /**
294 | * Returns the number of times {@link #create(Object)} returned a value.
295 | */
296 | public synchronized final int createCount() {
297 | return createCount;
298 | }
299 |
300 | /**
301 | * Returns the number of times {@link #put} was called.
302 | */
303 | public synchronized final int putCount() {
304 | return putCount;
305 | }
306 |
307 | /**
308 | * Returns the number of values that have been evicted.
309 | */
310 | public synchronized final int evictionCount() {
311 | return evictionCount;
312 | }
313 |
314 | /**
315 | * Returns a copy of the current contents of the cache, ordered from least
316 | * recently accessed to most recently accessed.
317 | */
318 | public synchronized final Map snapshot() {
319 | return new LinkedHashMap(map);
320 | }
321 |
322 | @Override public synchronized final String toString() {
323 | int accesses = hitCount + missCount;
324 | int hitPercent = accesses != 0 ? (100 * hitCount / accesses) : 0;
325 | return String.format("LruCache[maxSize=%d,hits=%d,misses=%d,hitRate=%d%%]",
326 | maxSize, hitCount, missCount, hitPercent);
327 | }
328 | }
329 |
--------------------------------------------------------------------------------
/LazyCache/src/main/java/com/robin/lazy/cache/process/CacheDataProcess.java:
--------------------------------------------------------------------------------
1 | /*
2 | * 文 件 名: CacheDataProcess.java
3 | * 版 权: Co., Ltd. Copyright YYYY-YYYY, All rights reserved
4 | * 描 述: <描述>
5 | * 修 改 人: jiangyufeng
6 | * 修改时间: 2015年12月14日
7 | * 跟踪单号: <跟踪单号>
8 | * 修改单号: <修改单号>
9 | * 修改内容: <修改内容>
10 | */
11 |
12 | package com.robin.lazy.cache.process;
13 | /**
14 | * 缓存数据加工接口
15 | *
16 | * @author jiangyufeng
17 | * @version [版本号, 2015年12月14日]
18 | * @param 范型
19 | * @see [相关类/方法]
20 | * @since [产品/模块版本]
21 | */
22 | public interface CacheDataProcess {
23 |
24 | /**
25 | * 加工缓存的数据
26 | * @param data
27 | * @return 返回加工处理后的数据
28 | * V
29 | * @see [类、类#方法、类#成员]
30 | */
31 | V process(V data);
32 | }
33 |
34 |
--------------------------------------------------------------------------------
/LazyCache/src/main/java/com/robin/lazy/cache/process/DefaultCacheDataProcess.java:
--------------------------------------------------------------------------------
1 | /*
2 | * 文 件 名: DefaultCacheDataProcess.java
3 | * 版 权: Co., Ltd. Copyright YYYY-YYYY, All rights reserved
4 | * 描 述: <描述>
5 | * 修 改 人: jiangyufeng
6 | * 修改时间: 2016年3月17日
7 | * 跟踪单号: <跟踪单号>
8 | * 修改单号: <修改单号>
9 | * 修改内容: <修改内容>
10 | */
11 |
12 | package com.robin.lazy.cache.process;
13 | /**
14 | * 默认的缓存数据加工处理器
15 | *
16 | * @author jiangyufeng
17 | * @version [版本号, 2016年3月17日]
18 | * @param
19 | * @see [相关类/方法]
20 | * @since [产品/模块版本]
21 | */
22 | public class DefaultCacheDataProcess implements CacheDataProcess{
23 |
24 | @Override
25 | public V process(V data) {
26 | return data;
27 | }
28 |
29 | }
30 |
31 |
--------------------------------------------------------------------------------
/LazyCache/src/main/java/com/robin/lazy/cache/util/DiskCacheUtils.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright 2011-2014 Sergey Tarasevich
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 | package com.robin.lazy.cache.util;
17 |
18 | import android.content.Context;
19 |
20 | import com.robin.lazy.cache.disk.DiskCache;
21 | import com.robin.lazy.cache.disk.impl.BaseDiskCache;
22 | import com.robin.lazy.cache.disk.impl.ext.LruDiskCache;
23 | import com.robin.lazy.cache.disk.naming.FileNameGenerator;
24 | import com.robin.lazy.cache.disk.naming.HashCodeFileNameGenerator;
25 | import com.robin.lazy.cache.disk.write.StringWriteInDisk;
26 | import com.robin.lazy.cache.util.log.CacheLog;
27 | import com.robin.lazy.util.StorageUtils;
28 |
29 | import java.io.File;
30 | import java.io.IOException;
31 |
32 | /**
33 | * 磁盘缓存相关工具
34 | *
35 | * @author jiangyufeng
36 | * @version [版本号, 2015年12月15日]
37 | * @see [相关类/方法]
38 | * @since [产品/模块版本]
39 | */
40 | public final class DiskCacheUtils {
41 |
42 | private final static String LOG_TAG=DiskCacheUtils.class.getSimpleName();
43 |
44 | private DiskCacheUtils() {
45 | }
46 |
47 | /**
48 | * 创建一个文件名称转换器
49 | * @return
50 | */
51 | public static FileNameGenerator createFileNameGenerator() {
52 | return new HashCodeFileNameGenerator();
53 | }
54 |
55 | /**
56 | * Creates default implementation of {@link DiskCache} depends on incoming
57 | * parameters
58 | * @param diskCacheSize 可使用磁盘最大大小(单位byte,字节)
59 | */
60 | public static DiskCache createDiskCache(Context context,
61 | FileNameGenerator diskCacheFileNameGenerator, long diskCacheSize,
62 | int diskCacheFileCount) {
63 | File reserveCacheDir = createReserveDiskCacheDir(context);
64 | if (diskCacheSize > 0 || diskCacheFileCount > 0) {
65 | File individualCacheDir = StorageUtils
66 | .getIndividualCacheDirectory(context);
67 | try {
68 | return new LruDiskCache(individualCacheDir, reserveCacheDir,
69 | diskCacheFileNameGenerator, diskCacheSize,
70 | diskCacheFileCount);
71 | } catch (IOException e) {
72 | CacheLog.e(LOG_TAG, "获取LruDiskCache错误",e);
73 | // continue and create unlimited cache
74 | }
75 | }
76 | File cacheDir = StorageUtils.getCacheDirectory(context);
77 | return new BaseDiskCache(cacheDir, reserveCacheDir,
78 | diskCacheFileNameGenerator);
79 | }
80 |
81 | /**
82 | * Creates reserve disk cache folder which will be used if primary disk
83 | * cache folder becomes unavailable
84 | */
85 | private static File createReserveDiskCacheDir(Context context) {
86 | File cacheDir = StorageUtils.getCacheDirectory(context, false);
87 | File individualDir = new File(cacheDir, "lazy-cache");
88 | if (individualDir.exists() || individualDir.mkdir()) {
89 | cacheDir = individualDir;
90 | }
91 | return cacheDir;
92 | }
93 |
94 | /**
95 | * Returns {@link File} of cached key or null if key was not cached
96 | * in disk cache
97 | */
98 | public static File findInCache(String key, DiskCache diskCache) {
99 | File file = diskCache.getFile(key);
100 | return file != null && file.exists() ? file : null;
101 | }
102 |
103 | /**
104 | * Removed cached key file from disk cache (if key was cached in disk cache
105 | * before)
106 | *
107 | * @return true - if cached key file existed and was deleted;
108 | * false - otherwise.
109 | */
110 | public static boolean removeFromCache(String key, DiskCache diskCache) {
111 | File file = diskCache.getFile(key);
112 | return file != null && file.exists() && file.delete();
113 | }
114 | }
115 |
--------------------------------------------------------------------------------
/LazyCache/src/main/java/com/robin/lazy/cache/util/MemoryCacheUtils.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright 2011-2014 Sergey Tarasevich
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 | package com.robin.lazy.cache.util;
17 |
18 | import android.annotation.TargetApi;
19 | import android.app.ActivityManager;
20 | import android.content.Context;
21 | import android.content.pm.ApplicationInfo;
22 | import android.os.Build;
23 |
24 | import com.robin.lazy.cache.memory.EntryRemovedProcess;
25 | import com.robin.lazy.cache.memory.MemoryCache;
26 | import com.robin.lazy.cache.memory.SizeOfCacheCalculator;
27 | import com.robin.lazy.cache.memory.impl.EntryRemovedProcessMemoryCache;
28 | import com.robin.lazy.cache.memory.impl.LruMemoryCache;
29 | import com.robin.lazy.cache.memory.impl.SizeOfMemoryCache;
30 |
31 | import java.util.ArrayList;
32 | import java.util.List;
33 |
34 | /**
35 | * 内存缓存工具
36 | *
37 | * @author jiangyufeng
38 | * @version [版本号, 2015年12月15日]
39 | * @see [相关类/方法]
40 | * @since [产品/模块版本]
41 | */
42 | public final class MemoryCacheUtils {
43 |
44 | private MemoryCacheUtils() {
45 | }
46 |
47 | /**
48 | * 创建一个有固定大小的内存缓存操作工具
49 | *
50 | * @param memoryCacheSize
51 | * 内存缓存的最大限度(单位兆MB)
52 | * @param calculator
53 | * 单个内存缓存占用内存大小计算器
54 | * @return SizeOfMemoryCache
55 | * @throws
56 | * @see [类、类#方法、类#成员]
57 | */
58 | public static SizeOfMemoryCache createMemoryCache(
59 | int memoryCacheSize, SizeOfCacheCalculator> calculator) {
60 | if (memoryCacheSize <= 0) {
61 | new NullPointerException("memoryCacheSize小于0");
62 | }
63 | return new SizeOfMemoryCache(memoryCacheSize, calculator);
64 | }
65 |
66 | /**
67 | * 创建一个有固定大小的内存缓存操作工具
68 | *
69 | * @param context
70 | * @param ratio
71 | * 占当前可用内存的比例(最大为1)
72 | * @param calculator
73 | * 单个内存缓存占用内存大小计算器
74 | * @return SizeOfMemoryCache
75 | * @throws
76 | * @see [类、类#方法、类#成员]
77 | */
78 | public static SizeOfMemoryCache createMemoryCache(Context context,
79 | float ratio, SizeOfCacheCalculator> calculator) {
80 | int memoryCacheSize = 0;
81 | ActivityManager am = (ActivityManager) context
82 | .getSystemService(Context.ACTIVITY_SERVICE);
83 | int memoryClass = am.getMemoryClass();// 获取可用内存大小(单位MB)
84 | if (hasHoneycomb() && isLargeHeap(context)) {
85 | memoryClass = getLargeMemoryClass(am);
86 | }
87 | memoryCacheSize = (int) (1024 * 1024 * memoryClass * ratio);
88 | return new SizeOfMemoryCache(memoryCacheSize, calculator);
89 | }
90 |
91 | /**
92 | * 创建能够自定义处理被回收的数据的内存缓存类
93 | *
94 | * @param maxSize 可以储存的缓存数据数量最大上限(单位未知)
95 | * @param entryRemovedProcess 删除处理者
96 | * @return LruMemoryCache
97 | * @throws
98 | * @see [类、类#方法、类#成员]
99 | */
100 | public static EntryRemovedProcessMemoryCache createLruMemoryCache(int maxSize,
101 | EntryRemovedProcess> entryRemovedProcess) {
102 | return new EntryRemovedProcessMemoryCache(maxSize,entryRemovedProcess);
103 | }
104 |
105 | /**
106 | * 创建默认的Lru内存缓存类
107 | *
108 | * @param maxSize
109 | * 可以储存的缓存数据数量最大上限(单位未知)
110 | * @return LruMemoryCache
111 | * @throws
112 | * @see [类、类#方法、类#成员]
113 | */
114 | public static LruMemoryCache createLruMemoryCache(int maxSize) {
115 | return new LruMemoryCache(maxSize);
116 | }
117 |
118 | private static boolean hasHoneycomb() {
119 | return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB;
120 | }
121 |
122 | @TargetApi(Build.VERSION_CODES.HONEYCOMB)
123 | private static boolean isLargeHeap(Context context) {
124 | return (context.getApplicationInfo().flags & ApplicationInfo.FLAG_LARGE_HEAP) != 0;
125 | }
126 |
127 | /**
128 | * 获取可用内存大小(单位MB)
129 | *
130 | * @param am
131 | * @return int
132 | * @throws
133 | * @see [类、类#方法、类#成员]
134 | */
135 | @TargetApi(Build.VERSION_CODES.HONEYCOMB)
136 | private static int getLargeMemoryClass(ActivityManager am) {
137 | return am.getLargeMemoryClass();
138 | }
139 |
140 |
141 | /**
142 | * 根据key查找对应的数据
143 | * @param key
144 | * @param memoryCache
145 | * @param
146 | * @return
147 | */
148 | public static List findCachedForkey(String key,
149 | MemoryCache memoryCache) {
150 | List values = new ArrayList();
151 | for (String k : memoryCache.keys()) {
152 | if (k.startsWith(key)) {
153 | V value = memoryCache.get(k);
154 | values.add(value);
155 | }
156 | }
157 | return values;
158 | }
159 |
160 | /**
161 | * 根据key查找对应的数据
162 | * @param key
163 | * @param memoryCache
164 | * @return
165 | */
166 | public static List findCacheKeysForkey(String key,
167 | MemoryCache memoryCache) {
168 | List values = new ArrayList();
169 | for (String k : memoryCache.keys()) {
170 | if (k.startsWith(key)) {
171 | values.add(k);
172 | }
173 | }
174 | return values;
175 | }
176 |
177 | /**
178 | * 根据key删除对应的数据
179 | * @param key
180 | * @param memoryCache
181 | */
182 | public static void removeFromCache(String key, MemoryCache memoryCache) {
183 | List keysToRemove = new ArrayList();
184 | for (String k : memoryCache.keys()) {
185 | if (k.startsWith(key)) {
186 | keysToRemove.add(k);
187 | }
188 | }
189 | for (String keyToRemove : keysToRemove) {
190 | memoryCache.remove(keyToRemove);
191 | }
192 | }
193 | }
194 |
--------------------------------------------------------------------------------
/LazyCache/src/main/java/com/robin/lazy/cache/util/log/AndroidLog.java:
--------------------------------------------------------------------------------
1 | package com.robin.lazy.cache.util.log;
2 |
3 | import android.util.Log;
4 |
5 | /**
6 | * @desc: android官方日志打印的实现
7 | * @projectName:LazyNetForAndroid
8 | * @className: AndroidLog
9 | * @author: jiangyufeng
10 | * @createTime: 2018/10/17 下午2:39
11 | */
12 | public class AndroidLog implements ILog {
13 | /**
14 | * VERBOSE类型日志
15 | */
16 | private final static int VERBOSE = 1;
17 |
18 | /**
19 | * debug类型日志
20 | */
21 | private final static int DEBUG = 2;
22 |
23 | /**
24 | * info类型日志
25 | */
26 | private final static int INFO = 3;
27 |
28 | /**
29 | * warn类型日志
30 | */
31 | private final static int WARN = 4;
32 |
33 | /**
34 | * error类型日志
35 | */
36 | private final static int ERROR = 5;
37 |
38 | /**
39 | * ASSERT类型日志
40 | */
41 | private final static int ASSERT = 6;
42 | /**
43 | * 一次日志最大打印长度
44 | */
45 | private final static int MAX_LENGTH = 3600;
46 |
47 | @Override
48 | public void d(String tag, String message) {
49 | log(DEBUG,tag,message,null);
50 | }
51 |
52 | @Override
53 | public void d(String tag, String message, Throwable throwable) {
54 | log(DEBUG,tag,message,throwable);
55 | }
56 |
57 | @Override
58 | public void e(String tag, String message) {
59 | log(ERROR,tag,message,null);
60 | }
61 |
62 | @Override
63 | public void e(String tag, String message, Throwable throwable) {
64 | log(ERROR,tag,message,throwable);
65 | }
66 |
67 | @Override
68 | public void w(String tag, String message) {
69 | log(WARN,tag,message,null);
70 | }
71 |
72 | @Override
73 | public void w(String tag, String message, Throwable throwable) {
74 | log(WARN,tag,message,throwable);
75 | }
76 |
77 | @Override
78 | public void i(String tag, String message) {
79 | log(INFO,tag,message,null);
80 | }
81 |
82 | @Override
83 | public void i(String tag, String message, Throwable throwable) {
84 | log(INFO,tag,message,throwable);
85 | }
86 |
87 | @Override
88 | public void v(String tag, String message) {
89 | log(VERBOSE,tag,message,null);
90 | }
91 |
92 | @Override
93 | public void v(String tag, String message, Throwable throwable) {
94 | log(VERBOSE,tag,message,throwable);
95 | }
96 |
97 | @Override
98 | public void wtf(String tag, String message) {
99 | log(ASSERT,tag,message,null);
100 | }
101 |
102 | @Override
103 | public void wtf(String tag, String message, Throwable throwable) {
104 | log(ASSERT,tag,message,throwable);
105 | }
106 |
107 | /**
108 | * 日志输入方法
109 | *
110 | * @param logLevel 日志ji'b级别
111 | * @param tag
112 | * @param message
113 | * @param throwable
114 | */
115 | private void log(int logLevel, String tag, String message,
116 | Throwable throwable) {
117 | while (message.length() > MAX_LENGTH) {
118 | print(logLevel,tag,message.substring(0, MAX_LENGTH),throwable);
119 | message = message.substring(MAX_LENGTH);
120 | }
121 | print(logLevel,tag,message,throwable);
122 | }
123 |
124 | /**
125 | * 最终打印方法
126 | *
127 | * @param logLevel
128 | * @param tag
129 | * @param message
130 | * @param throwable
131 | */
132 | private void print(int logLevel, String tag, String message,
133 | Throwable throwable) {
134 | switch (logLevel) {
135 | case VERBOSE:
136 | if(throwable==null){
137 | Log.v(tag, message);
138 | }else{
139 | Log.v(tag, message,throwable);
140 | }
141 | break;
142 | case DEBUG:
143 | if(throwable==null){
144 | Log.d(tag, message);
145 | }else {
146 | Log.d(tag, message,throwable);
147 | }
148 | break;
149 | case INFO:
150 | if(throwable==null){
151 | Log.i(tag, message);
152 | }else {
153 | Log.i(tag, message,throwable);
154 | }
155 | break;
156 | case WARN:
157 | if(throwable==null){
158 | Log.w(tag, message);
159 | }else{
160 | Log.w(tag, message,throwable);
161 | }
162 | break;
163 | case ERROR:
164 | if(throwable==null){
165 | Log.e(tag, message);
166 | }else {
167 | Log.e(tag, message,throwable);
168 | }
169 | break;
170 | case ASSERT:
171 | if(throwable==null){
172 | Log.wtf(tag, message);
173 | }else{
174 | Log.wtf(tag, message,throwable);
175 | }
176 | break;
177 | }
178 | }
179 | }
180 |
--------------------------------------------------------------------------------
/LazyCache/src/main/java/com/robin/lazy/cache/util/log/CacheLog.java:
--------------------------------------------------------------------------------
1 | package com.robin.lazy.cache.util.log;
2 |
3 | import android.text.TextUtils;
4 |
5 | import org.json.JSONArray;
6 | import org.json.JSONException;
7 | import org.json.JSONObject;
8 |
9 | /**
10 | * @desc: 日志打印的被委托者
11 | * @projectName:LazyNetForAndroid
12 | * @className: CacheLog
13 | * @author: jiangyufeng
14 | * @createTime: 2018/10/12 下午5:36
15 | */
16 | public final class CacheLog {
17 | /**
18 | * It is used for json pretty print
19 | */
20 | private static final int JSON_INDENT = 4;
21 |
22 | /***
23 | * 是否是debug
24 | */
25 | private static boolean isDebug=true;
26 | /***
27 | * 委托者
28 | */
29 | private static ILog delegate = new AndroidLog();
30 |
31 | /**
32 | * 重置委托者
33 | * @param delegate
34 | */
35 | public static void resetDelegate(ILog delegate) {
36 | CacheLog.delegate = delegate;
37 | }
38 |
39 | /**
40 | * 设置是否debug模式,debug才会输出日志,默认为true
41 | * @param isDebug
42 | */
43 | public static void setIsDebug(boolean isDebug) {
44 | CacheLog.isDebug = isDebug;
45 | }
46 |
47 | public static void d(String tag, String message) {
48 | if (!isDebug)return;
49 | if (delegate == null) throw new NullPointerException("delegate may not be null");
50 | delegate.d(tag,message);
51 | }
52 |
53 | public static void d(String tag, String message, Throwable throwable) {
54 | if (!isDebug)return;
55 | if (delegate == null) throw new NullPointerException("delegate may not be null");
56 | delegate.d(tag,message,throwable);
57 | }
58 |
59 | public static void e(String tag, String message) {
60 | if (!isDebug)return;
61 | if (delegate == null) throw new NullPointerException("delegate may not be null");
62 | delegate.e(tag,message);
63 | }
64 |
65 | public static void e(String tag, String message, Throwable throwable) {
66 | if (!isDebug)return;
67 | if (delegate == null) throw new NullPointerException("delegate may not be null");
68 | delegate.e(tag,message,throwable);
69 | }
70 |
71 | public static void w(String tag, String message) {
72 | if (!isDebug)return;
73 | if (delegate == null) throw new NullPointerException("delegate may not be null");
74 | delegate.w(tag,message);
75 | }
76 |
77 | public static void w(String tag, String message, Throwable throwable) {
78 | if (!isDebug)return;
79 | if (delegate == null) throw new NullPointerException("delegate may not be null");
80 | delegate.w(tag,message,throwable);
81 | }
82 |
83 | public static void i(String tag, String message) {
84 | if (!isDebug)return;
85 | if (delegate == null) throw new NullPointerException("delegate may not be null");
86 | delegate.i(tag,message);
87 | }
88 |
89 | public static void i(String tag, String message, Throwable throwable) {
90 | if (!isDebug)return;
91 | if (delegate == null) throw new NullPointerException("delegate may not be null");
92 | delegate.i(tag,message,throwable);
93 | }
94 |
95 | public static void v(String tag, String message) {
96 | if (!isDebug)return;
97 | if (delegate == null) throw new NullPointerException("delegate may not be null");
98 | delegate.v(tag,message);
99 | }
100 |
101 | public static void v(String tag, String message, Throwable throwable) {
102 | if (!isDebug)return;
103 | if (delegate == null) throw new NullPointerException("delegate may not be null");
104 | delegate.v(tag,message,throwable);
105 | }
106 |
107 | public static void wtf(String tag, String message) {
108 | if (!isDebug)return;
109 | if (delegate == null) throw new NullPointerException("delegate may not be null");
110 | delegate.wtf(tag,message);
111 | }
112 |
113 | public static void wtf(String tag, String message, Throwable throwable) {
114 | if (!isDebug)return;
115 | if (delegate == null) throw new NullPointerException("delegate may not be null");
116 | delegate.wtf(tag, message, throwable);
117 | }
118 |
119 | /**
120 | * 打印json数据日志
121 | * @param tag
122 | * @param json
123 | */
124 | public static void json(String tag, String json) {
125 | if (!isDebug)return;
126 | if (delegate == null) throw new NullPointerException("delegate may not be null");
127 | if (TextUtils.isEmpty(json)) {
128 | d(tag,"Empty/Null json content");
129 | return;
130 | }
131 | try {
132 | if (json.startsWith("{")) {
133 | JSONObject jsonObject = new JSONObject(json);
134 | String message = jsonObject.toString(JSON_INDENT);
135 | d(tag,message);
136 | return;
137 | }
138 | if (json.startsWith("[")) {
139 | JSONArray jsonArray = new JSONArray(json);
140 | String message = jsonArray.toString(JSON_INDENT);
141 | d(tag,message);
142 | }
143 | } catch (JSONException e) {
144 | e(tag,e.getCause().getMessage() + "\n" + json);
145 | }
146 | }
147 |
148 | }
149 |
--------------------------------------------------------------------------------
/LazyCache/src/main/java/com/robin/lazy/cache/util/log/ILog.java:
--------------------------------------------------------------------------------
1 | package com.robin.lazy.cache.util.log;
2 |
3 | /**
4 | * @desc: 日志输出接口
5 | * @projectName:LazyNetForAndroid
6 | * @className: ILog
7 | * @author: jiangyufeng
8 | * @createTime: 2018/10/12 下午5:35
9 | */
10 | public interface ILog {
11 |
12 | void d(String tag, String message);
13 |
14 | void d(String tag, String message, Throwable throwable);
15 |
16 | void e(String tag, String message);
17 |
18 | void e(String tag, String message, Throwable throwable);
19 |
20 | void w(String tag, String message);
21 |
22 | void w(String tag, String message, Throwable throwable);
23 |
24 | void i(String tag, String message);
25 |
26 | void i(String tag, String message, Throwable throwable);
27 |
28 | void v(String tag, String message);
29 |
30 | void v(String tag, String message, Throwable throwable);
31 |
32 | void wtf(String tag, String message);
33 |
34 | void wtf(String tag, String message, Throwable throwable);
35 | }
36 |
--------------------------------------------------------------------------------
/LazyCache/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | CacheLibrary
3 |
4 |
--------------------------------------------------------------------------------
/LazyCache/src/test/java/com/robin/lazy/cache/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.robin.lazy.cache;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * To work on unit tests, switch the Test Artifact in the Build Variants view.
9 | */
10 | public class ExampleUnitTest {
11 | @Test
12 | public void addition_isCorrect() throws Exception {
13 | assertEquals(4, 2 + 2);
14 | }
15 | }
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | LazyCacheForAndroid
2 | -----------------------------------
3 | # 项目介绍
4 | ### 项目地址
5 | * [LazyCacheForAndroid](https://github.com/Robin-jiangyufeng/LazyCacheForAndroid)
6 | ###版本
7 | * [](https://jitpack.io/#Robin-jiangyufeng/LazyCacheForAndroid)
8 | ### 介绍:
9 | * 这是一个android上的数据缓存框架,具有缓存和加载数据速度快,缓存数据类型全,能够实现任意缓存时间等优点
10 |
11 | ### 功能:
12 | * 1.目前已经实现的可以缓存String,Serialiable,Bitmap,InputStream,Bytes等类型数据,当然你也可以自己进行扩展实现自己需要缓存的类型数据
13 | * 2.支持多级缓存,目前已实现lru算法的磁盘缓存和lru算法的内存缓存,根据优先级进行缓存,当然你也可以扩展实现多级缓存,只要实现Cache接口,设置缓存优先级即可
14 | * 3.可以设置全局数据缓存的时间,也可以单独设置一条数据缓存的时间
15 | * 4.有更多功能
16 |
17 | ### 使用场景:
18 | * 1.替换SharePreference当做配置文件
19 | * 2.缓存网络数据,比如json,图片数据等
20 | * 3.自己想...
21 |
22 | # 使用方法
23 | ### 库引入方式
24 | * Gradle:
25 | ````
26 | repositories {
27 | jcenter()
28 | maven {url 'https://dl.bintray.com/jiangyufeng/maven/'}
29 | }
30 |
31 | compile 'com.github.Robin-jiangyufeng:LazyCacheForAndroid:1.0.9'
32 | ````
33 | * Maven:
34 | ````
35 |
36 | com.robin.lazy.cache
37 | LazyCache
38 | 1.0.7
39 | pom
40 |
41 | ````
42 |
43 | ### 所需权限
44 | ```java
45 |
46 |
47 |
48 | ```
49 |
50 | ### 初始化
51 | * 想要直接使用CacheLoaderManager进行数据储存的话,请先进行初始化,初始化方式如下:
52 | ```java
53 | /***
54 | * 初始化缓存的一些配置
55 | *
56 | * @param diskCacheFileNameGenerator
57 | * @param diskCacheSize 磁盘缓存大小
58 | * @param diskCacheFileCount 磁盘缓存文件的最大限度
59 | * @param maxMemorySize 内存缓存的大小
60 | * @return CacheLoaderConfiguration
61 | * @throws
62 | * @see [类、类#方法、类#成员]
63 | */
64 | CacheLoaderManager.getInstance().init(Context context,FileNameGenerator diskCacheFileNameGenerator, long diskCacheSize,
65 | int diskCacheFileCount, int maxMemorySize);
66 | ```
67 | ### 缓存数据
68 | * 以下代码只列举了储存String类型的数据,其它数据类型储存类似,具体请阅读 CacheLoaderManager.java
69 | ```java
70 | /**
71 | * save String到缓存
72 | * @param key
73 | * @param value 要缓存的值
74 | * @param maxLimitTime 缓存期限(单位分钟)
75 | * @return 是否保存成功
76 | * boolean
77 | * @throws
78 | * @see [类、类#方法、类#成员]
79 | */
80 | CacheLoaderManager.getInstance().saveString(String key,String value,long maxLimitTime);
81 | ```
82 | ### 加载缓存数据
83 | * 以下代码只列举了加载String类型的数据方法,其它数据加载类似,具体请阅读 CacheLoaderManager.java
84 | ```java
85 | /**
86 | * 加载String
87 | * @param key
88 | * @return 等到缓存数据
89 | * String
90 | * @throws
91 | * @see [类、类#方法、类#成员]
92 | */
93 | CacheLoaderManager.getInstance().loadString(String key);
94 | ```
95 | ### 其它
96 | * 上面介绍的是很小的一部分已经实现的功能,其中有还有很多功能可以高度定制,扩展性很强,更多功能待你发现;
97 |
98 | # 关于作者Robin
99 | * 屌丝程序员
100 | * GitHub: [Robin-jiangyufeng](https://github.com/Robin-jiangyufeng)
101 | * QQ:429257411
102 | * 交流QQ群 236395044
103 |
--------------------------------------------------------------------------------
/Sample/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/Sample/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 33
5 |
6 | defaultConfig {
7 | applicationId "com.robin.lazy.sample"
8 | minSdkVersion 14
9 | targetSdkVersion 33
10 | versionCode 11
11 | versionName "1.1"
12 | multiDexEnabled true
13 | //ADD THIS LINE:
14 | testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
15 | }
16 |
17 | buildTypes {
18 | release {
19 | minifyEnabled false
20 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
21 | }
22 | }
23 |
24 | lintOptions {
25 | abortOnError false
26 | }
27 |
28 | dexOptions {
29 | incremental true
30 | javaMaxHeapSize "4g"
31 | }
32 | }
33 |
34 | dependencies {
35 | implementation fileTree(include: ['*.jar'], dir: 'libs')
36 | implementation 'androidx.appcompat:appcompat:1.3.0'
37 | implementation 'com.google.android.material:material:1.4.0'
38 | testImplementation 'junit:junit:4.12'
39 | implementation 'androidx.multidex:multidex:2.0.1'
40 | //ADD THESE LINES:
41 | //noinspection GradleCompatible
42 | testImplementation 'androidx.test.ext:junit:1.1.1'
43 | testImplementation 'androidx.test:rules:1.1.1'
44 | testImplementation 'androidx.annotation:annotation:1.0.0'
45 | implementation 'androidx.test.espresso:espresso-core:3.4.0'
46 | implementation 'in.srain.cube:ultra-ptr:1.0.11'
47 | implementation project(':LazyCache')
48 | }
49 |
--------------------------------------------------------------------------------
/Sample/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Volumes/Work/androidIDE/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/Sample/src/androidTest/java/com/robin/lazy/sample/ApplicationTest.java:
--------------------------------------------------------------------------------
1 | package com.robin.lazy.sample;
2 |
3 | import android.test.ApplicationTestCase;
4 |
5 | /**
6 | * Testing Fundamentals
7 | */
8 | public class ApplicationTest extends ApplicationTestCase {
9 | public ApplicationTest() {
10 | super(App.class);
11 | }
12 | }
--------------------------------------------------------------------------------
/Sample/src/androidTest/java/com/robin/lazy/sample/MainActivityTest.java:
--------------------------------------------------------------------------------
1 | package com.robin.lazy.sample;
2 |
3 | import androidx.test.espresso.Espresso;
4 | import androidx.test.espresso.action.ViewActions;
5 | import androidx.test.espresso.core.deps.guava.util.concurrent.Uninterruptibles;
6 | import androidx.test.espresso.matcher.ViewMatchers;
7 | import androidx.test.rule.ActivityTestRule;
8 | import androidx.test.ext.junit.runners.AndroidJUnit4;
9 | import android.test.suitebuilder.annotation.LargeTest;
10 |
11 | import org.junit.Before;
12 | import org.junit.Rule;
13 | import org.junit.Test;
14 | import org.junit.runner.RunWith;
15 |
16 | import java.util.concurrent.TimeUnit;
17 |
18 | /**
19 | * 文 件 名: MainActivityTest.java
20 | * 版 权: Technologies Co., Ltd. Copyright YYYY-YYYY, All rights reserved
21 | * 描 述: <描述>
22 | * 修 改 人: 江钰锋 00501
23 | * 修改时间: 16/6/6
24 | * 跟踪单号: <跟踪单号>
25 | * 修改单号: <修改单号>
26 | * 修改内容: <修改内容>
27 | */
28 | @RunWith(AndroidJUnit4.class)
29 | @LargeTest
30 | public class MainActivityTest {
31 |
32 | @Rule
33 | public ActivityTestRule activityTestRule = new ActivityTestRule(MainActivity.class);
34 |
35 | @Before
36 | public void setUp() throws Exception {
37 |
38 | }
39 |
40 | @Test
41 | public void testOnCreate() throws Exception {
42 | Espresso.onView(ViewMatchers.withId(R.id.buttonSave)).perform(ViewActions.click());
43 | Uninterruptibles.sleepUninterruptibly(5, TimeUnit.SECONDS);
44 | Espresso.onView(ViewMatchers.withId(R.id.buttonLoad)).perform(ViewActions.click());
45 | }
46 |
47 | }
--------------------------------------------------------------------------------
/Sample/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
9 |
16 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/Sample/src/main/assets/province.json:
--------------------------------------------------------------------------------
1 | {
2 | "province": [
3 | {
4 | "areaid": "100000",
5 | "areaname": "全国"
6 | },
7 | {
8 | "areaid": "110000",
9 | "areaname": "北京市"
10 | },
11 | {
12 | "areaid": "120000",
13 | "areaname": "天津市"
14 | },
15 | {
16 | "areaid": "130000",
17 | "areaname": "河北省"
18 | },
19 | {
20 | "areaid": "140000",
21 | "areaname": "山西省"
22 | },
23 | {
24 | "areaid": "150000",
25 | "areaname": "内蒙古自治区"
26 | },
27 | {
28 | "areaid": "210000",
29 | "areaname": "辽宁省"
30 | },
31 | {
32 | "areaid": "220000",
33 | "areaname": "吉林省"
34 | },
35 | {
36 | "areaid": "230000",
37 | "areaname": "黑龙江省"
38 | },
39 | {
40 | "areaid": "310000",
41 | "areaname": "上海市"
42 | },
43 | {
44 | "areaid": "320000",
45 | "areaname": "江苏省"
46 | },
47 | {
48 | "areaid": "330000",
49 | "areaname": "浙江省"
50 | },
51 | {
52 | "areaid": "340000",
53 | "areaname": "安徽省"
54 | },
55 | {
56 | "areaid": "350000",
57 | "areaname": "福建省"
58 | },
59 | {
60 | "areaid": "360000",
61 | "areaname": "江西省"
62 | },
63 | {
64 | "areaid": "370000",
65 | "areaname": "山东省"
66 | },
67 | {
68 | "areaid": "410000",
69 | "areaname": "河南省"
70 | },
71 | {
72 | "areaid": "420000",
73 | "areaname": "湖北省"
74 | },
75 | {
76 | "areaid": "430000",
77 | "areaname": "湖南省"
78 | },
79 | {
80 | "areaid": "440000",
81 | "areaname": "广东省"
82 | },
83 | {
84 | "areaid": "450000",
85 | "areaname": "广西壮族自治区"
86 | },
87 | {
88 | "areaid": "460000",
89 | "areaname": "海南省"
90 | },
91 | {
92 | "areaid": "500000",
93 | "areaname": "重庆市"
94 | },
95 | {
96 | "areaid": "510000",
97 | "areaname": "四川省"
98 | },
99 | {
100 | "areaid": "520000",
101 | "areaname": "贵州省"
102 | },
103 | {
104 | "areaid": "530000",
105 | "areaname": "云南省"
106 | },
107 | {
108 | "areaid": "540000",
109 | "areaname": "西藏自治区"
110 | },
111 | {
112 | "areaid": "610000",
113 | "areaname": "陕西省"
114 | },
115 | {
116 | "areaid": "620000",
117 | "areaname": "甘肃省"
118 | },
119 | {
120 | "areaid": "630000",
121 | "areaname": "青海省"
122 | },
123 | {
124 | "areaid": "640000",
125 | "areaname": "宁夏回族自治区"
126 | },
127 | {
128 | "areaid": "650000",
129 | "areaname": "新疆维吾尔自治区"
130 | }
131 | ]
132 | }
--------------------------------------------------------------------------------
/Sample/src/main/java/com/robin/lazy/sample/App.java:
--------------------------------------------------------------------------------
1 | /*
2 | * 文 件 名: App.java
3 | * 版 权: Technologies Co., Ltd. Copyright YYYY-YYYY, All rights reserved
4 | * 描 述: <描述>
5 | * 修 改 人: 江钰锋 00501
6 | * 修改时间: 16/6/3
7 | * 跟踪单号: <跟踪单号>
8 | * 修改单号: <修改单号>
9 | * 修改内容: <修改内容>
10 | */
11 |
12 | package com.robin.lazy.sample;
13 |
14 | import android.app.Application;
15 |
16 | import com.robin.lazy.cache.CacheLoaderManager;
17 | import com.robin.lazy.cache.disk.naming.HashCodeFileNameGenerator;
18 |
19 | /**
20 | * <一句话功能简述>
21 | * <功能详细描述>
22 | *
23 | * @author 江钰锋 00501
24 | * @version [版本号, 16/6/3]
25 | * @see [相关类/方法]
26 | * @since [产品/模块版本]
27 | */
28 | public class App extends Application {
29 | @Override
30 | public void onCreate() {
31 | super.onCreate();
32 | CacheLoaderManager.getInstance().init(this, new HashCodeFileNameGenerator(), 1024 * 1024 * 8, 50, 20);
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/Sample/src/main/java/com/robin/lazy/sample/FileUtil.java:
--------------------------------------------------------------------------------
1 | package com.robin.lazy.sample;
2 |
3 | import android.content.Context;
4 |
5 | import java.io.ByteArrayOutputStream;
6 | import java.io.File;
7 | import java.io.FileNotFoundException;
8 | import java.io.IOException;
9 | import java.io.InputStream;
10 |
11 | /**
12 | * 文件工具
13 | *
14 | * @author jiangyufeng
15 | * @version [版本号, 2015年11月23日]
16 | * @see [相关类/方法]
17 | * @since [产品/模块版本]
18 | */
19 | public class FileUtil {
20 | /**
21 | * 删除文件
22 | *
23 | * @param context 程序上下文
24 | * @param fileName 文件名,要在系统内保持唯一
25 | * @return boolean 存储成功的标志
26 | */
27 | public static boolean deleteFile(Context context, String fileName) {
28 | return context.deleteFile(fileName);
29 | }
30 |
31 | /**
32 | * 文件是否存在
33 | *
34 | * @param context
35 | * @param fileName
36 | * @return
37 | */
38 | public static boolean exists(Context context, String fileName) {
39 | return new File(context.getFilesDir(), fileName).exists();
40 | }
41 |
42 | /**
43 | * 读取文本数据
44 | *
45 | * @param context 程序上下文
46 | * @param fileName 文件名
47 | * @return String, 读取到的文本内容,失败返回null
48 | */
49 | public static String readAssets(Context context, String fileName) {
50 | InputStream is = null;
51 | String content = null;
52 | try {
53 | is = context.getAssets().open(fileName);
54 | if (is != null) {
55 |
56 | byte[] buffer = new byte[1024];
57 | ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
58 | while (true) {
59 | int readLength = is.read(buffer);
60 | if (readLength == -1) break;
61 | arrayOutputStream.write(buffer, 0, readLength);
62 | }
63 | is.close();
64 | arrayOutputStream.close();
65 | content = new String(arrayOutputStream.toByteArray());
66 |
67 | }
68 | } catch (FileNotFoundException e) {
69 | e.printStackTrace();
70 | } catch (IOException e) {
71 | e.printStackTrace();
72 | content = null;
73 | } finally {
74 | try {
75 | if (is != null) is.close();
76 | } catch (IOException ioe) {
77 | ioe.printStackTrace();
78 | }
79 | }
80 | return content;
81 | }
82 |
83 | }
84 |
--------------------------------------------------------------------------------
/Sample/src/main/java/com/robin/lazy/sample/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.robin.lazy.sample;
2 |
3 | import android.os.Bundle;
4 | import androidx.appcompat.app.AppCompatActivity;
5 | import android.text.TextUtils;
6 | import android.view.View;
7 | import android.widget.TextView;
8 | import android.widget.Toast;
9 |
10 | import com.robin.lazy.cache.CacheLoaderManager;
11 |
12 | import in.srain.cube.views.ptr.PtrDefaultHandler;
13 | import in.srain.cube.views.ptr.PtrFrameLayout;
14 | import in.srain.cube.views.ptr.PtrHandler;
15 | import in.srain.cube.views.ptr.header.StoreHouseHeader;
16 | import in.srain.cube.views.ptr.util.PtrLocalDisplay;
17 |
18 | public class MainActivity extends AppCompatActivity implements View.OnClickListener {
19 |
20 | private long lastTime;
21 | private TextView textView;
22 |
23 | @Override
24 | protected void onCreate(Bundle savedInstanceState) {
25 | super.onCreate(savedInstanceState);
26 | View view= View.inflate(this,R.layout.activity_main,null);
27 | setContentView(view);
28 | findViewById(R.id.buttonSave).setOnClickListener(this);
29 | findViewById(R.id.buttonLoad).setOnClickListener(this);
30 | findViewById(R.id.buttonClear).setOnClickListener(this);
31 | textView=(TextView)findViewById(R.id.textView);
32 | }
33 |
34 | @Override
35 | public void onClick(View v) {
36 | int id = v.getId();
37 | if (id == R.id.buttonSave) {
38 | //把数据存入缓存需要操作io流,有对io的操作都是比较耗时的,最好都在子线程完成.
39 | //android 理想的fps是大于等于60(16s/帧),这种情况应用操作起来才会感觉不到卡顿
40 | //尽管测出来存储缓存数据使用的时间在6s左右,但是为了性能考虑,最好在子线程中操作
41 | new Thread(new Runnable() {
42 | @Override
43 | public void run() {
44 | runOnUiThread(new Runnable() {
45 | @Override
46 | public void run() {
47 | String area_strs = FileUtil.readAssets(MainActivity.this, "province.json");
48 | lastTime = System.currentTimeMillis();
49 | CacheLoaderManager.getInstance().saveString("area_strs", area_strs, 0);
50 | CacheLoaderManager.getInstance().saveString("area_strs1", area_strs, 0);
51 | textView.setText("保存数据用时:"+(System.currentTimeMillis() - lastTime) + "毫秒");
52 | }
53 | });
54 | }
55 | }).start();
56 | } else if (id == R.id.buttonLoad) {
57 | lastTime = System.currentTimeMillis();
58 | String area_strs=CacheLoaderManager.getInstance().loadString("area_strs");
59 | area_strs=area_strs+"\n\n"+CacheLoaderManager.getInstance().loadString("area_strs1");
60 | Toast.makeText(MainActivity.this, "加载数据用时:" + (System.currentTimeMillis() - lastTime) + "毫秒", Toast.LENGTH_SHORT).show();
61 | textView.setText(area_strs);
62 | }else if(id==R.id.buttonClear){
63 | CacheLoaderManager.getInstance().clear();
64 | textView.setText(null);
65 | }
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/Sample/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
11 |
12 |
16 |
17 |
21 |
29 |
30 |
37 |
44 |
45 |
46 |
49 |
50 |
54 |
62 |
63 |
64 |
65 |
--------------------------------------------------------------------------------
/Sample/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Robin-jiangyufeng/LazyCacheForAndroid/b4932bbb9a5428097663caef2a2d9af7b46edb9c/Sample/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/Sample/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Robin-jiangyufeng/LazyCacheForAndroid/b4932bbb9a5428097663caef2a2d9af7b46edb9c/Sample/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/Sample/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Robin-jiangyufeng/LazyCacheForAndroid/b4932bbb9a5428097663caef2a2d9af7b46edb9c/Sample/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/Sample/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Robin-jiangyufeng/LazyCacheForAndroid/b4932bbb9a5428097663caef2a2d9af7b46edb9c/Sample/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/Sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Robin-jiangyufeng/LazyCacheForAndroid/b4932bbb9a5428097663caef2a2d9af7b46edb9c/Sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/Sample/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/Sample/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/Sample/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 |
--------------------------------------------------------------------------------
/Sample/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Sample
3 |
4 |
--------------------------------------------------------------------------------
/Sample/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/Sample/src/test/java/com/robin/lazy/sample/Calculator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * 文 件 名: Calculator.java
3 | * 版 权: Technologies Co., Ltd. Copyright YYYY-YYYY, All rights reserved
4 | * 描 述: <描述>
5 | * 修 改 人: 江钰锋 00501
6 | * 修改时间: 16/6/6
7 | * 跟踪单号: <跟踪单号>
8 | * 修改单号: <修改单号>
9 | * 修改内容: <修改内容>
10 | */
11 |
12 | package com.robin.lazy.sample;
13 |
14 | /**
15 | * <一句话功能简述>
16 | * <功能详细描述>
17 | *
18 | * @author 江钰锋 00501
19 | * @version [版本号, 16/6/6]
20 | * @see [相关类/方法]
21 | * @since [产品/模块版本]
22 | */
23 | public class Calculator {
24 |
25 | /**
26 | * 加
27 | * @param a
28 | * @param b
29 | * @return
30 | */
31 | public double plus(double a, double b){
32 | return a+b;
33 | }
34 |
35 | /**
36 | * 减
37 | * @param a
38 | * @param b
39 | * @return
40 | */
41 | public double substract(double a, double b){
42 | return a-b;
43 | }
44 |
45 | /**
46 | * 乘
47 | * @param a
48 | * @param b
49 | * @return
50 | */
51 | public double multiply(double a, double b){
52 | return a*b;
53 | }
54 |
55 | /**
56 | * 除
57 | * @param a
58 | * @param b
59 | * @return
60 | */
61 | public double divide(double a, double b){
62 | return a/b;
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/Sample/src/test/java/com/robin/lazy/sample/CalculatorTest.java:
--------------------------------------------------------------------------------
1 | package com.robin.lazy.sample;
2 |
3 | import junit.framework.Assert;
4 |
5 | import org.junit.Before;
6 | import org.junit.Test;
7 |
8 | /**
9 | * 文 件 名: CalculatorTest.java
10 | * 版 权: Technologies Co., Ltd. Copyright YYYY-YYYY, All rights reserved
11 | * 描 述: <描述>
12 | * 修 改 人: 江钰锋 00501
13 | * 修改时间: 16/6/6
14 | * 跟踪单号: <跟踪单号>
15 | * 修改单号: <修改单号>
16 | * 修改内容: <修改内容>
17 | */
18 |
19 | public class CalculatorTest {
20 | private Calculator calculator;
21 |
22 | @Before
23 | public void setUp() throws Exception {
24 | calculator=new Calculator();
25 | }
26 |
27 | @Test
28 | public void testPlus() throws Exception {
29 | Assert.assertEquals(11d,calculator.plus(5d,6d));
30 | }
31 |
32 | @Test
33 | public void testSubstract() throws Exception {
34 | Assert.assertEquals(-1d,calculator.substract(5d, 6d));
35 | }
36 |
37 | @Test
38 | public void testMultiply() throws Exception {
39 | Assert.assertEquals(30d,calculator.multiply(5d, 6d));
40 | }
41 |
42 | @Test
43 | public void testDivide() throws Exception {
44 | Assert.assertEquals(4d,calculator.divide(5d, 6d));
45 | }
46 |
47 | @Test
48 | public void testArea(){
49 | System.out.printf("aaaaaaa");
50 | }
51 | }
--------------------------------------------------------------------------------
/Sample/src/test/java/com/robin/lazy/sample/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.robin.lazy.sample;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * To work on unit tests, switch the Test Artifact in the Build Variants view.
9 | */
10 | public class ExampleUnitTest {
11 | @Test
12 | public void addition_isCorrect() throws Exception {
13 | assertEquals(4, 2 + 2);
14 | }
15 | }
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | repositories {
5 | google()
6 | jcenter()
7 | mavenCentral()
8 | maven { url "https://jitpack.io" }
9 | }
10 |
11 | dependencies {
12 | classpath "com.android.tools.build:gradle:7.2.1"
13 | //自动上传至Bintray平台插件
14 | // classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4'
15 | // NOTE: Do not place your application dependencies here; they belong
16 | // in the individual module build.gradle files
17 | }
18 | tasks.withType(JavaCompile) { options.encoding = "UTF-8" }
19 | }
20 |
21 | allprojects {
22 | repositories {
23 | google()
24 | jcenter()
25 | mavenCentral()
26 | maven { url "https://jitpack.io" }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | android.enableJetifier=true
2 | android.useAndroidX=true
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Robin-jiangyufeng/LazyCacheForAndroid/b4932bbb9a5428097663caef2a2d9af7b46edb9c/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Fri Jul 22 10:35:34 CST 2022
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':LazyCache', ':Sample'
2 |
--------------------------------------------------------------------------------