Package com.bradmcevoy.io

Examples of com.bradmcevoy.io.BufferingOutputStream


        return list;
    }

    public OutputStream createOutputStream( long offset ) throws IOException {
        log.debug( "createOutputStream: " + offset );
        final BufferingOutputStream out = new BufferingOutputStream( 50000 );
        if( r instanceof ReplaceableResource ) {
            log.debug( "resource is replaceable" );
            final ReplaceableResource rr = (ReplaceableResource) r;
            Runnable runnable = new Runnable() {

                public void run() {
                    try {
                        rr.replaceContent(out.getInputStream(), out.getSize());
                    } catch (BadRequestException ex) {
                        throw new RuntimeException(ex);
                    } catch (ConflictException ex) {
                        throw new RuntimeException(ex);
                    } catch (NotAuthorizedException ex) {
                        throw new RuntimeException(ex);
                    }
                }
            };
            out.setOnClose( runnable );
            return out;
        } else {
            CollectionResource col;
            col = getParent();
            if( col == null ) {
                throw new IOException( "parent not found" );
            } else if( col instanceof PutableResource ) {
                final PutableResource putableResource = (PutableResource) col;
                final String newName = path.getName();
                Runnable runnable = new Runnable() {

                    public void run() {
                        try {
                            putableResource.createNew( newName, out.getInputStream(), out.getSize(), null );
                        } catch( BadRequestException ex ) {
                            throw new RuntimeException( ex );
                        } catch( NotAuthorizedException ex ) {
                            throw new RuntimeException( ex );
                        } catch( ConflictException ex ) {
                            throw new RuntimeException( ex );
                        } catch( IOException ex ) {
                            throw new RuntimeException( ex );
                        }
                    }
                };
                out.setOnClose( runnable );
                return out;
            } else {
                throw new IOException( "folder doesnt support PUT, and the resource is not replaceable" );
            }
        }
View Full Code Here


    public InputStream createInputStream( long offset ) throws IOException {
        if( r instanceof GetableResource ) {
            GetableResource gr = (GetableResource) r;
            String ct = gr.getContentType( null );
            BufferingOutputStream out = new BufferingOutputStream( 50000 );
            try {
                gr.sendContent( out, null, null, ct );
                out.close();
                return out.getInputStream();
            } catch( BadRequestException ex ) {
                log.warn( "bad request", ex );
                return null;
            } catch( NotAuthorizedException ex ) {
                log.warn( "not authorising", ex );
View Full Code Here

            if( canCompress( r, contentType, request.getAcceptEncodingHeader() ) ) {
                log.trace( "respondContent: compressable" );

                // get the zipped content before sending so we can determine its
                // compressed size
                BufferingOutputStream tempOut = new BufferingOutputStream( maxMemorySize );
                try {
                    OutputStream gzipOut = new GZIPOutputStream( tempOut );
                    r.sendContent( gzipOut, null, params, contentType );
                    gzipOut.flush();
                    gzipOut.close();
                    tempOut.flush();
                } catch( Exception ex ) {
                    tempOut.deleteTempFileIfExists();
                    throw new RuntimeException( ex );
                } finally {
                    FileUtils.close( tempOut );
                }

                log.trace( "respondContent-compressed: " + resource.getClass() );
                setRespondContentCommonHeaders( response, resource, Response.Status.SC_OK, request.getAuthorization() );
                response.setContentEncodingHeader( Response.ContentEncoding.GZIP );
                response.setVaryHeader( "Accept-Encoding" );
                Long contentLength = tempOut.getSize();
                response.setContentLengthHeader( contentLength );
                response.setContentTypeHeader( contentType );
                cacheControlHelper.setCacheControl( r, response, request.getAuthorization() );
                InputStream in = tempOut.getInputStream();
                try {
                    StreamUtils.readTo( in, response.getOutputStream() );
                } catch( ReadingException ex ) {
                    throw new RuntimeException( ex );
                } catch( WritingException ex ) {
View Full Code Here

      MetaFileMaker.MetaData metaData;
      if (r instanceof ZSyncResource) {
        ZSyncResource zr = (ZSyncResource) r;
        metaData = zr.getZSyncMetaData();
      } else {
        BufferingOutputStream bufOut = new BufferingOutputStream(maxMemorySize);
        try {
          r.sendContent(bufOut, null, params, contentType);
          bufOut.flush();
        } catch (Exception ex) {
          bufOut.deleteTempFileIfExists();
          throw new RuntimeException(ex);
        } finally {
          StreamUtils.close(bufOut);
        }
        InputStream in = bufOut.getInputStream();
        try {
          metaData = metaFileMaker.make(realPath, blocksize, fileLength, r.getModifiedDate(), in);
        } finally {
          StreamUtils.close(in);
        }
View Full Code Here

                log.trace("sending content with known content length: " + contentLength);
                response.setContentLengthHeader(contentLength);
                sendContent(request, response, (GetableResource) resource, params, null, ct);
            } else {
                log.trace("buffering content...");
                BufferingOutputStream tempOut = new BufferingOutputStream(maxMemorySize);
                try {
                    ((GetableResource) resource).sendContent(tempOut, null, params, ct);
                    tempOut.close();
                } catch (IOException ex) {
                    tempOut.deleteTempFileIfExists();
                    throw new RuntimeException("Exception generating buffered content", ex);
                }
                Long bufContentLength = tempOut.getSize();
                if (contentLength != null) {
                    if (!contentLength.equals(bufContentLength)) {
                        throw new RuntimeException("Lengthd dont match: " + contentLength + " != " + bufContentLength);
                    }
                }
                log.trace("sending buffered content...");
                response.setContentLengthHeader(bufContentLength);
                InputStream in = tempOut.getInputStream();
                try {
                    StreamUtils.readTo(in, response.getOutputStream());
                } catch (ReadingException ex) {
                    throw new RuntimeException(ex);
                } catch (WritingException ex) {
View Full Code Here

TOP

Related Classes of com.bradmcevoy.io.BufferingOutputStream

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.