summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBilal Alsharifi <bilal.alsharifi@gmail.com>2020-08-24 17:10:17 -0400
committerBilal Alsharifi <bilal.alsharifi@gmail.com>2020-08-24 17:10:17 -0400
commit7d2a33cf4c2f9d38734bf6c383fe866e9b5a292f (patch)
tree85deda3145bab3c998a31d7f73f48256b16072e3
parent8548de0830155c981add487e9bd9f6b020110a2c (diff)
downloadsdl_android-7d2a33cf4c2f9d38734bf6c383fe866e9b5a292f.tar.gz
Add SdlFile constructor with InputStream
-rw-r--r--android/sdl_android/src/main/java/com/smartdevicelink/managers/file/FileManager.java8
-rw-r--r--android/sdl_android/src/main/java/com/smartdevicelink/managers/file/filetypes/SdlArtwork.java15
-rw-r--r--android/sdl_android/src/main/java/com/smartdevicelink/managers/file/filetypes/SdlFile.java101
-rw-r--r--base/src/main/java/com/smartdevicelink/managers/file/BaseFileManager.java18
4 files changed, 127 insertions, 15 deletions
diff --git a/android/sdl_android/src/main/java/com/smartdevicelink/managers/file/FileManager.java b/android/sdl_android/src/main/java/com/smartdevicelink/managers/file/FileManager.java
index bdd21ba45..49e31de0d 100644
--- a/android/sdl_android/src/main/java/com/smartdevicelink/managers/file/FileManager.java
+++ b/android/sdl_android/src/main/java/com/smartdevicelink/managers/file/FileManager.java
@@ -118,6 +118,14 @@ public class FileManager extends BaseFileManager {
}else if(file.getFileData() != null){
// Use file data (raw bytes) to upload file
putFile.setFileData(file.getFileData());
+ } else if(file.getInputStream() != null) {
+ // Use input stream to upload file
+ byte[] contents = contentsOfInputStream(file.getInputStream(), file.getOffset(), file.getLength());
+ if (contents != null) {
+ putFile.setFileData(contents);
+ } else {
+ throw new IllegalArgumentException("Input stream was empty");
+ }
}else{
throw new IllegalArgumentException("The SdlFile to upload does " +
"not specify its resourceId, Uri, or file data");
diff --git a/android/sdl_android/src/main/java/com/smartdevicelink/managers/file/filetypes/SdlArtwork.java b/android/sdl_android/src/main/java/com/smartdevicelink/managers/file/filetypes/SdlArtwork.java
index d0e46f7e5..b345e971d 100644
--- a/android/sdl_android/src/main/java/com/smartdevicelink/managers/file/filetypes/SdlArtwork.java
+++ b/android/sdl_android/src/main/java/com/smartdevicelink/managers/file/filetypes/SdlArtwork.java
@@ -41,6 +41,8 @@ import com.smartdevicelink.proxy.rpc.enums.ImageType;
import com.smartdevicelink.proxy.rpc.enums.StaticIconName;
import com.smartdevicelink.util.DebugTool;
+import java.io.InputStream;
+
/**
* A class that extends SdlFile, representing artwork (JPEG, PNG, or BMP) to be uploaded to core
*/
@@ -88,6 +90,19 @@ public class SdlArtwork extends SdlFile implements Cloneable{
/**
* Creates a new instance of SdlArtwork
+ * @param fileName a String value representing the name that will be used to store the file in the head unit.
+ * @param fileType a FileType enum value representing the type of the file
+ * @param inputStream The stream of data that the FileManager will read from
+ * @param offset The data offset in bytes, a value of zero is used to indicate data starting from the beginning of the stream
+ * @param length The number of bytes to be read from the stream
+ * @param persistentFile a boolean value that indicates if the file is meant to persist between sessions / ignition cycles
+ */
+ public SdlArtwork(@NonNull String fileName, @NonNull FileType fileType, @NonNull InputStream inputStream, int offset, int length, boolean persistentFile){
+ super(fileName, fileType, inputStream, offset, length, persistentFile);
+ }
+
+ /**
+ * Creates a new instance of SdlArtwork
* @param staticIconName a StaticIconName enum value representing the name of a static file that comes pre-shipped with the head unit
*/
public SdlArtwork(@NonNull StaticIconName staticIconName) {
diff --git a/android/sdl_android/src/main/java/com/smartdevicelink/managers/file/filetypes/SdlFile.java b/android/sdl_android/src/main/java/com/smartdevicelink/managers/file/filetypes/SdlFile.java
index 4fe5e3898..db585ed4b 100644
--- a/android/sdl_android/src/main/java/com/smartdevicelink/managers/file/filetypes/SdlFile.java
+++ b/android/sdl_android/src/main/java/com/smartdevicelink/managers/file/filetypes/SdlFile.java
@@ -33,11 +33,13 @@
package com.smartdevicelink.managers.file.filetypes;
import android.net.Uri;
+
import androidx.annotation.NonNull;
import com.smartdevicelink.proxy.rpc.enums.FileType;
import com.smartdevicelink.proxy.rpc.enums.StaticIconName;
+import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
@@ -49,6 +51,9 @@ public class SdlFile{
private int id = -1;
private Uri uri;
private byte[] fileData;
+ private InputStream inputStream;
+ private int offset;
+ private int length;
private FileType fileType;
private boolean persistentFile;
private boolean isStaticIcon;
@@ -105,6 +110,24 @@ public class SdlFile{
/**
* Creates a new instance of SdlFile
+ * @param fileName a String value representing the name that will be used to store the file in the head unit.
+ * @param fileType a FileType enum value representing the type of the file
+ * @param inputStream The stream of data that the FileManager will read from
+ * @param offset The data offset in bytes, a value of zero is used to indicate data starting from the beginning of the stream
+ * @param length The number of bytes to be read from the stream
+ * @param persistentFile a boolean value that indicates if the file is meant to persist between sessions / ignition cycles
+ */
+ public SdlFile(@NonNull String fileName, @NonNull FileType fileType, @NonNull InputStream inputStream, int offset, int length, boolean persistentFile) {
+ setName(fileName);
+ setType(fileType);
+ setInputStream(inputStream);
+ setOffset(offset);
+ setLength(length);
+ setPersistent(persistentFile);
+ }
+
+ /**
+ * Creates a new instance of SdlFile
* @param staticIconName a StaticIconName enum value representing the name of a static file that comes pre-shipped with the head unit
*/
public SdlFile(@NonNull StaticIconName staticIconName){
@@ -123,13 +146,18 @@ public class SdlFile{
this.shouldAutoGenerateName = false;
this.fileName = fileName;
} else {
- this.shouldAutoGenerateName = true;
- if (this.getFileData() != null) {
- this.fileName = generateFileNameFromData(this.getFileData());
- } else if (this.getUri() != null) {
- this.fileName = generateFileNameFromUri(this.getUri());
- } else if (this.getResourceId() != 0) {
- this.fileName = generateFileNameFromResourceId(this.getResourceId());
+ if (this.getInputStream() != null) {
+ this.shouldAutoGenerateName = false;
+ throw new IllegalArgumentException("Please set the fileName. The fileName cannot be auto-generated if the source of the file is an InputStream");
+ } else {
+ this.shouldAutoGenerateName = true;
+ if (this.getFileData() != null) {
+ this.fileName = generateFileNameFromData(this.getFileData());
+ } else if (this.getUri() != null) {
+ this.fileName = generateFileNameFromUri(this.getUri());
+ } else if (this.getResourceId() != 0) {
+ this.fileName = generateFileNameFromResourceId(this.getResourceId());
+ }
}
}
}
@@ -200,6 +228,54 @@ public class SdlFile{
}
/**
+ * Sets the file's input stream
+ * @param inputStream The stream of data that the FileManager will read from
+ */
+ public void setInputStream(InputStream inputStream) {
+ this.inputStream = inputStream;
+ }
+
+ /**
+ * Gets the file's input stream
+ * @return The stream of data that the FileManager will read from
+ */
+ public InputStream getInputStream() {
+ return inputStream;
+ }
+
+ /**
+ * Sets the data offset for the input stream
+ * @param offset The data offset in bytes, a value of zero is used to indicate data starting from the beginning of the stream
+ */
+ public void setOffset(int offset) {
+ this.offset = offset;
+ }
+
+ /**
+ * Gets the data offset for the input stream
+ * @return The data offset in bytes, a value of zero is used to indicate data starting from the beginning of the stream
+ */
+ public int getOffset() {
+ return offset;
+ }
+
+ /**
+ * Sets the number of bytes to be read from the stream
+ * @param length The number of bytes to be read from the stream
+ */
+ public void setLength(int length) {
+ this.length = length;
+ }
+
+ /**
+ * Gets the number of bytes to be read from the stream
+ * @return The number of bytes to be read from the stream
+ */
+ public int getLength() {
+ return length;
+ }
+
+ /**
* Sets the type of the file
* @param fileType a FileType enum value representing the type of the file
*/
@@ -318,10 +394,13 @@ public class SdlFile{
result += ((getName() == null) ? 0 : Integer.rotateLeft(getName().hashCode(), 1));
result += ((getUri() == null) ? 0 : Integer.rotateLeft(getUri().hashCode(), 2));
result += ((getFileData() == null) ? 0 : Integer.rotateLeft(getFileData().hashCode(), 3));
- result += ((getType() == null) ? 0 : Integer.rotateLeft(getType().hashCode(), 4));
- result += Integer.rotateLeft(Boolean.valueOf(isStaticIcon()).hashCode(), 5);
- result += Integer.rotateLeft(Boolean.valueOf(isPersistent()).hashCode(), 6);
- result += Integer.rotateLeft(Integer.valueOf(getResourceId()).hashCode(), 7);
+ result += ((getInputStream() == null) ? 0 : Integer.rotateLeft(getInputStream().hashCode(), 4));
+ result += Integer.rotateLeft(getOffset(), 5);
+ result += Integer.rotateLeft(getLength(), 6);
+ result += ((getType() == null) ? 0 : Integer.rotateLeft(getType().hashCode(), 7));
+ result += Integer.rotateLeft(Boolean.valueOf(isStaticIcon()).hashCode(), 8);
+ result += Integer.rotateLeft(Boolean.valueOf(isPersistent()).hashCode(), 9);
+ result += Integer.rotateLeft(Integer.valueOf(getResourceId()).hashCode(), 10);
return result;
}
diff --git a/base/src/main/java/com/smartdevicelink/managers/file/BaseFileManager.java b/base/src/main/java/com/smartdevicelink/managers/file/BaseFileManager.java
index 6056cdbf6..5247f2c58 100644
--- a/base/src/main/java/com/smartdevicelink/managers/file/BaseFileManager.java
+++ b/base/src/main/java/com/smartdevicelink/managers/file/BaseFileManager.java
@@ -452,18 +452,19 @@ abstract class BaseFileManager extends BaseSubManager {
/**
* Helper method to take InputStream and turn it into byte array
* @param is valid InputStream
+ * @param offset the start offset at which the data is read
+ * @param length the maximum number of bytes to read
* @return Resulting byte array
*/
- byte[] contentsOfInputStream(InputStream is){
+ byte[] contentsOfInputStream(InputStream is, int offset, int length){
if(is == null){
return null;
}
try{
ByteArrayOutputStream os = new ByteArrayOutputStream();
- final int bufferSize = 4096;
- final byte[] buffer = new byte[bufferSize];
+ final byte[] buffer = new byte[length];
int available;
- while ((available = is.read(buffer)) >= 0) {
+ while ((available = is.read(buffer, offset, length)) >= 0) {
os.write(buffer, 0, available);
}
return os.toByteArray();
@@ -473,4 +474,13 @@ abstract class BaseFileManager extends BaseSubManager {
}
}
+ /**
+ * Helper method to take InputStream and turn it into byte array
+ * @param is valid InputStream
+ * @return Resulting byte array
+ */
+ byte[] contentsOfInputStream(InputStream is){
+ final int bufferSize = 4096;
+ return contentsOfInputStream(is, 0, bufferSize);
+ }
}