--- /dev/null
+/*
+ * Dynamic Analyzer
+ *
+ * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ * Hyunjong Park <phjwithyou.park@samsung.com>
+ * Juyoung Kim <j0.kim@samsung.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ *
+ */
+
+package org.tizen.dynamicanalyzer.utils;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.IOException;
+
+import org.tizen.dynamicanalyzer.common.AnalyzerConstants;
+import org.tizen.dynamicanalyzer.common.AnalyzerManager;
+import org.tizen.dynamicanalyzer.communicator.CommunicatorUtils;
+import org.tizen.dynamicanalyzer.constant.CommonConstants;
+import org.tizen.dynamicanalyzer.util.CommonUtil;
+import org.tizen.dynamicanalyzer.util.DALogger;
+import org.tizen.sdblib.service.SyncResult;
+
+public class DownloadContentfileManager {
+
+ private final static int SPLIT_PATH_INDEX = 1;
+
+ /**
+ * Download file of contents that payload of network or shader source of
+ * OpenGL from device.
+ *
+ * @param contents
+ * It will check that string like
+ * "SkWek2n3_File:/tmp/<file_name>" is included.
+ * @return new file path of download file
+ */
+ public static String getDowonloadFilePath(String contents) {
+ String returnString = contents;
+ if (contents.contains(AnalyzerConstants.CHECK_DOWNLOAD_FILE_STRING)) {
+ String[] strFile = contents
+ .split(AnalyzerConstants.CHECK_DOWNLOAD_FILE_STRING);
+ if (strFile.length < SPLIT_PATH_INDEX) {
+ return returnString;
+ }
+ String deviceFilePath = strFile[SPLIT_PATH_INDEX];
+
+ String[] fileNameSplit = deviceFilePath.split(File.separator
+ + AnalyzerConstants.TMP_FOLDER + File.separator);
+ if (fileNameSplit.length < SPLIT_PATH_INDEX) {
+ return returnString;
+ }
+ String downloadFolderPath = File.separator
+ + AnalyzerConstants.DOWNLOAD_FILE_FOLDER;
+
+ makeFolder(AnalyzerManager.getProject().getSavePath()
+ + downloadFolderPath);
+
+ String saveFilePath = downloadFolderPath + File.separator
+ + fileNameSplit[SPLIT_PATH_INDEX];
+
+ if (pullFile(deviceFilePath, AnalyzerManager.getProject()
+ .getSavePath() + saveFilePath)) {
+ returnString = saveFilePath;
+ }
+ }
+ return returnString;
+ }
+
+ /**
+ * copy contents file from device to local save folder. if the copy of the
+ * file is successful, delete the file on the device.
+ *
+ * @param from
+ * device file path
+ * @param to
+ * local file path
+ * @return
+ */
+ public static boolean pullFile(String from, String to) {
+ SyncResult res = CommunicatorUtils.pull(from, to);
+ if (null != res && res.isOk()) {
+ DALogger.getInstance().debug(from + "file copying success!!");//$NON-NLS-1$
+ } else {
+ DALogger.getInstance().debug("Failed to get " + from); //$NON-NLS-1$
+ return false;
+ }
+ CommunicatorUtils.removeCommand(from);
+ return true;
+ }
+
+ /**
+ * get contents of file
+ *
+ * @param contents
+ * get file path from database. It will check that string like
+ * "/download_file/<file_name>" is included.
+ * @return contents
+ */
+ public static String getFileContents(String contents) {
+ String returnString = contents;
+ if (contents.contains(AnalyzerConstants.DOWNLOAD_FILE_FOLDER
+ + File.separator)) {
+ StringBuffer contextsBuffer = new StringBuffer();
+
+ String filePath = AnalyzerManager.getProject().getSavePath()
+ + contents;
+ File contentsFile = new File(filePath);
+ if (contentsFile.isFile()) {
+ BufferedReader in = null;
+ String content;
+ try {
+ in = new BufferedReader(new FileReader(contentsFile));
+ while (null != (content = in.readLine())) {
+ contextsBuffer.append(content).append(
+ CommonConstants.NEW_LINE);
+ }
+ } catch (FileNotFoundException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ } finally {
+ CommonUtil.tryClose(in);
+ }
+ }
+ returnString = contextsBuffer.toString();
+ }
+ return returnString;
+ }
+
+ /**
+ * make new folder when not exist folder
+ *
+ * @param folderPath
+ *
+ */
+ private static void makeFolder(String folderPath) {
+ File toDir = new File(folderPath);
+ if (!toDir.exists() || !toDir.isDirectory()) {
+ toDir.mkdirs();
+ }
+ }
+
+}