001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.activemq.transport.auto.nio; 018 019import java.io.IOException; 020import java.net.Socket; 021import java.net.URI; 022import java.net.UnknownHostException; 023 024import javax.net.SocketFactory; 025 026import org.apache.activemq.transport.nio.NIOTransport; 027import org.apache.activemq.wireformat.WireFormat; 028 029/** 030 * 031 * 032 */ 033public class AutoNIOTransport extends NIOTransport { 034 035 public AutoNIOTransport(WireFormat format, Socket socket, 036 InitBuffer initBuffer) throws IOException { 037 super(format, socket, initBuffer); 038 } 039 040 public AutoNIOTransport(WireFormat wireFormat, Socket socket) 041 throws IOException { 042 super(wireFormat, socket); 043 } 044 045 public AutoNIOTransport(WireFormat wireFormat, SocketFactory socketFactory, 046 URI remoteLocation, URI localLocation) throws UnknownHostException, 047 IOException { 048 super(wireFormat, socketFactory, remoteLocation, localLocation); 049 } 050 051 052 boolean doneInitBuffer = false; 053 054 /** 055 * Read from the initial buffer if it is set 056 */ 057 @Override 058 protected int readFromBuffer() throws IOException { 059 int readSize = 0; 060 if (!doneInitBuffer) { 061 if (initBuffer == null || initBuffer.readSize < 8) { 062 throw new IOException("Protocol type could not be determined."); 063 } 064 if (nextFrameSize == -1) { 065 readSize = 4; 066 this.initBuffer.buffer.flip(); 067 if (this.initBuffer.buffer.remaining() < 8) { 068 throw new IOException("Protocol type could not be determined."); 069 } 070 for (int i = 0; i < 4; i++) { 071 currentBuffer.put(initBuffer.buffer.get()); 072 } 073 } else { 074 for (int i = 0; i < 4; i++) { 075 currentBuffer.put(initBuffer.buffer.get()); 076 } 077 readSize = 4; 078 doneInitBuffer = true; 079 } 080 081 } else { 082 readSize += channel.read(currentBuffer); 083 } 084 return readSize; 085 } 086 087 088}