标签:des style blog http color io os 使用 java
经过组织考察,令我忽然发觉自己在最常用的Java中也有很多不明白的地方,实为平身一大憾事,今天特意抽时间将这些点滴记录下来,与大家一起分享
第一批想整理的知识点如下:
闲话不多说,先开始今天的主题,研究一下IO的整个结构
从体系结构上划分,IO系统总共分为两大模块, IO和NIO(非阻塞),IO诞生于JDK1.4之前,JDK1.4时,产生了NIO,并且借用NIO重构了部分IO的代码,比如FileInputStream中增加了对NIO进行支持的getChannel()方法,再比如Reader和FileReader基本用nio全部重写了。
一、Think in IO
IO从实现上,大致分为字节流和字符流两种:
FileInputStream is = new FileInputStream("F:\\books\\base\\vim常用指令.txt"); byte[] buff = new byte[BUFFER_SIZE]; int readSize = 0; while ((readSize = is.read(buff)) != -1) { System.out.println(readSize); if(readSize<1024){ byte[] tmp = new byte[readSize]; System.arraycopy(buff, 0, tmp, 0, readSize); System.out.print(new String(tmp, "GBK")); }else{ System.out.print(new String(buff, "GBK")); } }
/** * Returns the default charset of this Java virtual machine. * * <p> The default charset is determined during virtual-machine startup and * typically depends upon the locale and charset of the underlying * operating system. * * @return A charset object for the default charset * * @since 1.5 */ public static Charset defaultCharset() { if (defaultCharset == null) { synchronized (Charset.class) { java.security.PrivilegedAction pa = new GetPropertyAction("file.encoding"); String csn = (String)AccessController.doPrivileged(pa); Charset cs = lookup(csn); if (cs != null) defaultCharset = cs; else defaultCharset = forName("UTF-8"); } } return defaultCharset; }
下面详细叙述一下字节流
有两点注意事项:
a) 使用PipedInputStream的read方法时候要注意,如果缓冲区没有数据的话,会阻塞当前线程,在主线程中运行的话,会卡住不动。
b)PipedOutputStream所在的线程如果停止,那么PipedOutputStream所使用的资源也会回收,会造成pipe 的“broken”,PipedInputStream的read方法也会报错。
“A pipe is said to be broken if a thread that was providing data bytes to the connected piped output stream is no longer alive. ”
FilterInputStream,本身是不能被实例化的,是BufferedInputStream等的父类,其实不创建这个类也可以实现它的子类,这个类内部的方法几乎全部都是复用父类的方法。其实它存在的意义更多是代表一个抽象,意思是在InputStream的基础之上对返回数据进行了重新包装或者处理,处理原因可能各不相同,于是又了各不相同的子类。
LineNumberInputStream,这个类是字节流和字符流转换中的失败产物,已经确定为被废弃,废弃的理由是在字节流中强制的判断读取换行,不考虑编码方面的问题。先不管功能能不能实现,首先从抽象层次上面就有欠缺。挪到字符流里面就皆大欢喜。对应的有LineNumberReader这个类可以使用。具体参见LineNumberReader详解。
DataInputStream,直接读取目标文件的byte,拼接或转化byte为其他基本类型,比如下面方法
public final int readInt() throws IOException { int ch1 = in.read(); int ch2 = in.read(); int ch3 = in.read(); int ch4 = in.read(); if ((ch1 | ch2 | ch3 | ch4) < 0) throw new EOFException(); return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0)); }
public final double readDouble() throws IOException { return Double.longBitsToDouble(readLong()); }
/** * Atomic updater to provide compareAndSet for buf. This is * necessary because closes can be asynchronous. We use nullness * of buf[] as primary indicator that this stream is closed. (The * "in" field is also nulled out on close.) */ private static final AtomicReferenceFieldUpdater<BufferedInputStream, byte[]> bufUpdater = AtomicReferenceFieldUpdater.newUpdater (BufferedInputStream.class, byte[].class, "buf");// 创建原子更新器 ... /** * Fills the buffer with more data, taking into account * shuffling and other tricks for dealing with marks. * Assumes that it is being called by a synchronized method. * This method also assumes that all data has already been read in, * hence pos > count. */ private void fill() throws IOException { byte[] buffer = getBufIfOpen(); if (markpos < 0) pos = 0; /* no mark: throw away the buffer */ else if (pos >= buffer.length) /* no room left in buffer */ if (markpos > 0) { /* can throw away early part of the buffer */ int sz = pos - markpos; System.arraycopy(buffer, markpos, buffer, 0, sz); pos = sz; markpos = 0; } else if (buffer.length >= marklimit) { markpos = -1; /* buffer got too big, invalidate mark */ pos = 0; /* drop buffer contents */ } else { /* grow buffer */ int nsz = pos * 2; if (nsz > marklimit) nsz = marklimit; byte nbuf[] = new byte[nsz]; System.arraycopy(buffer, 0, nbuf, 0, pos); if (!bufUpdater.compareAndSet(this, buffer, nbuf)) {//进行更新比较, 如果buf对象和buffer相同, 那么进行更新,不同的话,不更新 // Can‘t replace buf if there was an async close. // Note: This would need to be changed if fill() // is ever made accessible to multiple threads. // But for now, the only way CAS can fail is via close. // assert buf == null; throw new IOException("Stream closed"); } buffer = nbuf; } count = pos; int n = getInIfOpen().read(buffer, pos, buffer.length - pos); if (n > 0) count = n + pos; }
public ByteArrayInputStream(byte buf[]) public ByteArrayInputStream(byte buf[], int offset, int length)
public int read() throws IOException { if (in == null) { return -1; } int c = in.read(); if (c == -1) { nextStream();// 读完一个流以后, 自动变更下一个,但是这个方法不是线程安全的, 两个一起调,后果十分严重 return read(); } return c; } /** * Continues reading in the next stream if an EOF is reached. */ final void nextStream() throws IOException { if (in != null) { in.close(); } if (e.hasMoreElements()) { in = (InputStream) e.nextElement(); if (in == null) throw new NullPointerException(); } else in = null; }
标签:des style blog http color io os 使用 java
原文地址:http://www.cnblogs.com/driftsky/p/3936364.html