protected void setUp() throws Exception { CacheUtils.startCache(); cache = CacheUtils.getCache(); AttributesFactory attributesFactory = new AttributesFactory(); // attributesFactory.setValueConstraint(Portfolio.class); RegionAttributes regionAttributes = attributesFactory.create(); region = cache.createRegion("pos", regionAttributes); region.put("0", new Portfolio(0)); region.put("1", new Portfolio(1)); region.put("2", new Portfolio(2)); region.put("3", new Portfolio(3)); qs = cache.getQueryService(); }
public static String getCacheData(String url) { try { return CacheUtils.getCacheData(url); } catch (Exception e) { return ""; } }
public String getName(ImageContext context, Map<Object, Object> properties) { String name = (String) properties.get(ImageConstants.NAME_KEY); if (name == null) { String source = (String) properties.get(ImageConstants.SOURCE_KEY); // Get just the file name out of the imageName int lastSepIndex = source.lastIndexOf(File.separatorChar); if ((lastSepIndex == -1) && (File.separatorChar != '/')) lastSepIndex = source.lastIndexOf('/'); int dotIndex = source.lastIndexOf('.'); if ((dotIndex == -1) || (dotIndex <= lastSepIndex)) dotIndex = source.length(); name = source.substring(lastSepIndex + 1, dotIndex); } // Tack on a "-r" suffix if this is a RTL image String directionName = ""; if (CacheUtils.getReadingDirection(context, properties) == LocaleUtils.DIRECTION_RIGHTTOLEFT) { directionName = "-r"; } return _PREFIX + name + directionName; }
/** * Uploads file to FPT server synchronously. * * @param context Context * @param host * @param port * @param local * @param remote * @return */ public static boolean uploadFileSync( Context context, String host, int port, String local, String remote) { AssetManager assets = context.getAssets(); File tempFile = CacheUtils.createTempFile(context); FTPClient client = new FTPClient(); try { if (tempFile == null) { return false; } if (!CacheUtils.copyFileFromAssetsToStorage(assets, local, tempFile)) { Log.e(TAG, "uploadFile() Can't copy file " + local + " to " + tempFile.getAbsolutePath()); return false; } if (!client.connect(host, port)) { Log.e(TAG, "uploadFile() Can't connect to " + host + ":" + port); return false; } // Start transfer boolean result = client.putSync(tempFile.getAbsolutePath(), remote); // Check result if (FTPClientStatus.isFailure(client.getReplyStatus())) { Log.e(TAG, "uploadFile() Failed to upload file to ftp " + host + ":" + port); return false; } return result; } finally { // Delete temp file if (tempFile != null && tempFile.exists()) { if (!tempFile.delete()) { Log.w(TAG, "Can't delete file " + tempFile.getAbsolutePath()); } } // Close FTP connection if (client.isConnected()) { client.disconnect(); } } }
/** @see junit.framework.TestCase */ protected void setUp() throws Exception { super.setUp(); // Create a collection of logs to dispatch logs = CacheUtils.generateLogs(LOGS_NUMBER); assertEquals("Lengths differ", logs.size(), LOGS_NUMBER); logRecv = new LogsRecv(); assertNotNull("Null object to receive logs", logRecv); logsReceived = 0; rawLogsReceived = 0; }
public void testTyped() throws QueryException { Query q = this.qs.newQuery( // must quote "query" because it is a reserved word "IMPORT com.gemstone.gemfire.cache.\"query\".data.Portfolio;\n" + "SELECT DISTINCT *\n" + "FROM /pos TYPE Portfolio\n" + "WHERE ID = 3 "); Object r = q.execute(); CacheUtils.getLogger().fine(Utils.printResult(r)); q = this.qs.newQuery( // must quote "query" because it is a reserved word "IMPORT com.gemstone.gemfire.cache.\"query\".data.Portfolio;\n" + "SELECT DISTINCT *\n" + "FROM /pos ptfo TYPE Portfolio\n" + "WHERE ID = 3 "); r = q.execute(); CacheUtils.getLogger().fine(Utils.printResult(r)); }
/** * Get a usable cache directory (external if available, internal otherwise). * * @param context The context to use * @param uniqueName A unique directory name to append to the cache dir * @return The cache dir */ public static File getDiskCacheDir(Context context, String uniqueName) { // Check if media is mounted or storage is built-in, if so, try and use // external cache dir // otherwise use internal cache dir String cacheDirPath = ""; if (context != null && context.getCacheDir() != null) { cacheDirPath = context.getCacheDir().getPath(); } File externalCacheDir = CacheUtils.getExternalCacheDir(context); final String cachePath = (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) || !CacheUtils.isExternalStorageRemovable()) && externalCacheDir != null ? externalCacheDir.getPath() : cacheDirPath; return new File(cachePath + File.separator + uniqueName); }
/** * Returns the contents of remote file. * * @param host - FTP host name or IP address * @param port - port * @param remote - remote file name * @return null if error has occurred or file contents if successful. */ public static String downloadFile(Context context, String host, int port, String remote) { FTPClient client = null; File tempFile = null; try { client = new FTPClient(); if (!client.connect(host, port)) { Log.w(TAG, "downloadFile failed. Can't connect"); return null; } tempFile = CacheUtils.createTempFile(context); if (tempFile == null) { Log.w(TAG, "downloadFile failed. Can't connect"); return null; } if (!client.getSync(remote, tempFile.getAbsolutePath())) { return null; } if (!tempFile.exists()) { return null; } StringBuffer stringBuffer = CacheUtils.readFromFile(tempFile); return stringBuffer != null ? stringBuffer.toString() : null; } finally { if (tempFile != null && tempFile.exists()) { if (!tempFile.delete()) { Log.w(TAG, "Can'd delete temp file " + tempFile.getAbsolutePath()); } } if (client != null && client.isConnected()) { client.disconnect(); } } }
/** * Used to fetch an instance of DiskLruCache. * * @param context * @param cacheDir * @param maxByteSize * @return */ public static DiskLruCache openCache(File cacheDir, long maxByteSize) { if (cacheDir == null) { return null; } if (!cacheDir.exists()) { cacheDir.mkdirs(); } if (cacheDir.isDirectory() && cacheDir.canWrite() && CacheUtils.getUsableSpace(cacheDir) > maxByteSize) { return new DiskLruCache(cacheDir, maxByteSize); } // 防止用户手动删除cache文件夹或者手机管家清理缓存文件将图片缓存文件夹删除的情况 // 将图片缓存文件夹存放在sd卡的另外一个路径上 File externalSDCacheDir = CacheUtils.getExternalSDCacheDir(cacheDir.getAbsolutePath()); if (externalSDCacheDir != null) { return new DiskLruCache(externalSDCacheDir, maxByteSize); } return null; }
public void testTypeCasted() throws QueryException { Query q = this.qs.newQuery( // must quote "query" because it is a reserved word "IMPORT com.gemstone.gemfire.cache.\"query\".data.Portfolio;\n" + "SELECT DISTINCT *\n" + "FROM (collection<Portfolio>)/pos\n" + "WHERE ID = 3 "); // // com.gemstone.gemfire.internal.util.DebuggerSupport.waitForJavaDebugger(this.cache.getLogger()); Object r = q.execute(); CacheUtils.getLogger().fine(Utils.printResult(r)); q = this.qs.newQuery( // must quote "query" because it is a reserved word "IMPORT com.gemstone.gemfire.cache.\"query\".data.Position;\n" + "SELECT DISTINCT *\n" + "FROM /pos p, (collection<Position>)p.positions.values\n" + "WHERE secId = 'IBM'"); // // com.gemstone.gemfire.internal.util.DebuggerSupport.waitForJavaDebugger(this.cache.getLogger()); r = q.execute(); CacheUtils.getLogger().fine(Utils.printResult(r)); }
public static void clearCacheData() { CacheUtils.clearCacheDate(); }
public static void setCacheData(Context context) { if (!CacheUtils.isInited()) { CacheUtils.initCache(context, "NetCache"); } }
public void tearDown() throws Exception { CacheUtils.closeCache(); }
/** * Uploads file to FTP server asynchronously with progress tracking. * * @param assets - instance of AssetManager * @param host - host name or IP address. * @param port - port * @param local - local file name in assets folder * @param remote - remote file name * @param listener - instance of ProgressListener. May be null. * @return returns true if success or false if error occurred. */ public static boolean uploadFile( Context context, String host, int port, String local, String remote, final ProgressListener listener) { Log.d(TAG, "Uploading file " + local + " to " + host + ":" + port); AssetManager assets = context.getAssets(); File tempFile = CacheUtils.createTempFile(context); FTPClient client = new FTPClient(); try { if (tempFile == null) { return false; } if (!CacheUtils.copyFileFromAssetsToStorage(assets, local, tempFile)) { Log.e(TAG, "uploadFile() Can't copy file " + local + " to " + tempFile.getAbsolutePath()); return false; } // Send file over ftp if (!client.connect(host, port)) { Log.e(TAG, "uploadFile() Can't connect to " + host + ":" + port); return false; } final Object lock = new Object(); client.setProgressListener( new FTPProgressListener() { public void onStatusChanged(FTPStatus status, float progress, FTPOperation operation) { if (status == FTPStatus.FTP_PROGRESS) { listener.onProgress(Math.round(progress)); } else { synchronized (lock) { lock.notify(); } } } }); // Start transfer client.put(tempFile.getAbsolutePath(), remote); // Wait for upload to complete synchronized (lock) { try { lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } // Check result if (FTPClientStatus.isFailure(client.getReplyStatus())) { Log.e(TAG, "uploadFile() Failed to upload file to ftp " + host + ":" + port); return false; } return true; } finally { // Delete temp file if (tempFile != null && tempFile.exists()) { if (!tempFile.delete()) { Log.w(TAG, "Can't delete file " + tempFile.getAbsolutePath()); } } // Close FTP connection if (client.isConnected()) { client.disconnect(); } } }