Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2011 the original author or authors. See the notice.md file distributed with this work for additional information regarding copyright ownership. 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 org.iq80.leveldb.util;
 
 
 import java.util.Map;
 
 public final class Slices
 {
     public static Slice readLengthPrefixedBytes(SliceInput sliceInput)
     {
         int length = VariableLengthQuantity.readVariableLengthInt(sliceInput);
         return sliceInput.readBytes(length);
     }
 
     public static void writeLengthPrefixedBytes(SliceOutput sliceOutputSlice value)
     {
         VariableLengthQuantity.writeVariableLengthInt(value.length(), sliceOutput);
         sliceOutput.writeBytes(value);
     }

    
A buffer whose capacity is 0.
 
     public static final Slice EMPTY_SLICE = new Slice(0);
 
     private Slices()
     {
     }
 
     public static Slice ensureSize(Slice existingSliceint minWritableBytes)
     {
         if (existingSlice == null) {
             existingSlice = ;
         }
 
         if (minWritableBytes <= existingSlice.length()) {
             return existingSlice;
         }
 
         int newCapacity;
         if (existingSlice.length() == 0) {
             newCapacity = 1;
         }
         else {
             newCapacity = existingSlice.length();
         }
         int minNewCapacity = existingSlice.length() + minWritableBytes;
         while (newCapacity < minNewCapacity) {
             newCapacity <<= 1;
         }
 
         Slice newSlice = Slices.allocate(newCapacity);
         newSlice.setBytes(0, existingSlice, 0, existingSlice.length());
         return newSlice;
     }
 
     public static Slice allocate(int capacity)
     {
         if (capacity == 0) {
             return ;
         }
         return new Slice(capacity);
     }
 
     public static Slice wrappedBuffer(byte[] array)
     {
         if (array.length == 0) {
             return ;
         }
         return new Slice(array);
     }
    public static Slice copiedBuffer(ByteBuffer sourceint sourceOffsetint length)
    {
        Preconditions.checkNotNull(source"source is null");
        int newPosition = source.position() + sourceOffset;
        return copiedBuffer((ByteBuffersource.duplicate().order(.).clear().limit(newPosition + length).position(newPosition));
    }
    public static Slice copiedBuffer(ByteBuffer source)
    {
        Preconditions.checkNotNull(source"source is null");
        Slice copy = allocate(source.limit() - source.position());
        copy.setBytes(0, source.duplicate().order(.));
        return copy;
    }
    public static Slice copiedBuffer(String stringCharset charset)
    {
        Preconditions.checkNotNull(string"string is null");
        Preconditions.checkNotNull(charset"charset is null");
        return wrappedBuffer(string.getBytes(charset));
    }
    public static ByteBuffer encodeString(CharBuffer srcCharset charset)
    {
        final CharsetEncoder encoder = getEncoder(charset);
        final ByteBuffer dst = ByteBuffer.allocate(
                (int) ((doublesrc.remaining() * encoder.maxBytesPerChar()));
        try {
            CoderResult cr = encoder.encode(srcdsttrue);
            if (!cr.isUnderflow()) {
                cr.throwException();
            }
            cr = encoder.flush(dst);
            if (!cr.isUnderflow()) {
                cr.throwException();
            }
        }
        catch (CharacterCodingException x) {
            throw new IllegalStateException(x);
        }
        dst.flip();
        return dst;
    }
    public static String decodeString(ByteBuffer srcCharset charset)
    {
        final CharsetDecoder decoder = getDecoder(charset);
        final CharBuffer dst = CharBuffer.allocate(
                (int) ((doublesrc.remaining() * decoder.maxCharsPerByte()));
        try {
            CoderResult cr = decoder.decode(srcdsttrue);
            if (!cr.isUnderflow()) {
                cr.throwException();
            }
            cr = decoder.flush(dst);
            if (!cr.isUnderflow()) {
                cr.throwException();
            }
        }
        catch (CharacterCodingException x) {
            throw new IllegalStateException(x);
        }
        return dst.flip().toString();
    }

    
Toggles the endianness of the specified 16-bit short integer.
    public static short swapShort(short value)
    {
        return (short) (value << 8 | value >>> 8 & 0xff);
    }

    
Toggles the endianness of the specified 32-bit integer.
    public static int swapInt(int value)
    {
        return swapShort((shortvalue) << 16 |
                swapShort((short) (value >>> 16)) & 0xffff;
    }

    
Toggles the endianness of the specified 64-bit long integer.
    public static long swapLong(long value)
    {
        return (longswapInt((intvalue) << 32 |
                swapInt((int) (value >>> 32)) & 0xffffffffL;
    }
    private static final ThreadLocal<Map<CharsetCharsetEncoder>> encoders =
            new ThreadLocal<Map<CharsetCharsetEncoder>>()
            {
                @Override
                protected Map<CharsetCharsetEncoderinitialValue()
                {
                    return new IdentityHashMap<CharsetCharsetEncoder>();
                }
            };
    private static final ThreadLocal<Map<CharsetCharsetDecoder>> decoders =
            new ThreadLocal<Map<CharsetCharsetDecoder>>()
            {
                @Override
                protected Map<CharsetCharsetDecoderinitialValue()
                {
                    return new IdentityHashMap<CharsetCharsetDecoder>();
                }
            };

    
Returns a cached thread-local java.nio.charset.CharsetEncoder for the specified charset.
    private static CharsetEncoder getEncoder(Charset charset)
    {
        if (charset == null) {
            throw new NullPointerException("charset");
        }
        Map<CharsetCharsetEncodermap = .get();
        CharsetEncoder e = map.get(charset);
        if (e != null) {
            e.reset();
            e.onMalformedInput(.);
            return e;
        }
        e = charset.newEncoder();
        map.put(charsete);
        return e;
    }


    
Returns a cached thread-local java.nio.charset.CharsetDecoder for the specified charset.
    private static CharsetDecoder getDecoder(Charset charset)
    {
        if (charset == null) {
            throw new NullPointerException("charset");
        }
        Map<CharsetCharsetDecodermap = .get();
        CharsetDecoder d = map.get(charset);
        if (d != null) {
            d.reset();
            d.onMalformedInput(.);
            return d;
        }
        d = charset.newDecoder();
        map.put(charsetd);
        return d;
    }
New to GrepCode? Check out our FAQ X