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;
 
 
Low-level support for serializing Avro values. This class has two types of methods. One type of methods support the writing of leaf values (for example, writeLong(long) and writeString(org.apache.avro.util.Utf8)). These methods have analogs in Decoder. The other type of methods support the writing of maps and arrays. These methods are writeArrayStart(), startItem(), and writeArrayEnd() (and similar methods for maps). Some implementations of Encoder handle the buffering required to break large maps and arrays into blocks, which is necessary for applications that want to do streaming. (See writeArrayStart() for details on these methods.)

See also:
Decoder
 
 public class BinaryEncoder extends Encoder {
   protected OutputStream out;
   
   private interface ByteWriter {
     void write(ByteBuffer bytesthrows IOException;
   }
   
   private static final class SimpleByteWriter implements ByteWriter {
     private final OutputStream out;
 
     public SimpleByteWriter(OutputStream out) {
       this. = out;
     }
 
     @Override
     public void write(ByteBuffer bytesthrows IOException {
       encodeLong(bytes.remaining(), );
       .write(bytes.array(), bytes.position(), bytes.remaining());
     }
   }
 
   private static final class ReuseByteWriter implements ByteWriter {
     private final ByteBufferOutputStream bbout;
     public ReuseByteWriter(ByteBufferOutputStream bbout) {
       this. = bbout;
     }
 
     @Override
     public void write(ByteBuffer bytesthrows IOException {
       encodeLong(bytes.remaining(), );
       .writeBuffer(bytes);
     }
   }
   
   private final ByteWriter byteWriter;

  
Create a writer that sends its output to the underlying stream out.
 
   public BinaryEncoder(OutputStream out) {
     this. = out;
     this. = (out instanceof ByteBufferOutputStream) ?
         new ReuseByteWriter((ByteBufferOutputStreamout) :
           new SimpleByteWriter(out);
   }
 
   @Override
   public void init(OutputStream outthrows IOException {
     flush();
     this. = out;
   }
 
   @Override
   public void flush() throws IOException {
     if ( != null) {
       .flush();
    }
  }
  public void writeNull() throws IOException { }
  
  public void writeBoolean(boolean bthrows IOException {
    .write(b ? 1 : 0);
  }
  public void writeInt(int nthrows IOException {
    encodeLong(n);
  }
  public void writeLong(long nthrows IOException {
    encodeLong(n);
  }
  
  public void writeFloat(float fthrows IOException {
    encodeFloat(f);
  }
  public void writeDouble(double dthrows IOException {
    encodeDouble(d);
  }
  public void writeString(Utf8 utf8throws IOException {
    encodeLong(utf8.getLength(), );
    .write(utf8.getBytes(), 0, utf8.getLength());
  }
  public void writeBytes(ByteBuffer bytesthrows IOException {
    .write(bytes);
  }
  
  public void writeBytes(byte[] bytesint startint lenthrows IOException {
    encodeLong(len);
    .write(bytesstartlen);
  }
  
  public void writeFixed(byte[] bytesint startint lenthrows IOException {
    .write(bytesstartlen);
  }
  public void writeEnum(int ethrows IOException {
    encodeLong(e);
  }
  public void writeArrayStart() throws IOException {
  }
  public void setItemCount(long itemCountthrows IOException {
    if (itemCount > 0) {
      writeLong(itemCount);
    }
  }
  
  public void startItem() throws IOException {
  }
  public void writeArrayEnd() throws IOException {
    encodeLong(0, );
  }
  public void writeMapStart() throws IOException {
  }
  public void writeMapEnd() throws IOException {
    encodeLong(0, );
  }
  public void writeIndex(int unionIndexthrows IOException {
    encodeLong(unionIndex);
  }
  
  protected static void encodeLong(long nOutputStream othrows IOException {
    n = (n << 1) ^ (n >> 63); // move sign to low-order bit
    while ((n & ~0x7F) != 0) {
      o.write((byte)((n & 0x7f) | 0x80));
      n >>>= 7;
    }
    o.write((byte)n);
  }
  protected static int encodeLong(long nbyte[] bint pos) {
    n = (n << 1) ^ (n >> 63); // move sign to low-order bit
    while ((n & ~0x7F) != 0) {
      b[pos++] = (byte)((n & 0x7f) | 0x80);
      n >>>= 7;
    }
    b[pos++] = (byten;
    return pos;
  }
  protected static void encodeFloat(float fOutputStream othrows IOException {
    long bits = Float.floatToRawIntBits(f);
    o.write((int)(bits      ) & 0xFF);
    o.write((int)(bits >>  8) & 0xFF);
    o.write((int)(bits >> 16) & 0xFF);
    o.write((int)(bits >> 24) & 0xFF);
  }
  protected static int encodeFloat(float fbyte[] bint pos) {
    long bits = Float.floatToRawIntBits(f);
    b[pos++] = (byte)((bits      ) & 0xFF);
    b[pos++] = (byte)((bits >>  8) & 0xFF);
    b[pos++] = (byte)((bits >> 16) & 0xFF);
    b[pos++] = (byte)((bits >> 24) & 0xFF);
    return pos;
  }
  protected static void encodeDouble(double dOutputStream othrows IOException {
    long bits = Double.doubleToRawLongBits(d);
    o.write((int)(bits      ) & 0xFF);
    o.write((int)(bits >>  8) & 0xFF);
    o.write((int)(bits >> 16) & 0xFF);
    o.write((int)(bits >> 24) & 0xFF);
    o.write((int)(bits >> 32) & 0xFF);
    o.write((int)(bits >> 40) & 0xFF);
    o.write((int)(bits >> 48) & 0xFF);
    o.write((int)(bits >> 56) & 0xFF);
  }
  protected static int encodeDouble(double dbyte[] bint pos) {
    long bits = Double.doubleToRawLongBits(d);
    b[pos++] = (byte)((bits      ) & 0xFF);
    b[pos++] = (byte)((bits >>  8) & 0xFF);
    b[pos++] = (byte)((bits >> 16) & 0xFF);
    b[pos++] = (byte)((bits >> 24) & 0xFF);
    b[pos++] = (byte)((bits >> 32) & 0xFF);
    b[pos++] = (byte)((bits >> 40) & 0xFF);
    b[pos++] = (byte)((bits >> 48) & 0xFF);
    b[pos++] = (byte)((bits >> 56) & 0xFF);
    return pos;
  }
New to GrepCode? Check out our FAQ X