001    package railo.commons.io.res.type.s3;
002    
003    import java.io.IOException;
004    import java.io.OutputStream;
005    import java.net.SocketException;
006    
007    import railo.commons.lang.ExceptionUtil;
008    import railo.commons.lang.StringUtil;
009    
010    public final class S3ResourceOutputStream extends OutputStream {
011            
012            private final S3 s3;
013            
014            private final String contentType="application";
015            private final String bucketName;
016            private final String objectName;
017            private final int acl;
018    
019            private TemporaryStream ts;
020            
021            public S3ResourceOutputStream(S3 s3,String bucketName,String objectName,int acl) {
022                    this.s3=s3;
023                    this.bucketName=bucketName;
024                    this.objectName=objectName;
025                    this.acl=acl;
026                    
027                    ts = new TemporaryStream();
028            }
029            
030            /**
031             *
032             * @see java.io.OutputStream#close()
033             */
034            public void close() throws IOException {
035                    ts.close();
036                    
037                    //InputStream is = ts.getInputStream();
038                    try {
039                            s3.put(bucketName, objectName, acl, new TemporaryStreamRequestEntity(ts,contentType));
040                    } 
041    
042                    catch (SocketException se) {
043                            String msg = StringUtil.emptyIfNull(se.getMessage());
044                            if(StringUtil.indexOfIgnoreCase(msg, "Socket closed")==-1)
045                                    throw se;
046                    }
047                    catch (Exception e) {
048                            throw ExceptionUtil.toIOException(e);
049                    }
050            }
051    
052            /**
053             *
054             * @see java.io.OutputStream#flush()
055             */
056            public void flush() throws IOException {
057                    ts.flush();
058            }
059    
060            /**
061             *
062             * @see java.io.OutputStream#write(byte[], int, int)
063             */
064            public void write(byte[] b, int off, int len) throws IOException {
065                    ts.write(b, off, len);
066            }
067    
068            /**
069             *
070             * @see java.io.OutputStream#write(byte[])
071             */
072            public void write(byte[] b) throws IOException {
073                    ts.write(b);
074            }
075    
076            /**
077             *
078             * @see java.io.OutputStream#write(int)
079             */
080            public void write(int b) throws IOException {
081                    ts.write(b);
082            }
083    }