| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| TCPIOClient |
|
| 2.4285714285714284;2,429 |
| 1 | /* | |
| 2 | * | |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
| 4 | * | |
| 5 | * Copyright 2007-2008 Sun Microsystems, Inc. All rights reserved. | |
| 6 | * | |
| 7 | * The contents of this file are subject to the terms of either the GNU | |
| 8 | * General Public License Version 2 only ("GPL") or the Common Development | |
| 9 | * and Distribution License("CDDL") (collectively, the "License"). You | |
| 10 | * may not use this file except in compliance with the License. You can obtain | |
| 11 | * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html | |
| 12 | * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific | |
| 13 | * language governing permissions and limitations under the License. | |
| 14 | * | |
| 15 | * When distributing the software, include this License Header Notice in each | |
| 16 | * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt. | |
| 17 | * Sun designates this particular file as subject to the "Classpath" exception | |
| 18 | * as provided by Sun in the GPL Version 2 section of the License file that | |
| 19 | * accompanied this code. If applicable, add the following below the License | |
| 20 | * Header, with the fields enclosed by brackets [] replaced by your own | |
| 21 | * identifying information: "Portions Copyrighted [year] | |
| 22 | * [name of copyright owner]" | |
| 23 | * | |
| 24 | * Contributor(s): | |
| 25 | * | |
| 26 | * If you wish your version of this file to be governed by only the CDDL or | |
| 27 | * only the GPL Version 2, indicate your decision by adding "[Contributor] | |
| 28 | * elects to include this software in this distribution under the [CDDL or GPL | |
| 29 | * Version 2] license." If you don't indicate a single choice of license, a | |
| 30 | * recipient has the option to distribute your version of this file under | |
| 31 | * either the CDDL, the GPL Version 2 or to extend the choice of license to | |
| 32 | * its licensees as provided above. However, if you add GPL Version 2 code | |
| 33 | * and therefore, elected the GPL Version 2 license, then the option applies | |
| 34 | * only if the new code is made subject to such option by the copyright | |
| 35 | * holder. | |
| 36 | * | |
| 37 | */ | |
| 38 | ||
| 39 | package com.sun.grizzly.utils; | |
| 40 | ||
| 41 | import java.io.DataInputStream; | |
| 42 | import java.io.DataOutputStream; | |
| 43 | import java.io.EOFException; | |
| 44 | import java.io.IOException; | |
| 45 | import java.net.Socket; | |
| 46 | ||
| 47 | /** | |
| 48 | * @author Alexey Stashok | |
| 49 | */ | |
| 50 | public class TCPIOClient { | |
| 51 | private String host; | |
| 52 | private int port; | |
| 53 | ||
| 54 | private Socket socket; | |
| 55 | private DataInputStream is; | |
| 56 | private DataOutputStream os; | |
| 57 | ||
| 58 | 26 | public TCPIOClient(String host, int port) { |
| 59 | 26 | this.host = host; |
| 60 | 26 | this.port = port; |
| 61 | 26 | } |
| 62 | ||
| 63 | public void connect() throws IOException { | |
| 64 | 26 | socket = new Socket(host, port); |
| 65 | 25 | socket.setSoTimeout(10 * 1000); |
| 66 | 25 | is = new DataInputStream(socket.getInputStream()); |
| 67 | 25 | os = new DataOutputStream(socket.getOutputStream()); |
| 68 | 25 | } |
| 69 | ||
| 70 | public void send(byte[] msg) throws IOException { | |
| 71 | 1219 | send(msg, true); |
| 72 | 1219 | } |
| 73 | ||
| 74 | public void send(byte[] msg, boolean needFlush) throws IOException { | |
| 75 | 1219 | os.write(msg); |
| 76 | 1219 | if (needFlush) { |
| 77 | 1219 | os.flush(); |
| 78 | } | |
| 79 | 1219 | } |
| 80 | ||
| 81 | public byte[] receive(byte[] buf) throws IOException { | |
| 82 | 1219 | return receive(buf, buf.length); |
| 83 | } | |
| 84 | ||
| 85 | public byte[] receive(byte[] buf, int length) throws IOException { | |
| 86 | 1219 | int bytesRead = 0; |
| 87 | 2437 | while(bytesRead < length) { |
| 88 | 1219 | int readBytesCount = is.read(buf, bytesRead, length - bytesRead); |
| 89 | 1218 | if (readBytesCount == -1) throw new EOFException("Unexpected client EOF!"); |
| 90 | 1218 | bytesRead += readBytesCount; |
| 91 | 1218 | } |
| 92 | ||
| 93 | 1218 | return buf; |
| 94 | } | |
| 95 | ||
| 96 | public void close() throws IOException { | |
| 97 | 30 | if (is != null) { |
| 98 | try { | |
| 99 | 29 | is.close(); |
| 100 | 0 | } catch (IOException ex) { |
| 101 | 29 | } |
| 102 | } | |
| 103 | ||
| 104 | 30 | if (os != null) { |
| 105 | try { | |
| 106 | 29 | os.close(); |
| 107 | 0 | } catch (IOException ex) { |
| 108 | 29 | } |
| 109 | } | |
| 110 | ||
| 111 | 30 | if (socket != null) { |
| 112 | try { | |
| 113 | 29 | socket.close(); |
| 114 | 0 | } catch (IOException ex) { |
| 115 | 29 | } |
| 116 | } | |
| 117 | 30 | } |
| 118 | } |