PortParser.java revision d8cb70b52ca9f78d526ccd9cf67e4f4345ecdc95
1/* 2 * Copyright 2008 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16package org.mockftpserver.core.util; 17 18import java.net.InetAddress; 19import java.net.UnknownHostException; 20import java.util.Arrays; 21 22/** 23 * Utility class for parsing port values from command arguments. 24 * 25 * @author Chris Mair 26 * @version $Revision$ - $Date$ 27 */ 28public final class PortParser { 29 30 /** 31 * Private constructor. All methods are static. 32 */ 33 private PortParser() { 34 } 35 36 /** 37 * Parse a 32-bit IP address from the String[] of FTP command parameters. 38 * 39 * @param parameters - the String[] of command parameters. It is the concatenation 40 * of a 32-bit internet host address and a 16-bit TCP port address. This address 41 * information is broken into 8-bit fields and the value of each field is encoded 42 * as a separate parameter whose value is a decimal number (in character string 43 * representation). Thus, the six parameters for the port command would be: 44 * h1,h2,h3,h4,p1,p2 45 * where h1 is the high order 8 bits of the internet host address, and p1 is the 46 * high order 8 bits of the port number. 47 * @return the InetAddres representing the host parsed from the parameters 48 * @throws org.mockftpserver.core.util.AssertFailedException 49 * - if parameters is null or contains an insufficient number of elements 50 * @throws NumberFormatException - if one of the parameters does not contain a parsable integer 51 * @throws java.net.UnknownHostException 52 */ 53 public static InetAddress parseHost(String[] parameters) throws UnknownHostException { 54 verifySufficientParameters(parameters); 55 56 byte host1 = Byte.parseByte(parameters[0]); 57 byte host2 = Byte.parseByte(parameters[1]); 58 byte host3 = Byte.parseByte(parameters[2]); 59 byte host4 = Byte.parseByte(parameters[3]); 60 61 byte[] address = {host1, host2, host3, host4}; 62 InetAddress inetAddress = InetAddress.getByAddress(address); 63 64 return inetAddress; 65 } 66 67 /** 68 * Parse a 16-bit port number from the String[] of FTP command parameters. 69 * 70 * @param parameters - the String[] of command parameters. It is the concatenation 71 * of a 32-bit internet host address and a 16-bit TCP port address. This address 72 * information is broken into 8-bit fields and the value of each field is encoded 73 * as a separate parameter whose value is a decimal number (in character string 74 * representation). Thus, the six parameters for the port command would be: 75 * h1,h2,h3,h4,p1,p2 76 * where h1 is the high order 8 bits of the internet host address, and p1 is the 77 * high order 8 bits of the port number. 78 * @return the port number parsed from the parameters 79 * @throws org.mockftpserver.core.util.AssertFailedException 80 * - if parameters is null or contains an insufficient number of elements 81 * @throws NumberFormatException - if one of the parameters does not contain a parsable integer 82 */ 83 public static int parsePortNumber(String[] parameters) { 84 verifySufficientParameters(parameters); 85 86 int port1 = Integer.parseInt(parameters[4]); 87 int port2 = Integer.parseInt(parameters[5]); 88 int port = (port1 << 8) + port2; 89 90 return port; 91 } 92 93 /** 94 * Verify that the parameters is not null and contains the required number of elements 95 * 96 * @param parameters - the String[] of command parameters 97 * @throws org.mockftpserver.core.util.AssertFailedException 98 * - if parameters is null or contains an insufficient number of elements 99 */ 100 private static void verifySufficientParameters(String[] parameters) { 101 Assert.notNull(parameters, "parameters"); 102 Assert.isTrue(parameters.length >= 6, "The PORT command must contain least be 6 parameters: " + Arrays.asList(parameters)); 103 } 104 105}