Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.storemate.shared;
  
  import java.util.Arrays;
  
Simple read-only wrapper around basic in-heap byte array, used for buffering.
 
 public abstract class ByteContainer
 {
     public final static ByteContainer emptyContainer() {
         return .;
     }
 
     public final static ByteContainer simple(byte[] bytes) {
         return new SimpleContainer(bytes, 0, bytes.length);
     }
     
     public final static ByteContainer simple(byte[] bytesint offsetint len)
     {
         if (len == 0) {
             return emptyContainer();
         }
         // Let's do some sanity checks, saves us debugging
         if (bytes == nullthrow new IllegalArgumentException("Null 'bytes'");
         final int arrayLen = bytes.length;
         if (offset < 0 || len < 0 || (offset + len) > arrayLen) {
             throw new IllegalArgumentException("Illegal offset/length ("+offset+"/"+len
                     +"): extend beyond start/end of array of "+arrayLen);
         }
         return new SimpleContainer(bytesoffsetlen);
     }
    
    
Accessor for checking how many bytes are contained.
 
     public abstract int byteLength();

    
Simple accessor only to be used by tests (real code should use callbacks or bulk access)
 
     public abstract byte get(int index);
    
    
Factory method for creating a view of contents of this container.

Parameters:
offset Offset in the original buffer; has to be between 0 and length - 1 (inclusive)
length Length of the slice to create: must not extend past end of this instance
Returns:
New container with the view, if necessary; or this instance if no change
 
     public abstract ByteContainer view(int offsetint length);
     
     public abstract <T> T withBytes(WithBytesCallback<T> cb);
 
     public abstract <T> T withBytes(WithBytesCallback<T> cbint offsetint length);
    
    

Parameters:
buffer Buffer into which copy bytes
offset Offset to copy bytes at
Returns:
Offset after copying the bytes, that is: offset + byteLength()
 
     public abstract int getBytes(byte[] bufferint offset);
     
     public abstract byte[] asBytes();
 
     public abstract int hash(BlockHasher32 hasherint seed);
     
     public abstract void writeBytes(OutputStream outthrows IOException;
 
     public abstract void writeBytes(OutputStream outint offsetint lengththrows IOException;
     
     private final static class NoBytesContainer extends ByteContainer
     {
         final static byte[] NO_BYTES = new byte[0];
         
         final static NoBytesContainer instance = new NoBytesContainer();
         
         private NoBytesContainer() { }
 
         @Override public int byteLength() {
             return 0;
         }
 
         @Override public byte get(int index) {
             throw new IllegalArgumentException("Bad offset ("+index+"); this length is 0");
         }
         
         @Override public int getBytes(byte[] bufferint offset) {
             return offset;
        }
        @Override public byte[] asBytes() { return ; }
        @Override public int hash(BlockHasher32 hasherint seed) {
            return hasher.hash(seed);
        }
        
        @Override public void writeBytes(OutputStream out)  { }
        @Override
        public void writeBytes(OutputStream outint offsetint length) {
        	if (offset != 0 || length != 0) {
                throw new IllegalArgumentException("Bad offset/length ("+offset+"/"+length+"); this length is 0");
        	}
        }
        @Override public <T> T withBytes(WithBytesCallback<T> cb) {
            return cb.withBytes(, 0, 0);
        }
        
        @Override public <T> T withBytes(WithBytesCallback<T> cbint offsetint length) {
            if (offset == 0 && length == 0) {
                return cb.withBytes(, 0, 0);
            }
            throw new IllegalArgumentException("Bad offset/length ("+offset+"/"+length+"); this length is 0");
        }
        
        @Override
        public ByteContainer view(int offsetint length) {
            if (offset == 0 && length == 0) {
                return this;
            }
            throw new IllegalArgumentException("Bad offset/length ("+offset+"/"+length+"); this length is 0");
        }        
    }
    private final static class SimpleContainer extends ByteContainer
    {
        private final byte[] _data;
        private final int _offset_length;
        
        SimpleContainer(byte[] bytesint offsetint len) {
             = bytes;
             = offset;
             = len;
        }
        @Override public int byteLength() {
            return ;
        }
        @Override public byte get(int index) {
            if (index < 0 || index >= ) {
                throw new IllegalArgumentException("Bad offset ("+index+"); this length is "+);
            }
            return [ + index];
        }
        
        @Override public int getBytes(byte[] bufferint offset) {
            System.arraycopy(bufferoffset);
            return (offset + );
        }
        @Override
        public byte[] asBytes() {
            if ( == 0) {
                return Arrays.copyOf();
            }
            return Arrays.copyOfRange( + );
        }
        
        @Override public int hash(BlockHasher32 hasherint seed) {
            return hasher.hash(seed);
        }
        
        @Override public void writeBytes(OutputStream outthrows IOException {
            out.write();
        }
        @Override
        public void writeBytes(OutputStream outint offsetint lengththrows IOException
        {
        	if (offset < 0 || (offset+length) > ) {
                throw new IllegalArgumentException("Bad offset/length ("+offset+"/"+length+"); this length is "+);
            }
            out.write( + offsetlength);
        }
        
        @Override public <T> T withBytes(WithBytesCallback<T> cb) {
            return cb.withBytes();
        }
        @Override public <T> T withBytes(WithBytesCallback<T> cbint offsetint length) {
            if (offset == 0 && length == ) {
                return cb.withBytes();
            }
            if (offset < 0 || (offset+length) > ) {
                throw new IllegalArgumentException("Bad offset/length ("+offset+"/"+length+"); this length is "+);
            }
            return cb.withBytes( + offsetlength);
        }
        
        @Override
        public ByteContainer view(int offsetint length) {
            if (offset == 0 && length == ) {
                return this;
            }
            if (offset < 0 || (offset+length) > ) {
                throw new IllegalArgumentException("Bad offset/length ("+offset+"/"+length+"); this length is "+);
            }
            return new SimpleContainer( + offsetlength);
        }
    }
New to GrepCode? Check out our FAQ X