Reputation: 21
I am having a problem with writing bytes of a retrieved ObjectInputStream
object. I need this to be able to access the file in the format.
This is the code I am currently using:
public ObjectInputStream serve(String fileName) throws IOException,
ClassNotFoundException {
GcsFilename gcsFileName = getFileName(fileName, defaultbucketName);
int fileSize = (int) gcsService.getMetadata(gcsFileName).getLength();
GcsInputChannel readChannel = null;
readChannel = gcsService.openPrefetchingReadChannel(gcsFileName, 0,
fileSize);
try (ObjectInputStream oin = new ObjectInputStream(
Channels.newInputStream(readChannel))) {
return oin;
}
}
When I run the code it fails at the line ObjectInputStream oin = new ObjectInputStream(Channels.newInputStream(readChannel))
with an error, e.g.:
java.io.StreamCorruptedException: invalid stream header: FFD8FFE0
However there are no problems when I emmit the stream to a HTTPRESPONSE
object.
Upvotes: 2
Views: 585
Reputation: 2758
You are likely attempting to access a file which hasn't been written by a ObjectOutputStream
, which contains a special header.
Therefore, to access the file you should either rewrite its contents with the Google Cloud Storage writing APIs or with a dummy Java program like this one:
FileOutputStream fos = new FileOutputStream("test.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject("your content");
oos.close();
Later, upload test.txt
to your GCS bucket.
Upvotes: 2