?? jfmcore.java
字號:
/* * Created on 2004.08.19 * 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.util.Vector;/** * Class for basic encoding, decoding and checking operations needed in * all other classes. */public class JfmCore { /** * Static procedure for base64 decoding. Most of E-mail binaries * are base64 coded. There could be other encodings, which should * be implemented in future, as needed. * @param data List of strings representing binary value. * @return Decoded binary array. * @throws IOException Thrown on decoding exception. */ public static byte[] base64decode(String[] data) throws IOException { ByteArrayOutputStream bos=new ByteArrayOutputStream(); long __data=0x00000000; int turn=3; byte[] temp_buffer=new byte[3]; for(int i=0;i<data.length;i++) { for(int j=0;j<data[i].length();j++) { byte in=base64fielde(data[i].charAt(j)); if (in>=0 && in<64) { long __temp1=(byte)in & 0x000000FF; __temp1<<=(turn*6); __data|=__temp1; turn--; } else { turn=-1; } if (turn<0) { for(int k=0;k<3;k++) { temp_buffer[2-k]=(byte)__data; __data>>=8; } bos.write(temp_buffer); turn=3; __data=0; } } } return bos.toByteArray(); } /** * Static procedure for base64 encoding. Used for binaries in E-mail. * Attachments etc... * @param data Binary array for encoding. * @return List of base64 encoded strings. * @throws IOException Thrown on encoding exception. */ public static String[] base64encode(byte[] data) throws IOException { Vector lista_vec=new Vector(); long __data=0; int chars=0; String out=new String(""); boolean end_flag=false; for(int i=0;i<data.length;i+=3) { __data=0; if (i<data.length) { /* * Java uses obsolete type of binary representation. * Every varijable uses MSB for sign representation. This doesn't * affect arithmetic operations. When performing logical operations, * MSB is allways calculated from both MSB's. This results * with MSB comparison no matter variable size. * For example, int's (16-bit) MSB is compared with byte's * (8 bit) MSB. Like: * 10100100 - negative number * OR 0xxxxxxx11111111 - positive number * 1xxxxxxx11111111 - negativne number * I ask myself how do they implement this operation in hardware?? * No wonder Java is so terriby slow. We use a trick for negative * number correction in wider variable type, and then perform * logical operation. */ int b=(int)data[i]; if (b<0) b+=256; __data|=(b<<16); } else end_flag=true; if ((i+1)<data.length) { int b=(int)data[i+1]; if (b<0) b+=256; __data|=(b<<8); } else end_flag=true; if ((i+2)<data.length) { int b=(int)data[i+2]; if (b<0) b+=256; __data|=(b); } else end_flag=true; for(int j=0;j<4;j++) { __data<<=6; long x=__data & 0xFF000000; __data&=0x00FFFFFF; x>>=24; byte y=(byte)x; if (end_flag) { char c='='; if (y>0) c=base64fieldd(y); out+=c; } else { char c=base64fieldd(y); out+=c; } chars++; if (chars==76) { lista_vec.add(out); out=new String(""); chars=0; } } } lista_vec.add(out); String[] lista=new String[lista_vec.size()]; for(int i=0;i<lista_vec.size();i++) lista[i]=(String)lista_vec.elementAt(i); return lista; } /** * Procedure for character into byte tranformation. Used when decoding * base64 content. This procedure converts 8-bit character information * into 6-bit information used for decoding. * @param c Character for converting into 6-bit info. * @return 6-bit info in byte variable. */ public static byte base64fielde(char c) { byte x=(byte)c; if (x>=65 && x<=90) return (byte)(x-65); if (x>=97 && x<=122) return (byte)(x-71); if (x>=48 && x<=57) return (byte)(x+4); if (x==43) return (byte)62; if (x==47) return (byte)63; if (c=='=') return (byte)0x00; return (byte)0xFF; } /** * Procedure for byte into character transformation. Used when encoding * base64 content. This procedure convert 6-bit info in byte variable into * character. * @param b 6-bit info in byte variable. * @return Character accoring to 6-bit info. */ public static char base64fieldd(byte b) { if (b>=0 && b<=25) return (char)(b+65); if (b>25 && b<=51) return (char)(b+71); if (b>51 && b<=61) return (char)(b-4); if (b==62) return(char)43; if (b==63) return(char)47; return '='; } /** * Inner procedure for checking positive or negative result from SMTP. * @param line Resulting line, answered from server. * @return Operation succesfull or not. */ public static boolean checkSMTPresult(String line) { if (line.charAt(0)=='1' || line.charAt(0)=='2' || line.charAt(0)=='3') return true; return false; } /** * Method for checking text mime types. See JfmConsts for details. * @param mime_type Mime/type to check. * @return true if text mime/type otherwise false. */ public static boolean checkTextType(String mime_type) { boolean result=false; for(int i=0;i<JfmConsts.TEXT_MIME_TYPES.length;i++) { if (mime_type.toUpperCase().equals(JfmConsts.TEXT_MIME_TYPES[i])) result=true; } return result; }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -