001/* 002 * ==================================================================== 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, 014 * software distributed under the License is distributed on an 015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 016 * KIND, either express or implied. See the License for the 017 * specific language governing permissions and limitations 018 * under the License. 019 * ==================================================================== 020 * 021 * This software consists of voluntary contributions made by many 022 * individuals on behalf of the Apache Software Foundation. For more 023 * information on the Apache Software Foundation, please see 024 * <http://www.apache.org/>. 025 * 026 */ 027package org.apache.http.nio.util; 028 029import java.io.IOException; 030 031import org.apache.http.nio.ContentEncoder; 032 033/** 034 * Basic implementation of the {@link ContentOutputBuffer} interface. 035 * <p> 036 * This class is not thread safe. 037 * 038 * @since 4.0 039 */ 040public class SimpleOutputBuffer extends ExpandableBuffer implements ContentOutputBuffer { 041 042 private boolean endOfStream; 043 044 public SimpleOutputBuffer(final int buffersize, final ByteBufferAllocator allocator) { 045 super(buffersize, allocator); 046 this.endOfStream = false; 047 } 048 049 /** 050 * @since 4.3 051 */ 052 public SimpleOutputBuffer(final int buffersize) { 053 this(buffersize, HeapByteBufferAllocator.INSTANCE); 054 } 055 056 @Override 057 public int produceContent(final ContentEncoder encoder) throws IOException { 058 setOutputMode(); 059 final int bytesWritten = encoder.write(this.buffer); 060 if (!hasData() && this.endOfStream) { 061 encoder.complete(); 062 } 063 return bytesWritten; 064 } 065 066 @Override 067 public void write(final byte[] b, final int off, final int len) throws IOException { 068 if (b == null) { 069 return; 070 } 071 if (this.endOfStream) { 072 return; 073 } 074 setInputMode(); 075 ensureCapacity(this.buffer.position() + len); 076 this.buffer.put(b, off, len); 077 } 078 079 public void write(final byte[] b) throws IOException { 080 if (b == null) { 081 return; 082 } 083 if (this.endOfStream) { 084 return; 085 } 086 write(b, 0, b.length); 087 } 088 089 @Override 090 public void write(final int b) throws IOException { 091 if (this.endOfStream) { 092 return; 093 } 094 setInputMode(); 095 ensureCapacity(this.capacity() + 1); 096 this.buffer.put((byte)b); 097 } 098 099 @Override 100 public void reset() { 101 super.clear(); 102 this.endOfStream = false; 103 } 104 105 @Override 106 public void flush() { 107 } 108 109 @Override 110 public void writeCompleted() { 111 this.endOfStream = true; 112 } 113 114 public void shutdown() { 115 this.endOfStream = true; 116 } 117 118}