Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you 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.apache.avro.io;
 
 
A non-buffering version of BinaryDecoder.

This implementation will not read-ahead from the provided InputStream beyond the minimum required to service the API requests.

See also:
Encoder
 
 
 class DirectBinaryDecoder extends BinaryDecoder {
   private InputStream in;
 
   private class ByteReader {
     public ByteBuffer read(ByteBuffer oldint lengththrows IOException {
       ByteBuffer result;
       if (old != null && length <= old.capacity()) {
         result = old;
         result.clear();
       } else {
         result = ByteBuffer.allocate(length);
       }
       doReadBytes(result.array(), result.position(), length);
       result.limit(length);
       return result;
     }
   }
 
   private class ReuseByteReader extends ByteReader {
     private final ByteBufferInputStream bbi;
     
     public ReuseByteReader(ByteBufferInputStream bbi) {
       this. = bbi;
     }
     
     @Override
     public ByteBuffer read(ByteBuffer oldint lengththrows IOException {
       if (old != null) {
         return super.read(oldlength);
       } else {
         return .readBuffer(length);
       }
     }
     
   }
 
   private final ByteReader byteReader;
 
     super();
     this. = in;
      = (in instanceof ByteBufferInputStream) ?
         new ReuseByteReader((ByteBufferInputStreamin) : new ByteReader();
   }
 
   @Override
   public void init(InputStream in) {
     this. = in;
   }
 
   @Override
   public boolean readBoolean() throws IOException {
     int n = .read();
     if (n < 0) {
       throw new EOFException();
     }
     return n == 1;
   }
 
   @Override
   public int readInt() throws IOException {
     int n = 0;
     int b;
     int shift = 0;
    do {
      b = .read();
      if (b >= 0) {
         n |= (b & 0x7F) << shift;
         if ((b & 0x80) == 0) {
           return (n >>> 1) ^ -(n & 1); // back to two's-complement
         }
      } else {
        throw new EOFException();
      }
      shift += 7;
    } while (shift < 32);
    throw new IOException("Invalid int encoding");
    
  }
  public long readLong() throws IOException {
    long n = 0;
    int b;
    int shift = 0;
    do { 
      b = .read();
      if (b >= 0) {
         n |= (b & 0x7FL) << shift;
         if ((b & 0x80) == 0) {
           return (n >>> 1) ^ -(n & 1); // back to two's-complement
         }
      } else {
        throw new EOFException();
      }
      shift += 7;
    } while (shift < 64);
    throw new IOException("Invalid long encoding");
  }
  private final byte[] buf = new byte[8];
  public float readFloat() throws IOException {
    doReadBytes(, 0, 4);
    int n = (((int[0]) & 0xff)
      |  ((((int[1]) & 0xff) << 8)
      |  ((((int[2]) & 0xff) << 16)
      |  ((((int[3]) & 0xff) << 24);
    return Float.intBitsToFloat(n);
  }
  public double readDouble() throws IOException {
    doReadBytes(, 0, 8);
    long n = (((long[0]) & 0xff)
      |  ((((long[1]) & 0xff) << 8)
      |  ((((long[2]) & 0xff) << 16)
      |  ((((long[3]) & 0xff) << 24)
      |  ((((long[4]) & 0xff) << 32)
      |  ((((long[5]) & 0xff) << 40)
      |  ((((long[6]) & 0xff) << 48)
      |  ((((long[7]) & 0xff) << 56);
    return Double.longBitsToDouble(n);
  }
  public ByteBuffer readBytes(ByteBuffer oldthrows IOException {
    int length = readInt();
    return .read(oldlength);
  }
  protected void doSkipBytes(long lengththrows IOException {
    while (length > 0) {
      long n = .skip(length);
      if (n <= 0) {
        throw new EOFException();
      }
      length -= n;
    }
  }
  protected void doReadBytes(byte[] bytesint startint length)
    throws IOException {
    for (; ;) {
      int n = .read(bytesstartlength);
      if (n == length || length == 0) {
        return;
      } else if (n < 0) {
        throw new EOFException();
      }
      start += n;
      length -= n;
    }
  }
  public InputStream inputStream() {
    return ;
  }
  public boolean isEnd() throws IOException {
    throw new UnsupportedOperationException();
  }
New to GrepCode? Check out our FAQ X