- 浏览: 996187 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (367)
- JavaScript (23)
- Java (60)
- Python (41)
- 其他 (36)
- SQL (4)
- 开发工具 (26)
- Linux (15)
- AJAX (6)
- Cache (3)
- 正则 (4)
- 架构 (9)
- 手机 (3)
- MySQL (4)
- Android (115)
- vps (1)
- 网站 (4)
- scale (3)
- 搜索引擎 (3)
- iPhone (2)
- hessian (1)
- hessdroid (1)
- 411 (1)
- jstat (1)
- gc (1)
- gallery (1)
- 惯性 (1)
- eclipse (1)
- mac wget error (1)
- miui file explorer 无用 解决办法 (1)
- vim (1)
最新评论
-
qingyezhangluo:
哎。楼主您既然是分享代码的为什么要加密的呢?而且问你密码还不回 ...
android应用换皮肤(转) -
MagicError:
kavoe 写道下载文件有密码。。。。
http抓包工具 -
knightdf:
我先试下再来
JAVA的RAS加密例子 -
kavoe:
下载文件有密码。。。。
http抓包工具 -
changanfounder:
hmc1985 写道setCallbackDuringFlin ...
android gallery滑动惯性问题
前段时间一直忙着做J2EE服务器与C++客户端的项目。终于,项目告一段落,有一些收获在这里与大家分享。
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.IOException;
- /**
- * 仅仅适用于 Java 与 C++ 通讯中,网络流解析与生成使用
- *
- * 高低位互换(Big-Endian 大头在前 & Little-Endian 小头在前)。
- * 举例而言,有一个4字节的数据0x01020304,要存储在内存中或文件中编号0˜3字节的位置,两种字节序的排列方式分别如下:
- *
- * Big Endian
- *
- * 低地址 高地址
- * ---------------------------------------------------->
- * 地址编号
- * | 0 | 1 | 2 | 3 |
- * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
- * | 01 | 02 | 03 | 04 |
- * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
- *
- * Little Endian
- *
- * 低地址 高地址
- * ---------------------------------------------------->
- * 地址编号
- * | 0 | 1 | 2 | 3 |
- * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
- * | 04 | 03 | 02 | 01 |
- * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
- *
- * Java则统一使用big模式
- * c中的unsigned short 对应着java中的char两个字节,无符号
- * c的无符号int,short,byte字节数组,相应转换成java的long,char,short
- *
- * @author Snowolf
- * @version 1.0
- * @since 1.0
- */
- public abstract class CIOUtil {
- public static final String CHARSET = "UTF-8";
- /**
- * 从输入流中读布尔
- *
- * @param is
- * @return
- * @throws IOException
- */
- public static boolean readBoolean(DataInputStream is) throws IOException {
- return is.readBoolean();
- }
- /**
- * 从流中读定长度字节数组
- *
- * @param is
- * @param s
- * @return
- * @throws IOException
- */
- public static byte[] readBytes(DataInputStream is, int i)
- throws IOException {
- byte[] data = new byte[i];
- is.readFully(data);
- return data;
- }
- /**
- * 从输入流中读字符
- *
- * @param is
- * @return
- * @throws IOException
- */
- public static char readChar(DataInputStream is) throws IOException {
- return (char) readShort(is);
- }
- /**
- * 从输入流中读双精度
- *
- * @param is
- * @return
- * @throws IOException
- */
- public static double readDouble(DataInputStream is) throws IOException {
- return Double.longBitsToDouble(readLong(is));
- }
- /**
- * 从输入流中读单精度
- *
- * @param is
- * @return
- * @throws IOException
- */
- public static float readFloat(DataInputStream is) throws IOException {
- return Float.intBitsToFloat(readInt(is));
- }
- /**
- * 从流中读整型
- *
- * @param is
- * @return
- * @throws IOException
- */
- public static int readInt(DataInputStream is) throws IOException {
- return Integer.reverseBytes(is.readInt());
- }
- /**
- * 从流中读长整型
- *
- * @param is
- * @return
- * @throws IOException
- */
- public static long readLong(DataInputStream is) throws IOException {
- return Long.reverseBytes(is.readLong());
- }
- /**
- * 从流中读短整型
- *
- * @param is
- * @return
- * @throws IOException
- */
- public static short readShort(DataInputStream is) throws IOException {
- return Short.reverseBytes(is.readShort());
- }
- /**
- * 从输入流中读字符串 字符串 结构 为 一个指定字符串字节长度的短整型+实际字符串
- *
- * @param is
- * @return
- * @throws IOException
- */
- public static String readUTF(DataInputStream is) throws IOException {
- short s = readShort(is);
- byte[] str = new byte[s];
- is.readFully(str);
- return new String(str, CHARSET);
- }
- /**
- * 向输出流中写布尔
- *
- * @param os
- * @param b
- * @throws IOException
- */
- public static void writeBoolean(DataOutputStream os, boolean b)
- throws IOException {
- os.writeBoolean(b);
- }
- /**
- * 向输出流中写字节数组
- *
- * @param os
- * @param data
- * @throws IOException
- */
- public static void writeBytes(DataOutputStream os, byte[] data)
- throws IOException {
- os.write(data);
- }
- /**
- * 向输出流中写字符
- *
- * @param os
- * @param b
- * @throws IOException
- */
- public static void writeChar(DataOutputStream os, char b)
- throws IOException {
- writeShort(os, (short) b);
- }
- /**
- * 向输出流中写双精度
- *
- * @param os
- * @param d
- * @throws IOException
- */
- public static void writeDouble(DataOutputStream os, double d)
- throws IOException {
- writeLong(os, Double.doubleToLongBits(d));
- }
- /**
- * 向输出流中写单精度
- *
- * @param os
- * @param f
- * @throws IOException
- */
- public static void writeFloat(DataOutputStream os, float f)
- throws IOException {
- writeInt(os, Float.floatToIntBits(f));
- }
- /**
- * 向输出流中写整型
- *
- * @param os
- * @param i
- * @throws IOException
- */
- public static void writeInt(DataOutputStream os, int i) throws IOException {
- os.writeInt(Integer.reverseBytes(i));
- }
- /**
- * 向输出流中写长整型
- *
- * @param os
- * @param l
- * @throws IOException
- */
- public static void writeLong(DataOutputStream os, long l)
- throws IOException {
- os.writeLong(Long.reverseBytes(l));
- }
- /**
- * 向输出流中写短整型
- *
- * @param os
- * @param s
- * @throws IOException
- */
- public static void writeShort(DataOutputStream os, short s)
- throws IOException {
- os.writeShort(Short.reverseBytes(s));
- }
- /**
- * 向输出流中写字符串 字符串 结构 为 一个指定字符串字节长度的短整型+实际字符串
- *
- * @param os
- * @param str
- * @throws IOException
- */
- public static void writeUTF(DataOutputStream os, String str)
- throws IOException {
- byte[] data = str.getBytes(CHARSET);
- writeShort(os, (short) data.length);
- os.write(data);
- }
- }
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; /** * 仅仅适用于 Java 与 C++ 通讯中,网络流解析与生成使用 * * 高低位互换(Big-Endian 大头在前 & Little-Endian 小头在前)。 * 举例而言,有一个4字节的数据0x01020304,要存储在内存中或文件中编号0˜3字节的位置,两种字节序的排列方式分别如下: * * Big Endian * * 低地址 高地址 * ----------------------------------------------------> * 地址编号 * | 0 | 1 | 2 | 3 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * | 01 | 02 | 03 | 04 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * * Little Endian * * 低地址 高地址 * ----------------------------------------------------> * 地址编号 * | 0 | 1 | 2 | 3 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * | 04 | 03 | 02 | 01 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * * Java则统一使用big模式 * c中的unsigned short 对应着java中的char两个字节,无符号 * c的无符号int,short,byte字节数组,相应转换成java的long,char,short * * @author Snowolf * @version 1.0 * @since 1.0 */ public abstract class CIOUtil { public static final String CHARSET = "UTF-8"; /** * 从输入流中读布尔 * * @param is * @return * @throws IOException */ public static boolean readBoolean(DataInputStream is) throws IOException { return is.readBoolean(); } /** * 从流中读定长度字节数组 * * @param is * @param s * @return * @throws IOException */ public static byte[] readBytes(DataInputStream is, int i) throws IOException { byte[] data = new byte[i]; is.readFully(data); return data; } /** * 从输入流中读字符 * * @param is * @return * @throws IOException */ public static char readChar(DataInputStream is) throws IOException { return (char) readShort(is); } /** * 从输入流中读双精度 * * @param is * @return * @throws IOException */ public static double readDouble(DataInputStream is) throws IOException { return Double.longBitsToDouble(readLong(is)); } /** * 从输入流中读单精度 * * @param is * @return * @throws IOException */ public static float readFloat(DataInputStream is) throws IOException { return Float.intBitsToFloat(readInt(is)); } /** * 从流中读整型 * * @param is * @return * @throws IOException */ public static int readInt(DataInputStream is) throws IOException { return Integer.reverseBytes(is.readInt()); } /** * 从流中读长整型 * * @param is * @return * @throws IOException */ public static long readLong(DataInputStream is) throws IOException { return Long.reverseBytes(is.readLong()); } /** * 从流中读短整型 * * @param is * @return * @throws IOException */ public static short readShort(DataInputStream is) throws IOException { return Short.reverseBytes(is.readShort()); } /** * 从输入流中读字符串 字符串 结构 为 一个指定字符串字节长度的短整型+实际字符串 * * @param is * @return * @throws IOException */ public static String readUTF(DataInputStream is) throws IOException { short s = readShort(is); byte[] str = new byte[s]; is.readFully(str); return new String(str, CHARSET); } /** * 向输出流中写布尔 * * @param os * @param b * @throws IOException */ public static void writeBoolean(DataOutputStream os, boolean b) throws IOException { os.writeBoolean(b); } /** * 向输出流中写字节数组 * * @param os * @param data * @throws IOException */ public static void writeBytes(DataOutputStream os, byte[] data) throws IOException { os.write(data); } /** * 向输出流中写字符 * * @param os * @param b * @throws IOException */ public static void writeChar(DataOutputStream os, char b) throws IOException { writeShort(os, (short) b); } /** * 向输出流中写双精度 * * @param os * @param d * @throws IOException */ public static void writeDouble(DataOutputStream os, double d) throws IOException { writeLong(os, Double.doubleToLongBits(d)); } /** * 向输出流中写单精度 * * @param os * @param f * @throws IOException */ public static void writeFloat(DataOutputStream os, float f) throws IOException { writeInt(os, Float.floatToIntBits(f)); } /** * 向输出流中写整型 * * @param os * @param i * @throws IOException */ public static void writeInt(DataOutputStream os, int i) throws IOException { os.writeInt(Integer.reverseBytes(i)); } /** * 向输出流中写长整型 * * @param os * @param l * @throws IOException */ public static void writeLong(DataOutputStream os, long l) throws IOException { os.writeLong(Long.reverseBytes(l)); } /** * 向输出流中写短整型 * * @param os * @param s * @throws IOException */ public static void writeShort(DataOutputStream os, short s) throws IOException { os.writeShort(Short.reverseBytes(s)); } /** * 向输出流中写字符串 字符串 结构 为 一个指定字符串字节长度的短整型+实际字符串 * * @param os * @param str * @throws IOException */ public static void writeUTF(DataOutputStream os, String str) throws IOException { byte[] data = str.getBytes(CHARSET); writeShort(os, (short) data.length); os.write(data); } }
再写个测试类
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import org.junit.Test;
- import static org.junit.Assert.*;
- /**
- *
- * @author Snowolf
- * @version 1.0
- * @since 1.0
- */
- public class CIOUtilTest {
- /**
- * 测试布尔值
- *
- * @throws IOException
- */
- @Test
- public final void testBoolean() throws IOException {
- boolean input = true;
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- DataOutputStream os = new DataOutputStream(baos);
- CIOUtil.writeBoolean(os, input);
- byte[] b = baos.toByteArray();
- baos.flush();
- baos.close();
- ByteArrayInputStream bais = new ByteArrayInputStream(b);
- DataInputStream is = new DataInputStream(bais);
- boolean output = CIOUtil.readBoolean(is);
- bais.close();
- assertEquals(input, output);
- }
- /**
- * 测试字节数组
- *
- * @throws IOException
- */
- @Test
- public final void testBytes() throws IOException {
- byte[] input = "中文".getBytes("UTF-8");
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- DataOutputStream os = new DataOutputStream(baos);
- CIOUtil.writeBytes(os, input);
- byte[] b = baos.toByteArray();
- baos.flush();
- baos.close();
- ByteArrayInputStream bais = new ByteArrayInputStream(b);
- DataInputStream is = new DataInputStream(bais);
- byte[] output = CIOUtil.readBytes(is, 6);
- bais.close();
- assertArrayEquals(input, output);
- }
- /**
- * 测试字符
- *
- * @throws IOException
- */
- @Test
- public final void testChar() throws IOException {
- char input = '中';
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- DataOutputStream os = new DataOutputStream(baos);
- CIOUtil.writeChar(os, input);
- byte[] b = baos.toByteArray();
- baos.flush();
- baos.close();
- ByteArrayInputStream bais = new ByteArrayInputStream(b);
- DataInputStream is = new DataInputStream(bais);
- char output = CIOUtil.readChar(is);
- bais.close();
- assertEquals(input, output);
- }
- /**
- * 测试双精度
- *
- * @throws IOException
- */
- @Test
- public final void testDouble() throws IOException {
- double input = 1.23456789d;
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- DataOutputStream os = new DataOutputStream(baos);
- CIOUtil.writeDouble(os, input);
- byte[] b = baos.toByteArray();
- baos.flush();
- baos.close();
- ByteArrayInputStream bais = new ByteArrayInputStream(b);
- DataInputStream is = new DataInputStream(bais);
- double output = CIOUtil.readDouble(is);
- bais.close();
- assertEquals(input, output, 9);
- }
- /**
- * 测试单精度
- *
- * @throws IOException
- */
- @Test
- public final void testFloat() throws IOException {
- float input = 1.23456789f;
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- DataOutputStream os = new DataOutputStream(baos);
- CIOUtil.writeFloat(os, input);
- byte[] b = baos.toByteArray();
- baos.flush();
- baos.close();
- ByteArrayInputStream bais = new ByteArrayInputStream(b);
- DataInputStream is = new DataInputStream(bais);
- float output = CIOUtil.readFloat(is);
- bais.close();
- assertEquals(input, output, 9);
- }
- /**
- * 测试整型
- *
- * @throws IOException
- */
- @Test
- public final void testInt() throws IOException {
- int input = 1;
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- DataOutputStream os = new DataOutputStream(baos);
- CIOUtil.writeInt(os, input);
- byte[] b = baos.toByteArray();
- baos.flush();
- baos.close();
- ByteArrayInputStream bais = new ByteArrayInputStream(b);
- DataInputStream is = new DataInputStream(bais);
- int output = CIOUtil.readInt(is);
- bais.close();
- assertEquals(input, output);
- }
- /**
- * 测试长整型
- *
- * @throws IOException <l
发表评论
-
android屏幕适配
2012-11-16 17:24 2145屏幕适配一直是一个让人头疼的问题,论坛上讨论这个问题的最后也 ... -
禁止Eclipse中xml文件Run as的XSL Transformation生成out.xml以方便Android应用开发
2012-08-26 21:38 2586可以在Eclipse里面配置,菜单Windows->P ... -
android 自带的主题 theme 的使用
2012-08-25 23:21 2334在android的sdk 安装目录data\r ... -
android开发之gallery 实现滚动一张且短距离滑动实现滚动
2011-12-19 18:50 1886首先gallery的特点就不用多说了吧,惯性滚动、半屏翻页,但 ... -
使用Jstat监控gc情况(收藏)
2011-12-19 13:22 1803性能测试过程中,我们 ... -
eclipse生成javadoc乱码解决
2011-10-24 09:37 1233eclipse在生成javadoc的时候出现乱码,是因为 ... -
java中在静态方法或变量中动态获取当前类的类名
2011-10-14 11:20 3648java中在静态方法中动态获取当前类的类名或者动态获取当前类的 ... -
飞鸽传书实现原理
2011-10-13 11:35 3121飞鸽传书的实现原理: (1)最关键的是局域网用户列表的 ... -
Android开发——利用Cursor+CursorAdapter实现界面实时更新(转)
2011-09-20 15:47 3040好久没有更新博客了 ... -
Nginx使用反向代理时 Hessian 的 411 错误解决方案【转】
2011-09-15 13:48 2327问题描述: 用 Hessian 实现 web se ... -
"Copy" did not complete normally. Please see the log for more information.
2011-05-06 17:19 4003在用android日志的时候老是弹出一个窗口,内容 ... -
jdk api下载地址备份
2011-02-15 10:58 1539中文jdk6的api: JDK6 API 中文版 H ... -
JAVA NIO 简介(转)
2010-12-09 13:03 1023http://www.iteye.com/topic/8344 ... -
netty的资料
2010-10-31 16:06 1255http://hornetq.sourceforge.net/ ... -
Java程序发邮件小例子(转载收藏)
2010-10-27 21:25 1532今天试了一个Java写的发邮件小例子,需要的jar包有 ... -
使用Netty 构造一个异步的httpclient
2010-09-21 00:35 7800原文地址:http://dev.firnow.com/co ... -
Netty使用初步
2010-09-21 00:26 13860收藏自:http://www.kafk ... -
集群环境下SESSION处理(转)
2010-09-08 15:24 1806本文转自:http://blog.csdn.net/l ... -
Java集合的Stack、Queue、Map的遍历
2010-09-08 13:00 1390在集合操作中,常常 ... -
<转>,防止刷新/后退引起的重复提交问题的Java Token代码,非Struts
2009-12-10 13:31 1875贴子转自http://hi.baidu.com/bobylou ...
相关推荐
netty通信时经常和底层数据交互,C语言和java的数据类型和范围不同,通信时需要转化或兼容,附件为字节码、进制常用的转换类。
Java数据压缩与传输实例,可以学习一下实例化套按字、得到文件输入流、压缩输入流、文件输出流、实例化缓冲区、写入数据到文件、关闭输入流、关闭套接字关闭输出流、输出错误信息等Java编程小技巧。 Java数组倒置...
Java数据压缩与传输实例,可以学习一下实例化套按字、得到文件输入流、压缩输入流、文件输出流、实例化缓冲区、写入数据到文件、关闭输入流、关闭套接字关闭输出流、输出错误信息等Java编程小技巧。 Java数组倒置...
Java数据压缩与传输实例,可以学习一下实例化套按字、得到文件输入流、压缩输入流、文件输出流、实例化缓冲区、写入数据到文件、关闭输入流、关闭套接字关闭输出流、输出错误信息等Java编程小技巧。 Java数组倒置...
Java数据压缩与传输实例,可以学习一下实例化套按字、得到文件输入流、压缩输入流、文件输出流、实例化缓冲区、写入数据到文件、关闭输入流、关闭套接字关闭输出流、输出错误信息等Java编程小技巧。 Java数组倒置...
Java数据压缩与传输实例,可以学习一下实例化套按字、得到文件输入流、压缩输入流、文件输出流、实例化缓冲区、写入数据到文件、关闭输入流、关闭套接字关闭输出流、输出错误信息等Java编程小技巧。 Java数组倒置...
Java数据压缩与传输实例,可以学习一下实例化套按字、得到文件输入流、压缩输入流、文件输出流、实例化缓冲区、写入数据到文件、关闭输入流、关闭套接字关闭输出流、输出错误信息等Java编程小技巧。 Java数组倒置...
数据类型转换的基本原则 垃圾回收(GC) Java 集合框架 数组 字符串 GOF 设计模式 SOLID (单一功能、 开闭原则、 里氏替换、 接口隔离以及依赖反转) 设计原则 抽象类与接口 Java 基础, 如 equals 和 hashcode ...
Java Remote Desktop 是一个Java 的远程桌面软件,支持很多特性例如文件传输、数据压缩、颜色转换、键盘鼠标事件转换等等。 最短路径算法实现 k-shortest-paths 这是一个实现了 Yen 的排名算法的无环路径的项目 ...
Java Remote Desktop 是一个Java 的远程桌面软件,支持很多特性例如文件传输、数据压缩、颜色转换、键盘鼠标事件转换等等。 最短路径算法实现 k-shortest-paths 这是一个实现了 Yen 的排名算法的无环路径的项目 ...
Java Remote Desktop 是一个Java 的远程桌面软件,支持很多特性例如文件传输、数据压缩、颜色转换、键盘鼠标事件转换等等。 最短路径算法实现 k-shortest-paths 这是一个实现了 Yen 的排名算法的无环路径的项目 ...
Java Remote Desktop 是一个Java 的远程桌面软件,支持很多特性例如文件传输、数据压缩、颜色转换、键盘鼠标事件转换等等。 最短路径算法实现 k-shortest-paths 这是一个实现了 Yen 的排名算法的无环路径的项目 ...
Java Remote Desktop 是一个Java 的远程桌面软件,支持很多特性例如文件传输、数据压缩、颜色转换、键盘鼠标事件转换等等。 最短路径算法实现 k-shortest-paths 这是一个实现了 Yen 的排名算法的无环路径的项目 ...
Java Remote Desktop 是一个Java 的远程桌面软件,支持很多特性例如文件传输、数据压缩、颜色转换、键盘鼠标事件转换等等。 最短路径算法实现 k-shortest-paths 这是一个实现了 Yen 的排名算法的无环路径的项目 ...
Java Remote Desktop 是一个Java 的远程桌面软件,支持很多特性例如文件传输、数据压缩、颜色转换、键盘鼠标事件转换等等。 最短路径算法实现 k-shortest-paths 这是一个实现了 Yen 的排名算法的无环路径的项目 ...
Java Remote Desktop 是一个Java 的远程桌面软件,支持很多特性例如文件传输、数据压缩、颜色转换、键盘鼠标事件转换等等。 最短路径算法实现 k-shortest-paths 这是一个实现了 Yen 的排名算法的无环路径的项目 ...
Java Remote Desktop 是一个Java 的远程桌面软件,支持很多特性例如文件传输、数据压缩、颜色转换、键盘鼠标事件转换等等。 最短路径算法实现 k-shortest-paths 这是一个实现了 Yen 的排名算法的无环路径的项目 ...
Java Remote Desktop 是一个Java 的远程桌面软件,支持很多特性例如文件传输、数据压缩、颜色转换、键盘鼠标事件转换等等。 最短路径算法实现 k-shortest-paths 这是一个实现了 Yen 的排名算法的无环路径的项目 ...
Java数据压缩与传输实例 1个目标文件 摘要:Java源码,文件操作,数据压缩,文件传输 Java数据压缩与传输实例,可以学习一下实例化套按字、得到文件输入流、压缩输入流、文件输出流、实例化缓冲区、写入数据到文件、...