📄 smppio.java
字号:
/* * Java SMPP API * Copyright (C) 1998 - 2002 by Oran Kelly * * 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 * * A copy of the LGPL can be viewed at http://www.gnu.org/copyleft/lesser.html * Java SMPP API author: orank@users.sf.net * Java SMPP API Homepage: http://smppapi.sourceforge.net/ * $Id: SMPPIO.java,v 1.12 2005/05/09 21:04:50 orank Exp $ */package ie.omk.smpp.util;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;/** * Class that provides input and output methods for writing Java types encoded * as SMPP types. This class cannot be instantiated...all it's methods are * static. */public class SMPPIO { /** * Read an Integer from an InputStream. The integer read from the stream is * assumed to be in network byte order (ie Big Endian). * * @param in * The InputStream to read from * @param len * The number of bytes to form the integer from (usually either 1 * or 4, limited to 1 <= len <= 8) * @return An integer representation of the len bytes read from in. * @throws java.io.IOException * If EOS is reached before <code>len</code> bytes are read. * @see java.io.InputStream */ public static final int readInt(InputStream in, int len) throws java.io.IOException { byte[] b = new byte[len]; int p = 0; int x = 0; for (int loop = 0; loop < (len - p); loop++) { int r = in.read(b, p, (len - p)); if (r == -1) break; p += r; } return (bytesToInt(b, 0, len)); } /** * Read in a NUL-terminated string from an InputStream * * @param in * The InputStream to read from * @return A String representation with the NUL byte removed. * @throws java.io.IOException * If EOS is reached before a NUL byte * @see java.io.InputStream */ public static final String readCString(InputStream in) throws java.io.IOException { StringBuffer s = new StringBuffer(); int b = in.read(); while (b != 0) { if (b == -1) throw new IOException("End of Input Stream before NULL byte"); s.append((char) b); b = in.read(); } if (s.length() == 0) return (null); else return (s.toString()); } /** * Read a nul-terminated ASCII string from a byte array. * * @return A java String object representing the read string without the * terminating nul. */ public static final String readCString(byte[] b, int offset) { try { int p = offset; while (b[p] != (byte) 0) p++; if (p > offset) return (new String(b, offset, p - offset, "US-ASCII")); else return (""); } catch (java.io.UnsupportedEncodingException x) { return (""); } } /** * Read in a string of specified length from an InputStream. The String may * contain NUL bytes. * * @param in * The InputStream to read from * @param len * The number of bytes to read in from the InputStream * @return A String of length <code>len</code>. null if <code>len</code> * is less than 1. * @throws java.io.IOException * If EOS is reached before a NUL byte * @see java.io.InputStream */ public static final String readString(InputStream in, int len) throws java.io.IOException { if (len < 1) return (null); byte[] b = new byte[len]; int l = 0; StringBuffer s = new StringBuffer(); while (l < len) { int r = in.read(b, 0, (len - l)); if (r == -1) throw new IOException("EOS before NUL byte read."); l += r; s.append(new String(b, 0, r)); } if (s.length() == 0) return (null); else return (s.toString()); } /** * XXX write the javadoc. */ public static final String readString(byte[] b, int offset, int len) { try { if (len > 0) return (new String(b, offset, len - offset, "US-ASCII")); else return (""); } catch (java.io.UnsupportedEncodingException x) { return (""); } } /** * Convert an integer to a byte array in MSB first order * * @param num * The number to store * @param len * The length of the integer to convert * @return An array of length len containing the byte representation of num. */ public static final byte[] intToBytes(int num, int len) { return (intToBytes(num, len, null, 0)); } /** * Convert an integer to a byte array in MSB first order. This method exists * as well as the <code>longToBytes</code> method for performance reasons. * More often than not, a 4-byte value is the largest being * converted...doing that using <code>ints</code> instead of * <code>longs</code> will offer a slight performance increase. The code * for the two methods is identical except for using ints instead of longs * to hold mask, shiftwidth and number values. * * @param num * The number to store * @param len * The length of the integer to convert (that is, the number of * bytes to generate). * @param b * the byte array to write the integer to. * @param offset * the offset in <code>b</code> to write the integer to. * @return An array of length len containing the byte representation of num. */ public static final byte[] intToBytes(int num, int len, byte[] b, int offset) { if (b == null) { b = new byte[len]; offset = 0; } int sw = ((len - 1) * 8); int mask = (0xff << sw); for (int l = 0; l < len; l++) { b[offset + l] = (byte) ((num & mask) >>> sw); sw -= 8; mask >>>= 8; } return (b); } /** * Convert a long to a byte array in MSB first order. * * @param num * The number to store * @param len * The length of the integer to convert (that is, the number of
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -