0

So I am using the Java SDK solution to download CloudFiles to a local host: here

I am able to find a test container, list what is in it and access/ write the files. But when I write them they don't have any of the data from the original files. They are just husks (0KB). They aren't big files and I seem to have a problem when I call the method openStream() to create an InputStream... I call read() on it and it immediately gives me back -1 indicating it has come to the end of the file... no idea why. Here is some code to help explain:

ObjectList object = objectApi.list();
    for (int x = 0; x < testContainer.getObjectCount(); x++){
        SwiftObject object2 = object.get(x);

        InputStream inputStream = object2.getPayload().openStream();
        System.out.println(inputStream.read()); <-- giving me -1
        System.out.println(inputStream.available()); <-- giving me 0
        String objectName = object2.getName();
        System.out.println(objectName);
}

This is just error checking. Everything seems to work with my TestContainer... it will show me all of the folders/files within and when I create:

Payload pl = object2.getPayload();

System.out.println(pl.getMetaData());

it shows me that the contentLength matches (well almost matches) the size of the file... so I THINK my SwiftObject is being built fine with object.get(x)....

No idea WHY I am returning zero bytes when I call read(), though... it seems the .openStream isn't working... any help is greatly appreciated!!

And just in case it is somehow an issue with my other code, here is that:

InputStream inputStream = object2.getPayload().openStream();
        String objectName = object2.getName();
        System.out.println(objectName);
        if (objectName.contains(".")){
            File newFile = new File("C:\\Users\\-----\\temp\\" + objectName);
            newFile.getParentFile().mkdirs();
            FileOutputStream outputStream = new FileOutputStream(newFile);
            byte[] aByte = IOUtils.toByteArray(inputStream);
            IOUtils.write(aByte, outputStream); //also tried IOUtils.copy(inputStream, outputStream);
            inputStream.close();
            outputStream.close();
        }
drawnitsud
  • 55
  • 11
  • What is your jclouds version? Have you tried to replicate the failure with a different tool like curl? – zacksh Jul 15 '16 at 11:51

1 Answers1

0

Okay, so for anyone having this problem, the solution was that when I made the code:

ObjectList object = objectApi.list();
for (int x = 0; x <testContainer.getObjectCount(); x++){
SwiftObject object2 = object.get(x);

This doesn't get the actual object, just a list of the objects and out of that, you get the name. To solve my problem and to get the object with its name, keep the above code but add:

SwiftObject realObject = object.get(Object2.getName())

This will give you the ACTUAL object. Proceed from there. Hope this helps others....

drawnitsud
  • 55
  • 11