?? jfmsocketcore.java
字號:
/* * Created on 2004.08.17 * JFreeMail - Java mail component * Copyright (C) 2004 Dalibor Krleza * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ package org.jfreemail.core;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.Socket;import java.util.Vector;/** * Class with core methods for socket operation. All methods static. */public class JfmSocketCore { /** * Special method for reading multiple text lines from socket. Text lines * are separated on CRLF chars and stored into string array. * @param client_socket Socket with connection to server. * @return String array with incoming information. * @throws IOException Thrown on socket error. */ public static String[] getLine(Socket client_socket) throws IOException{ InputStream is=client_socket.getInputStream(); int length=0,i; byte[] in_buffer=new byte[2048]; Vector result=new Vector(); String out=new String(); while(true) { /* * Reading buffer from client socket. If reading full buffer, this line * will be called more than once. If reading number of bytes multiple * to buffer size, last buffer length will be 0. On the other hand, on * reading not full buffer, exiting immediately. */ length=is.read(in_buffer,0,2048); for(i=0;i<length;i++) { out+=(char)in_buffer[i]; /* * Testing CRLF. If detected CRLF, store line into vector. We will * create string array later. */ if (out.length()>=2 && out.charAt(out.length()-1)==(char)10 && out.charAt(out.length()-2)==(char)13) { result.add(out); out=new String(); } } if (length<2048) { /* * Now we have readed all info from socket. Creating string * array from vector. I wanted to decrease memory use with * string array. */ String[] result_list=new String[result.size()]; for(i=0;i<result.size();i++) result_list[i]=(String)result.elementAt(i); return result_list; } length=0; } } /** * Special method for reading binary buffer from client socket. This method * is used when unable to detect charset for reading lines. * @param client_socket Client socket with connection to server. * @return Binary array. * @throws IOException Thrown on communication error. */ public static byte[] getBinary(Socket client_socket) throws IOException{ InputStream is=client_socket.getInputStream(); int length=0; byte[] in_buffer=new byte[2048]; /* * We don't know size of readed byte stream. So it's logical to store * this byte array into stream object. For decreasing memory consumption * we will pull out only binary array. */ ByteArrayOutputStream bos=new ByteArrayOutputStream(); while(true) { length=is.read(in_buffer,0,2048); bos.write(in_buffer,0,length); if (length<2048) { return bos.toByteArray(); } length=0; } }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -