I am trying to upload a HashMap to GCS. The first thing I am doing is saving my HashMap to internal memory:
HashMap<Integer, Integer> testHash = new HashMap<Integer, Integer>();
private void writeHashMap() {
String filename = "writeTest";
try {
FileOutputStream f = openFileOutput(filename, Context.MODE_PRIVATE);
ObjectOutputStream s = new ObjectOutputStream(f);
s.writeObject(testHash);
s.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Using the following code I am able to create am object name "myUploadTest" in my bucket "mybucket":
public void uploadHashMap() throws Exception {
// Authorization.
if (googleCredential == null) {
googleCredential = authorize();
}
// Set up global Storage instance.
client = new Storage.Builder(httpTransport, JSON_FACTORY, googleCredential).setApplicationName(APPLICATION_NAME).build();
FileInputStream f = openFileInput("writeTest");
try {
String contentType = URLConnection.guessContentTypeFromStream(f);
InputStreamContent content = new InputStreamContent(contentType, f);
Storage.Objects.Insert insert = client.objects().insert("mybucket", null, content);
insert.setName("myUploadTest");
insert.execute();
} finally {
f.close();
}
}
The trouble begins when I try to download the object. This is what I am attempting:
public void downloadHashMap() throws Exception {
// Authorization.
if (googleCredential == null) {
googleCredential = authorize();
}
// Set up global Storage instance.
client = new Storage.Builder(httpTransport, JSON_FACTORY, googleCredential).setApplicationName(APPLICATION_NAME).build();
String fileName = "writeTest";
Storage.Objects.Get get = client.objects().get("mybucket", fileName);
FileOutputStream f = openFileOutput(fileName);
try {
get.executeAndDownloadTo(f);
} catch (Exception e) {
e.printStackTrace();
}
f.close();
}
But I am unsure as to how to input the raw data from the FileOutputStream back to my original HashMap "testHash". Actually I'm unsure if the correct data is even being saved to my bucket, when I download the object and view in Notepad++ it is not readable.
Any help would be greatly appreciated!!
Jenny
Aucun commentaire:
Enregistrer un commentaire