- 浏览: 232918 次
文章分类
- 全部博客 (160)
- java语言基础 (67)
- jsp基础 (2)
- eclipse使用 (2)
- java源码解读 (6)
- 计算机基础 (3)
- eclipse插件 (0)
- 网络基础 (8)
- 算法 (2)
- linux (0)
- 英语 (0)
- C语言 (4)
- JavaScript (17)
- 数学 (0)
- struts2 (2)
- 自然哲学 (0)
- Servlet (1)
- HttpServer (2)
- ext (1)
- 个人 (1)
- dojo (27)
- spring (2)
- hibernate (4)
- css (3)
- 多线程 (0)
- chrome插件开发 (0)
- svn (0)
- thrift (2)
- phonegap (1)
- java线程 (1)
- 不是很熟悉的css属性 (0)
- 数据库性能调优 (0)
- 项目管理 (1)
- ios (0)
- 软件工程 (0)
- db2 (0)
- 词汇管理 (0)
- zhenyan (0)
- 计划 (0)
- android (0)
- ssss (0)
- 是的 (0)
- dsada (0)
- 泛点是 (0)
- fds (0)
- cxzc (0)
- 权限 (0)
- dfsfds (0)
- http://www.cnblogs.com/kingboy2008/p/5261771.html (0)
- sss (0)
- ddd (0)
- fdsfdsf (0)
- sso (0)
- nginx (0)
- 分布式数据一致性 (0)
- mysql (0)
- ios永久存储 (0)
- js匿名函数 (0)
- 打印机qqq (0)
最新评论
public class BufferedInputStream extends FilterInputStream { private static int defaultBufferSize = 8192; // 内部用来存储数据的缓冲数组 protected volatile byte buf[]; private static final AtomicReferenceFieldUpdater<BufferedInputStream, byte[]> bufUpdater = AtomicReferenceFieldUpdater .newUpdater(BufferedInputStream.class, byte[].class, "buf"); protected int count; protected int pos; protected int markpos = -1; protected int marklimit; //构造器,传入一个输入流,为内部的字节数组开辟空间 public BufferedInputStream(InputStream in) { this(in, defaultBufferSize); } public BufferedInputStream(InputStream in, int size) { super(in); if (size <= 0) { throw new IllegalArgumentException("Buffer size <= 0"); } buf = new byte[size]; } //获取输入流 private InputStream getInIfOpen() throws IOException { InputStream input = in;//in继承自FilterInputStream if (input == null) throw new IOException("Stream closed"); return input; } //获取缓冲数组首地址 private byte[] getBufIfOpen() throws IOException { byte[] buffer = buf; if (buffer == null) throw new IOException("Stream closed"); return buffer; } /** * 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)) { // 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; } /** * See the general contract of the <code>read</code> method of * <code>InputStream</code>. * * @return the next byte of data, or <code>-1</code> if the end of the * stream is reached. * @exception IOException * if this input stream has been closed by invoking its * {@link #close()} method, or an I/O error occurs. * @see java.io.FilterInputStream#in */ public synchronized int read() throws IOException { if (pos >= count) { fill(); if (pos >= count) return -1; } return getBufIfOpen()[pos++] & 0xff; } /** * Read characters into a portion of an array, reading from the underlying * stream at most once if necessary. */ private int read1(byte[] b, int off, int len) throws IOException { int avail = count - pos; if (avail <= 0) { /* * If the requested length is at least as large as the buffer, and * if there is no mark/reset activity, do not bother to copy the * bytes into the local buffer. In this way buffered streams will * cascade harmlessly. */ if (len >= getBufIfOpen().length && markpos < 0) { return getInIfOpen().read(b, off, len); } fill(); avail = count - pos; if (avail <= 0) return -1; } int cnt = (avail < len) ? avail : len; System.arraycopy(getBufIfOpen(), pos, b, off, cnt); pos += cnt; return cnt; } /** * Reads bytes from this byte-input stream into the specified byte array, * starting at the given offset. * * <p> * This method implements the general contract of the corresponding * <code>{@link InputStream#read(byte[], int, int) read}</code> method of * the <code>{@link InputStream}</code> class. As an additional convenience, * it attempts to read as many bytes as possible by repeatedly invoking the * <code>read</code> method of the underlying stream. This iterated * <code>read</code> continues until one of the following conditions becomes * true: * <ul> * * <li>The specified number of bytes have been read, * * <li>The <code>read</code> method of the underlying stream returns * <code>-1</code>, indicating end-of-file, or * * <li>The <code>available</code> method of the underlying stream returns * zero, indicating that further input requests would block. * * </ul> * If the first <code>read</code> on the underlying stream returns * <code>-1</code> to indicate end-of-file then this method returns * <code>-1</code>. Otherwise this method returns the number of bytes * actually read. * * <p> * Subclasses of this class are encouraged, but not required, to attempt to * read as many bytes as possible in the same fashion. * * @param b * destination buffer. * @param off * offset at which to start storing bytes. * @param len * maximum number of bytes to read. * @return the number of bytes read, or <code>-1</code> if the end of the * stream has been reached. * @exception IOException * if this input stream has been closed by invoking its * {@link #close()} method, or an I/O error occurs. */ public synchronized int read(byte b[], int off, int len) throws IOException { getBufIfOpen(); // Check for closed stream if ((off | len | (off + len) | (b.length - (off + len))) < 0) { throw new IndexOutOfBoundsException(); } else if (len == 0) { return 0; } int n = 0; for (;;) { int nread = read1(b, off + n, len - n); if (nread <= 0) return (n == 0) ? nread : n; n += nread; if (n >= len) return n; // if not closed but no bytes available, return InputStream input = in; if (input != null && input.available() <= 0) return n; } } /** * See the general contract of the <code>skip</code> method of * <code>InputStream</code>. * * @exception IOException * if the stream does not support seek, or if this input * stream has been closed by invoking its {@link #close()} * method, or an I/O error occurs. */ public synchronized long skip(long n) throws IOException { getBufIfOpen(); // Check for closed stream if (n <= 0) { return 0; } long avail = count - pos; if (avail <= 0) { // If no mark position set then don't keep in buffer if (markpos < 0) return getInIfOpen().skip(n); // Fill in buffer to save bytes for reset fill(); avail = count - pos; if (avail <= 0) return 0; } long skipped = (avail < n) ? avail : n; pos += skipped; return skipped; } /** * Returns an estimate of the number of bytes that can be read (or skipped * over) from this input stream without blocking by the next invocation of a * method for this input stream. The next invocation might be the same * thread or another thread. A single read or skip of this many bytes will * not block, but may read or skip fewer bytes. * <p> * This method returns the sum of the number of bytes remaining to be read * in the buffer (<code>count - pos</code>) and the result of calling * the {@link java.io.FilterInputStream#in in}.available(). * * @return an estimate of the number of bytes that can be read (or skipped * over) from this input stream without blocking. * @exception IOException * if this input stream has been closed by invoking its * {@link #close()} method, or an I/O error occurs. */ public synchronized int available() throws IOException { return getInIfOpen().available() + (count - pos); } /** * See the general contract of the <code>mark</code> method of * <code>InputStream</code>. * * @param readlimit * the maximum limit of bytes that can be read before the mark * position becomes invalid. * @see java.io.BufferedInputStream#reset() */ public synchronized void mark(int readlimit) { marklimit = readlimit; markpos = pos; } /** * See the general contract of the <code>reset</code> method of * <code>InputStream</code>. * <p> * If <code>markpos</code> is <code>-1</code> (no mark has been set or the * mark has been invalidated), an <code>IOException</code> is thrown. * Otherwise, <code>pos</code> is set equal to <code>markpos</code>. * * @exception IOException * if this stream has not been marked or, if the mark has * been invalidated, or the stream has been closed by * invoking its {@link #close()} method, or an I/O error * occurs. * @see java.io.BufferedInputStream#mark(int) */ public synchronized void reset() throws IOException { getBufIfOpen(); // Cause exception if closed if (markpos < 0) throw new IOException("Resetting to invalid mark"); pos = markpos; } /** * Tests if this input stream supports the <code>mark</code> and * <code>reset</code> methods. The <code>markSupported</code> method of * <code>BufferedInputStream</code> returns <code>true</code>. * * @return a <code>boolean</code> indicating if this stream type supports * the <code>mark</code> and <code>reset</code> methods. * @see java.io.InputStream#mark(int) * @see java.io.InputStream#reset() */ public boolean markSupported() { return true; } public void close() throws IOException { byte[] buffer; while ((buffer = buf) != null) { if (bufUpdater.compareAndSet(this, buffer, null)) { InputStream input = in; in = null; if (input != null) input.close(); return; } // Else retry in case a new buf was CASed in fill() } } }
发表评论
-
mysql并发
2013-01-08 13:38 0/** * 测试msql JDBC连接并发安全性 ... -
java注解2
2013-01-06 22:02 1010由前一篇的代码,运行代码如下:public clas ... -
java注解1
2013-01-06 21:56 945本文演示java注解的使用 1. getDe ... -
Java集合框架分析
2012-08-29 21:28 01. Java集合整体框架 下面的两张图说明 ... -
AbstractList
2012-08-29 20:48 977public abstract class Abstra ... -
Set
2012-08-28 11:17 677public interface Set<E> e ... -
List源码
2012-08-28 11:15 1003public interface List<E&g ... -
Collection源码
2012-08-28 11:13 940public interface Collection< ... -
java集合框架
2012-08-28 10:39 0java的集合框架中,主要有3类常用的集合。 -
web的debug
2012-03-29 10:48 0hh -
文件读取
2012-03-10 19:32 0public class Util { publ ... -
HTML元素的访问
2011-11-30 09:31 0有3忠方法可以访问html中的元素。 -
Schema数据类型
2011-11-26 16:34 0Schema不仅内置了丰富的数据类型,而且还允许开发者 ... -
初学XML3
2011-11-26 10:08 0编写了XML Schema语义约束之后,必须将其导入目 ... -
初学XML2
2011-11-26 09:22 817<?xml version="1.0& ... -
初学XML
2011-11-26 08:50 885<?xml version="1.0&q ... -
JavaScript字符串
2011-11-19 21:29 916JavaScript有三种基本数据类型,字符串,数字以 ... -
项目管理
2011-11-05 22:39 0项目管理开始于项目计划阶段,贯穿于整个系统开发生命周期 ... -
项目可行性分析
2011-11-05 21:23 0项目可行性包括三个方面:技术可行性,经济可行性,组织 ... -
系统开发生命周期
2011-11-05 21:16 0系统开发生命周期有四个4个基本阶段: 计划- ...
相关推荐
BufferedInputStream(缓冲输入流)详解 BufferedInputStream 是 Java 中的一种缓冲输入流,继承自 FilterInputStream。它的主要作用是为另一个输入流添加一些功能,例如提供“缓冲功能”和支持“mark() 标记”和...
BufferedInputStream 是缓冲输入流。它继承于FilterInputStream。 BufferedInputStream 的作用是为另一个输入流添加一些功能,例如,提供“缓冲功能”以及支持“mark()标记”和“reset()重置方法”。
而BufferedInputStream是一种带有缓冲区的输入流,它可以提高数据读取的性能。 在使用InputStream进行数据读取时,数据是直接从底层数据源(如文件或网络连接)中一个字节一个字节地读取,并实时地传递到上层应用。...
BufferedInputStream源码分析图
BufferedInputStream的用法1---马克-to-win java视频
BufferedInputStream的用法3---马克-to-win java视频缓存输字节流
jdk api-BufferedInputStream基础、应用、实战
Java IO中的BufferedInputStream是Java I/O流处理中非常重要的一部分,它属于过滤输入流(FilterInputStream)的一个子类。BufferedInputStream的主要目的是提高输入流的读取效率,通过内部维护一个缓冲区来批量处理...
BufferedInputStream的用法2---马克-to-win java啊视频
java代码,使用字符流读取文件,让新手了解字符流,以及新手学习字符流。在学习过程中进步,用java中的BufferedOutputStream.
BufferedInputStream 的增强版,不使用 available()。 BuffereInputStream 的自定义实现,它根本不调用嵌套流的方法“available()”。 对于那些不阻止它或实现错误的 InputStreams 来说,这可能是合理和有帮助的 ...
如果是文件,则通过`getInputStream`获取流并用`BufferedInputStream`进行缓冲,然后将其写入到目标文件。 ```java ZipFile zipFile = new ZipFile(zipFileName); java.util.Enumeration<ZipEntry> e = zipFile....
在Java I/O流处理中,`BufferedOutputStream` 和 `BufferedInputStream` 是两种非常重要的类,它们分别属于字节缓冲输出流和字节缓冲输入流。这两个类都是为了提高I/O操作的效率,通过在实际操作底层流之前,先将...
- 输入流:InputStream、FileInputStream、ByteArrayInputStream、BufferedInputStream 等。 - 输出流:OutputStream、FileOutputStream、ByteArrayOutputStream、BufferedOutputStream 等。 - **字符流**:字符...
BufferedInputStream bfin = new BufferedInputStream(in); ``` **BufferedOutputStream** 类则是用于处理字节输出的缓冲流,它继承自FilterOutputStream。它同样维护了一个内部缓冲区,允许一次写入多个字节,直到...
为了提高效率,我们通常会结合BufferedInputStream和BufferedOutputStream使用,以实现缓冲功能。本文将详细探讨Java中FileBuffered的InputOutputStream的基本操作。 1. FileInputStream与FileOutputStream: - ...
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream); //创建播放器对象,把文件的缓冲流传入进去 player = new Player(bufferedInputStream); ...
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream); ``` 接下来,我们需要一个输出流来保存从URL获取的数据。在本地文件系统上,这通常是一个`FileOutputStream`: ```java ...
以Java IO中的FileInputStream和BufferedInputStream为例,假设我们有一个FileInputStream实例,如果我们想要添加缓冲功能,可以创建一个BufferedInputStream,将FileInputStream作为参数传递给BufferedInputStream...