1 /* 2 * ==================================================================== 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * ==================================================================== 20 * 21 * This software consists of voluntary contributions made by many 22 * individuals on behalf of the Apache Software Foundation. For more 23 * information on the Apache Software Foundation, please see 24 * <http://www.apache.org/>. 25 * 26 */ 27 28 package org.apache.http.impl.nio.codecs; 29 30 import java.io.IOException; 31 import java.nio.ByteBuffer; 32 import java.nio.channels.ReadableByteChannel; 33 34 import org.apache.http.impl.io.HttpTransportMetricsImpl; 35 import org.apache.http.nio.ContentDecoder; 36 import org.apache.http.nio.reactor.SessionInputBuffer; 37 import org.apache.http.util.Args; 38 39 /** 40 * Abstract {@link ContentDecoder} that serves as a base for all content 41 * decoder implementations. 42 * 43 * @since 4.0 44 */ 45 public abstract class AbstractContentDecoder implements ContentDecoder { 46 47 protected final ReadableByteChannel channel; 48 protected final SessionInputBuffer buffer; 49 protected final HttpTransportMetricsImpl metrics; 50 51 protected boolean completed; 52 53 /** 54 * Creates an instance of this class. 55 * 56 * @param channel the source channel. 57 * @param buffer the session input buffer that can be used to store 58 * session data for intermediate processing. 59 * @param metrics Transport metrics of the underlying HTTP transport. 60 */ 61 public AbstractContentDecoder( 62 final ReadableByteChannel channel, 63 final SessionInputBuffer buffer, 64 final HttpTransportMetricsImpl metrics) { 65 super(); 66 Args.notNull(channel, "Channel"); 67 Args.notNull(buffer, "Session input buffer"); 68 Args.notNull(metrics, "Transport metrics"); 69 this.buffer = buffer; 70 this.channel = channel; 71 this.metrics = metrics; 72 } 73 74 @Override 75 public boolean isCompleted() { 76 return this.completed; 77 } 78 79 /** 80 * Sets the completed status of this decoder. Normally this is not necessary 81 * (the decoder will automatically complete when the underlying channel 82 * returns EOF). It is useful to mark the decoder as completed if you have 83 * some other means to know all the necessary data has been read and want to 84 * reuse the underlying connection for more messages. 85 * 86 * @param completed the completed status of this decoder. 87 * @since 4.4.11 88 */ 89 public void setCompleted(final boolean completed) { 90 this.completed = completed; 91 } 92 93 /** 94 * Sets the completed status of this decoder to true. Normally this is not necessary 95 * (the decoder will automatically complete when the underlying channel 96 * returns EOF). It is useful to mark the decoder as completed if you have 97 * some other means to know all the necessary data has been read and want to 98 * reuse the underlying connection for more messages. 99 * 100 * @since 4.4.11 101 */ 102 protected void setCompleted() { 103 this.completed = true; 104 } 105 106 /** 107 * Reads from the channel to the destination. 108 * 109 * @param dst destination. 110 * @return number of bytes transferred. 111 * 112 * @since 4.3 113 */ 114 protected int readFromChannel(final ByteBuffer dst) throws IOException { 115 final int bytesRead = this.channel.read(dst); 116 if (bytesRead > 0) { 117 this.metrics.incrementBytesTransferred(bytesRead); 118 } 119 return bytesRead; 120 } 121 122 /** 123 * Reads from the channel to the session buffer. 124 * @return number of bytes transferred. 125 * 126 * @since 4.3 127 */ 128 protected int fillBufferFromChannel() throws IOException { 129 final int bytesRead = this.buffer.fill(this.channel); 130 if (bytesRead > 0) { 131 this.metrics.incrementBytesTransferred(bytesRead); 132 } 133 return bytesRead; 134 } 135 136 /** 137 * Reads from the channel to the destination. 138 * 139 * @param dst destination. 140 * @param limit max number of bytes to transfer. 141 * @return number of bytes transferred. 142 * 143 * @since 4.3 144 */ 145 protected int readFromChannel(final ByteBuffer dst, final int limit) throws IOException { 146 final int bytesRead; 147 if (dst.remaining() > limit) { 148 final int oldLimit = dst.limit(); 149 final int newLimit = oldLimit - (dst.remaining() - limit); 150 dst.limit(newLimit); 151 bytesRead = this.channel.read(dst); 152 dst.limit(oldLimit); 153 } else { 154 bytesRead = this.channel.read(dst); 155 } 156 if (bytesRead > 0) { 157 this.metrics.incrementBytesTransferred(bytesRead); 158 } 159 return bytesRead; 160 } 161 162 }