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 abstract class Encoder implements Flushable {

  
Redirect output (and reset the parser state if we're checking).
 
   public abstract void init(OutputStream outthrows IOException;

  
"Writes" a null value. (Doesn't actually write anything, but advances the state of the parser if this class is stateful.)

Throws:
AvroTypeException If this is a stateful writer and a null is not expected
 
   public abstract void writeNull() throws IOException;
  
  
Write a boolean value.

Throws:
AvroTypeException If this is a stateful writer and a boolean is not expected
 
   public abstract void writeBoolean(boolean bthrows IOException;

  
Writes a 32-bit integer.

Throws:
AvroTypeException If this is a stateful writer and an integer is not expected
 
   public abstract void writeInt(int nthrows IOException;

  
Write a 64-bit integer.

Throws:
AvroTypeException If this is a stateful writer and a long is not expected
 
   public abstract void writeLong(long nthrows IOException;
  
  
Write a float.

Throws:
java.io.IOException
AvroTypeException If this is a stateful writer and a float is not expected
 
   public abstract void writeFloat(float fthrows IOException;

  
Write a double.

Throws:
AvroTypeException If this is a stateful writer and a double is not expected
 
   public abstract void writeDouble(double dthrows IOException;

  
Write a Unicode character string.

Throws:
AvroTypeException If this is a stateful writer and a char-string is not expected
 
   public abstract void writeString(Utf8 utf8throws IOException;

  
Write a Unicode character string.

Throws:
AvroTypeException If this is a stateful writer and a char-string is not expected
  public void writeString(String strthrows IOException {
    writeString(new Utf8(str));
  }

  
Write a byte string.

Throws:
AvroTypeException If this is a stateful writer and a byte-string is not expected
  public abstract void writeBytes(ByteBuffer bytesthrows IOException;
  
  
Write a byte string.

Throws:
AvroTypeException If this is a stateful writer and a byte-string is not expected
  public abstract void writeBytes(byte[] bytesint startint lenthrows IOException;
  
  
Writes a byte string. Equivalent to writeBytes(bytes, 0, bytes.length)

Throws:
java.io.IOException
AvroTypeException If this is a stateful writer and a byte-string is not expected
  public void writeBytes(byte[] bytesthrows IOException {
    writeBytes(bytes, 0, bytes.length);
  }

  
Writes a fixed size binary object.

Parameters:
bytes The contents to write
start The position within bytes where the contents start.
len The number of bytes to write.
Throws:
AvroTypeException If this is a stateful writer and a byte-string is not expected
java.io.IOException
  public abstract void writeFixed(byte[] bytesint startint lenthrows IOException;

  
A shorthand for writeFixed(bytes, 0, bytes.length)

Parameters:
bytes
  public void writeFixed(byte[] bytesthrows IOException {
    writeFixed(bytes, 0, bytes.length);
  }
  
  
Writes an enumeration.

Parameters:
e
Throws:
AvroTypeException If this is a stateful writer and an enumeration is not expected or the e is out of range.
java.io.IOException
  public abstract void writeEnum(int ethrows IOException;

  
Call this method to start writing an array. When starting to serialize an array, call writeArrayStart(). Then, before writing any data for any item call setItemCount(long) followed by a sequence of startItem() and the item itself. The number of startItem() should match the number specified in setItemCount(long). When actually writing the data of the item, you can call any Encoder method (e.g., writeLong(long)). When all items of the array have been written, call writeArrayEnd(). As an example, let's say you want to write an array of records, the record consisting of an Long field and a Boolean field. Your code would look something like this:
  out.writeArrayStart();
  out.setItemCount(list.size());
  for (Record r : list) {
    out.startItem();
    out.writeLong(r.longField);
    out.writeBoolean(r.boolField);
  }
  out.writeArrayEnd();
  

Throws:
AvroTypeException If this is a stateful writer and an array is not expected
  public abstract void writeArrayStart() throws IOException;

  
Call this method before writing a batch of items in an array or a map. Then for each item, call startItem() followed by any of the other write methods of Encoder. The number of calls to startItem() must be equal to the count specified in setItemCount(long). Once a batch is completed you can start another batch with setItemCount(long).

Parameters:
itemCount The number of startItem() calls to follow.
Throws:
java.io.IOException
  public abstract void setItemCount(long itemCountthrows IOException;
  
  
Start a new item of an array or map. See writeArrayStart() for usage information.

Throws:
AvroTypeException If called outside of an array or map context
  public abstract void startItem() throws IOException;

  
Call this method to finish writing an array. See writeArrayStart() for usage information.

Throws:
AvroTypeException If items written does not match count provided to writeArrayStart()
AvroTypeException If not currently inside an array
  public abstract void writeArrayEnd() throws IOException;

  
Call this to start a new map. See writeArrayStart() for details on usage. As an example of usage, let's say you want to write a map of records, the record consisting of an Long field and a Boolean field. Your code would look something like this:
 out.writeMapStart();
 out.setItemCount(list.size());
 for (Map.Entry<String,Record> entry : map.entrySet()) {
   out.startItem();
   out.writeString(entry.getKey());
   out.writeLong(entry.getValue().longField);
   out.writeBoolean(entry.getValue().boolField);
 }
 out.writeMapEnd();
 

Throws:
AvroTypeException If this is a stateful writer and a map is not expected
  public abstract void writeMapStart() throws IOException;

  
Call this method to terminate the inner-most, currently-opened map. See writeArrayStart() for more details.

Throws:
AvroTypeException If items written does not match count provided to writeMapStart()
AvroTypeException If not currently inside a map
  public abstract void writeMapEnd() throws IOException;

  
Call this method to write the tag of a union. As an example of usage, let's say you want to write a union, whose second branch is a record consisting of an Long field and a Boolean field. Your code would look something like this:
 out.writeIndex(1);
 out.writeLong(record.longField);
 out.writeBoolean(record.boolField);
 

Throws:
AvroTypeException If this is a stateful writer and a map is not expected
  public abstract void writeIndex(int unionIndexthrows IOException;
New to GrepCode? Check out our FAQ X