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.ipc;
Utility to collect data written to an java.io.OutputStream in java.nio.ByteBuffers
public class ByteBufferOutputStream extends OutputStream {
  public static final int BUFFER_SIZE = 8192;
  private List<ByteBufferbuffers;
  public ByteBufferOutputStream() {
    reset();
  }

  
Returns all data written and resets the stream to be empty.
  public List<ByteBuffergetBufferList() {
    List<ByteBufferresult = ;
    reset();
    for (ByteBuffer buffer : resultbuffer.flip();
    return result;
  }
  public void reset() {
     = new ArrayList<ByteBuffer>(1);
    .add(ByteBuffer.allocate());
  }
  public void write(ByteBuffer buffer) {
    .add(buffer);
  }
  public void write(int b) {
    ByteBuffer buffer = .get(.size()-1);
    if (buffer.remaining() < 1) {
      buffer = ByteBuffer.allocate();
      .add(buffer);
    }
    buffer.put((byte)b);
  }
  public void write(byte[] bint offint len) {
    ByteBuffer buffer = .get(.size()-1);
    int remaining = buffer.remaining();
    while (len > remaining) {
      buffer.put(boffremaining);
      len -= remaining;
      off += remaining;
      buffer = ByteBuffer.allocate();
      .add(buffer);
      remaining = buffer.remaining();
    }
    buffer.put(bofflen);
  }

  
Add a buffer to the output without copying, if possible. Sets buffer's position to its limit.
  public void writeBuffer(ByteBuffer bufferthrows IOException {
    if (buffer.remaining() < ) {
      write(buffer.array(), buffer.position(), buffer.remaining());
    } else {
      .add(buffer);                        // append w/o copying
    }
    buffer.position(buffer.limit());              // mark data as consumed
  }
New to GrepCode? Check out our FAQ X