Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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 com.facebook.presto.block;
 
 
 
 import java.util.Set;
 
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
 import static java.nio.charset.StandardCharsets.UTF_8;
 
 public final class BlockEncodingManager
         implements BlockEncodingSerde
 {
     private final TypeManager typeManager;
     private final ConcurrentMap<StringBlockEncodingFactory<?>> blockEncodings = new ConcurrentHashMap<>();
 
     public BlockEncodingManager(TypeManager typeManagerBlockEncodingFactory<?>... blockEncodingFactories)
     {
         this(typeManager, ImmutableSet.copyOf(blockEncodingFactories));
     }
 
     @Inject
     public BlockEncodingManager(TypeManager typeManagerSet<BlockEncodingFactory<?>> blockEncodingFactories)
     {
         this. = checkNotNull(typeManager"typeManager is null");
         for (BlockEncodingFactory<?> factory : checkNotNull(blockEncodingFactories"blockEncodingFactories is null")) {
             addBlockEncodingFactory(factory);
         }
     }
 
     public void addBlockEncodingFactory(BlockEncodingFactory<?> blockEncoding)
     {
         checkNotNull(blockEncoding"blockEncoding is null");
         BlockEncodingFactory<?> existingEntry = .putIfAbsent(blockEncoding.getName(), blockEncoding);
         checkArgument(existingEntry == null"Encoding %s is already registered"blockEncoding.getName());
     }
 
     @Override
     public BlockEncoding readBlockEncoding(SliceInput input)
     {
         // read the encoding name
         String encodingName = readLengthPrefixedString(input);
 
         // look up the encoding factory
         BlockEncodingFactory<?> blockEncoding = .get(encodingName);
         checkArgument(blockEncoding != null"Unknown block encoding %s"encodingName);
 
         // load read the encoding factory from the output stream
         return blockEncoding.readEncoding(thisinput);
     }
 
     @Override
     public void writeBlockEncoding(SliceOutput outputBlockEncoding encoding)
     {
         // get the encoding name
         String encodingName = encoding.getName();
 
         // look up the encoding factory
         BlockEncodingFactory<BlockEncodingblockEncoding = (BlockEncodingFactory<BlockEncoding>) .get(encodingName);
         checkArgument(blockEncoding != null"Unknown block encoding %s"encodingName);
 
         // write the name to the output
         writeLengthPrefixedString(outputencodingName);
 
         // write the encoding to the output
         blockEncoding.writeEncoding(thisoutputencoding);
     }
 
     private static String readLengthPrefixedString(SliceInput input)
     {
         int length = input.readInt();
         byte[] bytes = new byte[length];
         input.readBytes(bytes);
         return new String(bytes);
     }
 
    private static void writeLengthPrefixedString(SliceOutput outputString value)
    {
        byte[] bytes = value.getBytes();
        output.writeInt(bytes.length);
        output.writeBytes(bytes);
    }
New to GrepCode? Check out our FAQ X