diff options
author | Bilal Alsharifi <bilal.alsharifi@gmail.com> | 2020-12-09 15:03:00 -0500 |
---|---|---|
committer | Bilal Alsharifi <bilal.alsharifi@gmail.com> | 2020-12-09 15:03:00 -0500 |
commit | 2fe1c118193b2afdb50ff81a0d18b712eeed70f8 (patch) | |
tree | 42eb2fe46b71fc180dd06f4a3b382031021e625c /javaSE | |
parent | def46af9b23a5baf31f8da737d619439e4e5a6e7 (diff) | |
download | sdl_android-2fe1c118193b2afdb50ff81a0d18b712eeed70f8.tar.gz |
Refactor FileManager to use queues
Diffstat (limited to 'javaSE')
-rw-r--r-- | javaSE/javaSE/src/main/java/com/smartdevicelink/managers/file/FileManager.java | 75 |
1 files changed, 14 insertions, 61 deletions
diff --git a/javaSE/javaSE/src/main/java/com/smartdevicelink/managers/file/FileManager.java b/javaSE/javaSE/src/main/java/com/smartdevicelink/managers/file/FileManager.java index a2a464c11..8239a9310 100644 --- a/javaSE/javaSE/src/main/java/com/smartdevicelink/managers/file/FileManager.java +++ b/javaSE/javaSE/src/main/java/com/smartdevicelink/managers/file/FileManager.java @@ -41,9 +41,9 @@ import com.smartdevicelink.proxy.rpc.PutFile; import com.smartdevicelink.util.DebugTool; import com.smartdevicelink.util.FileUtls; -import java.io.IOException; -import java.io.InputStream; +import java.io.*; import java.net.URI; +import java.nio.file.Files; /** * <strong>FileManager</strong> <br> @@ -74,76 +74,29 @@ public class FileManager extends BaseFileManager { super(internalInterface, fileManagerConfig); } - /** - * Creates and returns a PutFile request that would upload a given SdlFile - * - * @param file SdlFile with fileName and one of A) fileData, B) Uri, or C) resourceID set - * @return a valid PutFile request if SdlFile contained a fileName and sufficient data - */ @Override - PutFile createPutFile(@NonNull final SdlFile file) { - PutFile putFile = new PutFile(); - if (file.getName() == null) { - throw new IllegalArgumentException("You must specify an file name in the SdlFile"); - } else { - putFile.setSdlFileName(file.getName()); - } + InputStream openInputStreamWithFile(@NonNull SdlFile file) { + InputStream inputStream = null; if (file.getFilePath() != null) { - //Attempt to access the file via a path - byte[] data = FileUtls.getFileData(file.getFilePath()); - if (data != null) { - putFile.setFileData(data); - } else { - throw new IllegalArgumentException("File at path was empty"); + try { + inputStream = new FileInputStream(file.getFilePath()); + } catch (FileNotFoundException e) { + e.printStackTrace(); } } else if (file.getURI() != null) { - // Use URI to upload file - byte[] data = contentsOfUri(file.getURI()); - if (data != null) { - putFile.setFileData(data); - } else { - throw new IllegalArgumentException("Uri was empty"); + try { + inputStream = file.getURI().toURL().openStream(); + } catch (IOException e) { + e.printStackTrace(); } } else if (file.getFileData() != null) { - // Use file data (raw bytes) to upload file - putFile.setFileData(file.getFileData()); + inputStream = new ByteArrayInputStream(file.getFileData()); } else { throw new IllegalArgumentException("The SdlFile to upload does " + "not specify its resourceId, Uri, or file data"); } - if (file.getType() != null) { - putFile.setFileType(file.getType()); - } - putFile.setPersistentFile(file.isPersistent()); - - return putFile; - } - - - /** - * Helper method to take Uri and turn it into byte array - * - * @param uri Uri for desired file - * @return Resulting byte array - */ - private byte[] contentsOfUri(URI uri) { - InputStream is = null; - try { - is = uri.toURL().openStream(); - return contentsOfInputStream(is); - } catch (IOException e) { - DebugTool.logError(TAG, "Can't read from URI", e); - return null; - } finally { - if (is != null) { - try { - is.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - } + return inputStream; } } |