Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.clustermate.client.call;
  
  import java.io.*;
  
Helper class used for creating one of standard PutContentProvider implementations.
 
 public class PutContentProviders
 {
     public static PutContentProvider forFile(File flong length) {
         return new FileBacked(flength);
     }
 
     public static PutContentProvider forBytes(byte[] bytes) {
         return forBytes(bytes, 0, bytes.length);
     }
 
     public static PutContentProvider forBytes(byte[] bytesint offsetint len) {
         return new ByteBacked(ByteContainer.simple(bytesoffsetlen));
     }

    
Intermediate base class used for building actual PutContentProvider instances.
 
     public abstract static class ProviderBase
         implements PutContentProvider
     {
         protected final Compression _existingCompression;
         protected final long _uncompressedLength;
 
         protected final AtomicInteger _contentHash = new AtomicInteger(.);
 
         protected ProviderBase() {
              = null;
              = 0L;
         }
         
         protected ProviderBase(Compression complong uncompressedLength) {
              = comp;
              = uncompressedLength;
         }

        
Default implementation does not require any clean up, so implementation is empty.
 
         @Override
         public void release() { }
         
         @Override
         public int getContentHash() {
             return .get();
         }
 
         @Override
         public void setContentHash(int hash) {
             // Should get same value always, but just to make sure we never
             // change value, or overwrite with "no hash"
             .compareAndSet(.hash);
         }
 
         @Override
         public Compression getExistingCompression() {
             return ;
         }
 
         @Override
         public long uncompressedLength() {
             return ;
         }
     }
 
     /*
     /**********************************************************************
     /* Simple standard implementation for byte[] backed
     /**********************************************************************
      */

    
Simple PutContentProvider implementation that is backed by a raw byte array.
 
     protected static class ByteBacked extends ProviderBase
     {
         protected final ByteContainer _bytes;
 
         public ByteBacked(ByteContainer data) {
             super();
              = data;
         }
 
         public ByteBacked(ByteContainer data,
                 Compression complong originalLength) {
            super(comporiginalLength);
             = data;
        }
        @Override
        public long length() {
            return (long.byteLength();
        }
        
        @Override
        public ByteContainer contentAsBytes() {
            return ;
        }
        @Override
        public File contentAsFile() {
            return null;
        }
        
        @Override
        public InputStream contentAsStream() {
            return null;
        }
    }
    
    /*
    /**********************************************************************
    /* Simple standard implementations for providers
    /**********************************************************************
     */
    
    
Simple PutContentProvider implementation that is backed by a File.
    protected static class FileBacked
        extends ProviderBase
    {
        protected final File _file;
        protected final long _length;
        public FileBacked(File filelong length) {
            this(filelengthnull, 0L);
        }
        public FileBacked(File filelong length,
                Compression existingCompressionlong uncompressedLength)
        {
            super(existingCompressionuncompressedLength);
             = file;
             = length;
        }
        @Override
        public long length() {
            return ;
        }
        @Override
        public ByteContainer contentAsBytes() {
            return null;
        }
        @Override
        public File contentAsFile() {
            return ;
        }
        
        @Override
        public InputStream contentAsStream() {
            return null;
        }
    }
New to GrepCode? Check out our FAQ X