-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathDownloadFeatureFileSample.java
More file actions
82 lines (72 loc) · 3.41 KB
/
DownloadFeatureFileSample.java
File metadata and controls
82 lines (72 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package http;
import com.envisioniot.enos.iot_http_sdk.HttpConnection;
import com.envisioniot.enos.iot_http_sdk.SessionConfiguration;
import com.envisioniot.enos.iot_http_sdk.StaticDeviceCredential;
import com.envisioniot.enos.iot_http_sdk.file.FileCategory;
import com.envisioniot.enos.iot_http_sdk.file.IFileCallback;
import com.envisioniot.enos.iot_mqtt_sdk.core.exception.EnvisionException;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* @author :charlescai
* @date :2020-04-21
*/
public class DownloadFeatureFileSample {
// EnOS HTTP Broker URL, which can be obtained from Environment Information page in EnOS Console
static final String BROKER_URL = "https://broker_url/";
// Device credentials, which can be obtained from Device Details page in EnOS Console
static final String PRODUCT_KEY = "productKey";
static final String DEVICE_KEY = "deviceKey";
static final String DEVICE_SECRET = "deviceSecret";
public static void main(String[] args) throws EnvisionException {
// construct a static device credential via ProductKey, DeviceKey and DeviceSecret
StaticDeviceCredential credential = new StaticDeviceCredential(
PRODUCT_KEY, DEVICE_KEY, DEVICE_SECRET);
SessionConfiguration configuration = SessionConfiguration.builder()
.lifetime(30_000)
.build();
// construct a http connection
HttpConnection connection = new HttpConnection.Builder(BROKER_URL, credential)
.sessionConfiguration(configuration)
.build();
// fileUri is an enos scheme file uri
String fileUri = "enos-connect://xxx.txt";
int bufferLength = 1024;
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
InputStream inputStream = connection.downloadFile(fileUri, FileCategory.FEATURE);
byte[] buffer = new byte[bufferLength];
int len;
while ((len = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
System.out.println(outputStream);
} catch (EnvisionException | IOException e) {
e.printStackTrace();
}
// Asynchronously call the file download request
try {
connection.downloadFileAsync(fileUri, FileCategory.FEATURE, new IFileCallback() {
@Override
public void onResponse(InputStream inputStream) throws IOException {
System.out.println("download feature file asynchronously");
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
byte[] buffer = new byte[bufferLength];
int len;
while ((len = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
System.out.println(outputStream);
}
}
@Override
public void onFailure(Exception failure) {
failure.printStackTrace();
}
}
);
} catch (EnvisionException e) {
e.printStackTrace();
}
}
}