三、使用Okio框架进行输入操作(Source)
2015-03-26 11:11
120 查看
Source相当于输入流(InputStream)。把硬盘中的数据输入到内存中。
![](http://s3.51cto.com/wyfs02/M01/5B/CF/wKiom1UTcZTSZNnCAABUplp4Pnk401.jpg)
例子:
Api文档
本文出自 “我的编程之路” 博客,请务必保留此出处http://aiwoapp.blog.51cto.com/8677066/1624681
![](http://s3.51cto.com/wyfs02/M01/5B/CF/wKiom1UTcZTSZNnCAABUplp4Pnk401.jpg)
例子:
try { File file = new File("test.txt"); BufferedSource source = Okio.buffer(Okio.source(file)); //byte[] data = source.readByteArray(); //System.out.println(new String(data, Charset.forName("UTF-8"))); BufferedSink sink = Okio.buffer(Okio.sink(new File("test2.txt"))); source.readAll(sink); sink.close(); source.close(); } catch (IOException e) { e.printStackTrace(); }
Api文档
/** * Returns this source's internal buffer. */ Buffer buffer(); /** * Returns true if there are no more bytes in this source. This will block * until there are bytes to read or the source is definitely exhausted. */ boolean exhausted() throws IOException; /** * Returns when the buffer contains at least {@code byteCount} bytes. Throws * an {@link java.io.EOFException} if the source is exhausted before the * required bytes can be read. */ void require(long byteCount) throws IOException; /** * Returns true when the buffer contains at least {@code byteCount} bytes, * expanding it as necessary. Returns false if the source is exhausted before * the requested bytes can be read. */ boolean request(long byteCount) throws IOException; /** * Removes a byte from this source and returns it. */ byte readByte() throws IOException; /** * Removes two bytes from this source and returns a big-endian short. */ short readShort() throws IOException; /** * Removes two bytes from this source and returns a little-endian short. */ short readShortLe() throws IOException; /** * Removes four bytes from this source and returns a big-endian int. */ int readInt() throws IOException; /** * Removes four bytes from this source and returns a little-endian int. */ int readIntLe() throws IOException; /** * Removes eight bytes from this source and returns a big-endian long. */ long readLong() throws IOException; /** * Removes eight bytes from this source and returns a little-endian long. */ long readLongLe() throws IOException; /** * Reads a long from this source in signed decimal form (i.e., as a string in base 10 with * optional leading '-'). This will iterate until a non-digit character is found. * * @throws NumberFormatException if the found digits do not fit into a {@code long} or a decimal * number was not present. */ long readDecimalLong() throws IOException; /** * Reads a long form this source in hexadecimal form (i.e., as a string in base 16). This will * iterate until a non-hexadecimal character is found. * * @throws NumberFormatException if the found hexadecimal does not fit into a {@code long} or * hexadecimal was not found. */ long readHexadecimalUnsignedLong() throws IOException; /** * Reads and discards {@code byteCount} bytes from this source. Throws an * {@link java.io.EOFException} if the source is exhausted before the * requested bytes can be skipped. */ void skip(long byteCount) throws IOException; /** * Removes all bytes bytes from this and returns them as a byte string. */ ByteString readByteString() throws IOException; /** * Removes {@code byteCount} bytes from this and returns them as a byte string. */ ByteString readByteString(long byteCount) throws IOException; /** * Removes all bytes from this and returns them as a byte array. */ byte[] readByteArray() throws IOException; /** * Removes {@code byteCount} bytes from this and returns them as a byte array. */ byte[] readByteArray(long byteCount) throws IOException; /** * Removes up to {@code sink.length} bytes from this and copies them into {@code sink}. * Returns the number of bytes read, or -1 if this source is exhausted. */ int read(byte[] sink) throws IOException; /** * Removes exactly {@code sink.length} bytes from this and copies them into {@code sink}. * Throws an {@link java.io.EOFException} if the requested number of bytes cannot be read. */ void readFully(byte[] sink) throws IOException; /** * Removes up to {@code byteCount} bytes from this and copies them into {@code sink} at * {@code offset}. Returns the number of bytes read, or -1 if this source is exhausted. */ int read(byte[] sink, int offset, int byteCount) throws IOException; /** * Removes exactly {@code byteCount} bytes from this and appends them to * {@code sink}. Throws an {@link java.io.EOFException} if the requested * number of bytes cannot be read. */ void readFully(Buffer sink, long byteCount) throws IOException; /** * Removes all bytes from this and appends them to {@code sink}. Returns the * total number of bytes written to {@code sink} which will be 0 if this is * exhausted. */ long readAll(Sink sink) throws IOException; /** * Removes all bytes from this, decodes them as UTF-8, and returns the string. */ String readUtf8() throws IOException; /** * Removes {@code byteCount} bytes from this, decodes them as UTF-8, and * returns the string. */ String readUtf8(long byteCount) throws IOException; /** * Removes and returns characters up to but not including the next line break. * A line break is either {@code "\n"} or {@code "\r\n"}; these characters are * not included in the result. * <p/> * <p><strong>On the end of the stream this method returns null,</strong> just * like {@link java.io.BufferedReader}. If the source doesn't end with a line * break then an implicit line break is assumed. Null is returned once the * source is exhausted. Use this for human-generated data, where a trailing * line break is optional. */ String readUtf8Line() throws IOException; /** * Removes and returns characters up to but not including the next line break. * A line break is either {@code "\n"} or {@code "\r\n"}; these characters are * not included in the result. * <p/> * <p><strong>On the end of the stream this method throws.</strong> Every call * must consume either '\r\n' or '\n'. If these characters are absent in the * stream, an {@link java.io.EOFException} is thrown. Use this for * machine-generated data where a missing line break implies truncated input. */ String readUtf8LineStrict() throws IOException; /** * Removes all bytes from this, decodes them as {@code charset}, and returns * the string. */ String readString(Charset charset) throws IOException; /** * Removes {@code byteCount} bytes from this, decodes them as {@code charset}, * and returns the string. */ String readString(long byteCount, Charset charset) throws IOException; /** * Returns the index of the first {@code b} in the buffer. This expands the * buffer as necessary until {@code b} is found. This reads an unbounded * number of bytes into the buffer. Returns -1 if the stream is exhausted * before the requested byte is found. */ long indexOf(byte b) throws IOException; /** * Returns the index of the first {@code b} in the buffer at or after {@code * fromIndex}. This expands the buffer as necessary until {@code b} is found. * This reads an unbounded number of bytes into the buffer. Returns -1 if the * stream is exhausted before the requested byte is found. */ long indexOf(byte b, long fromIndex) throws IOException; /** * Returns the index of the first byte in {@code targetBytes} in the buffer. * This expands the buffer as necessary until a target byte is found. This * reads an unbounded number of bytes into the buffer. Returns -1 if the * stream is exhausted before the requested byte is found. */ long indexOfElement(ByteString targetBytes) throws IOException; /** * Returns the index of the first byte in {@code targetBytes} in the buffer * at or after {@code fromIndex}. This expands the buffer as necessary until * a target byte is found. This reads an unbounded number of bytes into the * buffer. Returns -1 if the stream is exhausted before the requested byte is * found. */ long indexOfElement(ByteString targetBytes, long fromIndex) throws IOException; /** * Returns an input stream that reads from this source. */ InputStream inputStream();
本文出自 “我的编程之路” 博客,请务必保留此出处http://aiwoapp.blog.51cto.com/8677066/1624681
相关文章推荐
- 二、使用Okio框架进行输出操作(Sink)
- 【hibernate框架】使用Annotation注解进行实体类操作
- DataGridView中使用BindSource进行数据绑定操作
- 无废话Android之android下junit测试框架配置、保存文件到手机内存、android下文件访问的权限、保存文件到SD卡、获取SD卡大小、使用SharedPreferences进行数据存储、使用Pull解析器操作XML文件、android下操作sqlite数据库和事务(2)
- 使用iOS原生sqlite3框架对sqlite数据库进行操作
- 在EXT框架中,使用JS文件设置UEditor文本框,出现新增内容很多,页面变型,不出现滚动条,导致无法进行操作。
- Web自动化框架LazyUI使用手册(6)--8种控件对应的class,及可对其进行的操作
- Web自动化框架LazyUI使用手册(6)--8种控件对应的class,及可对其进行的操作
- 使用DBUtil框架进行多表操作
- 在Java中使用标准输入输出设备进行字符串,整数浮点数等 的输入输出操作
- 使用MyBatis框架进行数据库的增删改查操作
- struts2使用XML框架进行输入校验
- 简单的三层框架以及使用dbutils进行数据库操作(入门)
- 简单的三层框架以及使用dbutils进行数据库操作(入门)
- 简单的三层框架以及使用dbutils进行数据库操作(入门)
- 简单的三层框架以及使用dbutils进行数据库操作(入门)
- DataGridView中使用BindSource进行数据绑定操作 推荐
- day03-spring与数据库操作的框架解析即dataSource的获取(JDBC)、使用spring中已经有的事务进行JDBC操作
- 使用iOS原生sqlite3框架对sqlite数据库进行操作
- JavaScript对DOM节点进行操作(不使用第三方框架)