com.talis.storage.s3.ExternalizableS3Object.java Source code

Java tutorial

Introduction

Here is the source code for com.talis.storage.s3.ExternalizableS3Object.java

Source

/*
 * Copyright 2010 Talis Information Ltd
 * 
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 * 
 *        http://www.apache.org/licenses/LICENSE-2.0
 * 
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License 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.talis.storage.s3;

import java.io.ByteArrayInputStream;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.jets3t.service.S3ServiceException;
import org.jets3t.service.acl.AccessControlList;
import org.jets3t.service.model.S3Object;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ExternalizableS3Object extends S3Object implements Externalizable {

    private static final transient Logger LOG = LoggerFactory.getLogger(ExternalizableS3Object.class);

    @Override
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {

        this.setKey(in.readUTF());
        this.setBucketName(in.readUTF());
        this.setAcl((AccessControlList) in.readObject());
        this.setMetadataComplete(in.readBoolean());
        this.addAllMetadata((Map) in.readObject());
        int length = in.readInt();
        byte[] entity = new byte[length];
        in.readFully(entity);
        this.setDataInputStream(new ByteArrayInputStream(entity));
    }

    @Override
    public void writeExternal(ObjectOutput out) throws IOException {
        out.writeUTF(getKey());
        out.writeUTF(getBucketName());
        out.writeObject(getAcl());
        out.writeBoolean(isMetadataComplete());
        out.writeObject(getMetadataMap());
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        try {
            IOUtils.copy(this.getDataInputStream(), buffer);
        } catch (S3ServiceException e) {
            LOG.error("Error copying entity stream", e);
            throw new IOException("Error serializing object", e);
        }
        out.writeInt(buffer.toByteArray().length);
        out.write(buffer.toByteArray());
    }

}