All Downloads are FREE. Search and download functionalities are using the official Maven repository.

main.java.com.amazonaws.mobileconnectors.s3.transfermanager.PersistableTransfer Maven / Gradle / Ivy

Go to download

The AWS Android SDK for Amazon S3 module holds the client classes that are used for communicating with Amazon Simple Storage Service

There is a newer version: 2.77.0
Show newest version
/*
 * Copyright 2013-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */
package com.amazonaws.mobileconnectors.s3.transfermanager;

import static com.amazonaws.util.StringUtils.UTF8;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

import com.amazonaws.services.s3.model.ResponseHeaderOverrides;
import com.amazonaws.util.json.AwsJsonReader;
import com.amazonaws.util.json.JsonUtils;

/**
 * Abstract base class for the information of a pausible upload or download; such
 * information can be used to resume the upload or download later on, and can be
 * serialized/deserialized for persistence purposes.
 */
public abstract class PersistableTransfer {

    /**
     * Returns the serialized representation of the paused transfer state.
     */
    public abstract String serialize();

    /**
     * Writes the serialized representation of the paused transfer state to the
     * given OutputStream. Caller of this method should explicitly
     * close the OutputStream.
     */
    public final void serialize(OutputStream out) throws IOException {
        out.write(serialize().getBytes(UTF8));
        out.flush();
    }

    /**
     * Returns the deserialized transfer state of the given serialized
     * representation. Caller of this method should explicitly close the
     * InputStream.
     *
     * @throws UnsupportedOperationException
     *             if the paused transfer type extracted from the serialized
     *             representation is not supported.
     */
    @SuppressWarnings("unchecked")
    public static  T deserializeFrom(InputStream in) {
        String type = null;
        String bucketName = null;
        String key = null;
        String file = null;

        String multipartUploadId = null;
        long partSize = -1;
        long mutlipartUploadThreshold = -1;

        String versionId = null;
        long[] range = new long[] {
                -1, -1
        };
        ResponseHeaderOverrides responseHeaders = null;
        boolean isRequesterPays = false;

        AwsJsonReader reader = JsonUtils
                .getJsonReader(new BufferedReader(new InputStreamReader(in)));
        try {

            reader.beginObject();
            while (reader.hasNext()) {
                String name = reader.nextName();
                if (name.equals("pauseType")) {
                    type = reader.nextString();
                } else if (name.equals("bucketName")) {
                    bucketName = reader.nextString();
                } else if (name.equals("key")) {
                    key = reader.nextString();
                } else if (name.equals("file")) {
                    file = reader.nextString();
                }
                // upload properties
                else if (name.equals("multipartUploadId")) {
                    multipartUploadId = reader.nextString();
                } else if (name.equals("partSize")) {
                    partSize = Long.parseLong(reader.nextString());
                } else if (name.equals("mutlipartUploadThreshold")) {
                    mutlipartUploadThreshold = Long.parseLong(reader.nextString());
                }
                // download properties
                else if (name.equals("versionId")) {
                    versionId = reader.nextString();
                } else if (name.equals("range")) {
                    reader.beginArray();
                    range[0] = Long.parseLong(reader.nextString());
                    range[1] = Long.parseLong(reader.nextString());
                    reader.endArray();
                } else if (name.equals("responseHeaders")) {
                    responseHeaders = new ResponseHeaderOverrides();
                    reader.beginObject();
                    while (reader.hasNext()) {
                        String n = reader.nextName();
                        if (n.equals("contentType")) {
                            responseHeaders.setContentType(reader.nextString());
                        } else if (n.equals("contentLanguage")) {
                            responseHeaders.setContentLanguage(reader.nextString());
                        } else if (n.equals("expires")) {
                            responseHeaders.setExpires(reader.nextString());
                        } else if (n.equals("cacheControl")) {
                            responseHeaders.setCacheControl(reader.nextString());
                        } else if (n.equals("contentDisposition")) {
                            responseHeaders.setContentDisposition(reader.nextString());
                        } else if (n.equals("contentEncoding")) {
                            responseHeaders.setContentEncoding(reader.nextString());
                        } else {
                            reader.skipValue();
                        }
                    }
                    reader.endObject();
                } else if (name.equals("isRequesterPays")) {
                    isRequesterPays = Boolean.parseBoolean(reader.nextString());
                } else {
                    reader.skipValue();
                }
            }
            reader.endObject();
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }

        if (PersistableDownload.TYPE.equals(type)) {
            return (T) new PersistableDownload(bucketName, key, versionId, range, responseHeaders,
                    isRequesterPays, file);
        } else if (PersistableUpload.TYPE.equals(type)) {
            return (T) new PersistableUpload(bucketName, key, file, multipartUploadId, partSize,
                    mutlipartUploadThreshold);
        } else {
            throw new UnsupportedOperationException(
                    "Unsupported paused transfer type: " + type);
        }
    }

    /**
     * Returns the deserialized transfer state of the given serialized
     * representation.
     *
     * @throws UnsupportedOperationException
     *             if the paused transfer type extracted from the serialized
     *             representation is not supported.
     */
    public static  T deserializeFrom(
            String serialized) {
        if (serialized == null)
            return null;
        ByteArrayInputStream byteStream = new ByteArrayInputStream(
                serialized.getBytes(UTF8));
        try{
            return deserializeFrom(byteStream);
        }finally{
            try { byteStream.close(); } catch(IOException ioe) { } ;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy