001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-nio/src/main/java/org/apache/http/impl/nio/codecs/IdentityDecoder.java $
003: * $Revision: 613298 $
004: * $Date: 2008-01-18 23:09:22 +0100 (Fri, 18 Jan 2008) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031:
032: package org.apache.http.impl.nio.codecs;
033:
034: import java.io.IOException;
035: import java.nio.ByteBuffer;
036: import java.nio.channels.FileChannel;
037: import java.nio.channels.ReadableByteChannel;
038:
039: import org.apache.http.impl.io.HttpTransportMetricsImpl;
040: import org.apache.http.nio.FileContentDecoder;
041: import org.apache.http.nio.reactor.SessionInputBuffer;
042:
043: /**
044: * Identity decoder implementation.
045: *
046: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
047: * @author Andrea Selva
048: *
049: * @version $Revision: 613298 $
050: *
051: * @since 4.0
052: */
053: public class IdentityDecoder extends AbstractContentDecoder implements
054: FileContentDecoder {
055:
056: public IdentityDecoder(final ReadableByteChannel channel,
057: final SessionInputBuffer buffer,
058: final HttpTransportMetricsImpl metrics) {
059: super (channel, buffer, metrics);
060: }
061:
062: public int read(final ByteBuffer dst) throws IOException {
063: if (dst == null) {
064: throw new IllegalArgumentException(
065: "Byte buffer may not be null");
066: }
067: if (this .completed) {
068: return -1;
069: }
070:
071: int bytesRead;
072: if (this .buffer.hasData()) {
073: bytesRead = this .buffer.read(dst);
074: } else {
075: bytesRead = this .channel.read(dst);
076: if (bytesRead > 0) {
077: this .metrics.incrementBytesTransferred(bytesRead);
078: }
079: }
080: if (bytesRead == -1) {
081: this .completed = true;
082: }
083: return bytesRead;
084: }
085:
086: public long transfer(final FileChannel dst, long position,
087: long count) throws IOException {
088:
089: if (dst == null) {
090: return 0;
091: }
092: if (this .completed) {
093: return 0;
094: }
095:
096: long bytesRead;
097: if (this .buffer.hasData()) {
098: bytesRead = this .buffer.read(dst);
099: } else {
100: if (this .channel.isOpen()) {
101: bytesRead = dst.transferFrom(this .channel, position,
102: count);
103: } else {
104: bytesRead = -1;
105: }
106: if (bytesRead > 0) {
107: this .metrics.incrementBytesTransferred(bytesRead);
108: }
109: }
110: if (bytesRead == -1) {
111: this .completed = true;
112: }
113: return bytesRead;
114: }
115:
116: @Override
117: public String toString() {
118: StringBuffer buffer = new StringBuffer();
119: buffer.append("[identity; completed: ");
120: buffer.append(this .completed);
121: buffer.append("]");
122: return buffer.toString();
123: }
124:
125: }
|