bufferedinputstream

Adding characters to beginning and end of InputStream in Java

好久不见. 提交于 2019-11-28 10:54:51
I have an InputStream which I need to add characters to the beginning and end of, and should end up with another variable of type InputStream . How could I easily do this? You want a SequenceInputStream and a couple of ByteArrayInputStream s. You can use String.getBytes to make the bytes for the latter. SequenceInputStream is ancient, so it's a little clunky to use: InputStream middle ; String beginning = "Once upon a time ...\n"; String end = "\n... and they lived happily ever after."; List<InputStream> streams = Arrays.asList( new ByteArrayInputStream(beginning.getBytes()), middle, new

Usage of BufferedInputStream

半世苍凉 提交于 2019-11-28 04:21:14
Let me preface this post with a single caution. I am a total beginner when it comes to Java. I have been programming PHP on and off for a while, but I was ready to make a desktop application, so I decided to go with Java for various reasons. The application I am working on is in the beginning stages (less than 5 classes) and I need to read bytes from a local file. Typically, the files are currently less than 512kB (but may get larger in the future). Currently, I am using a FileInputStream to read the file into three byte arrays, which perfectly satisfies my requirements. However, I have seen a

Sockets: BufferedOutputStream or just OutputStream?

▼魔方 西西 提交于 2019-11-27 23:59:44
问题 In order to get the fastest transfer speeds over TCP in Java, which is better: Option A: InputStream in = socket.getInputStream(); OutputStream out = socket.getOutputStream(); Option B: BufferedInputStream in = new BufferedInputStream(socket.getInputStream()); BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream()); I've read that performance takes a hit when writing over 8 KiB to OutputStream, it was recommended that it'd be written to it in small chunks not pver 8 KiB

Why does BufferedInputStream copy a field to a local variable rather than use the field directly

孤街浪徒 提交于 2019-11-27 19:53:27
问题 When I read the source code from java.io.BufferedInputStream.getInIfOpen() , I am confused about why it wrote code like this: /** * Check to make sure that underlying input stream has not been * nulled out due to close; if not return it; */ private InputStream getInIfOpen() throws IOException { InputStream input = in; if (input == null) throw new IOException("Stream closed"); return input; } Why is it using the alias instead of using the field variable in directly like below: /** * Check to

Why is the performance of BufferedReader so much worse than BufferedInputStream?

こ雲淡風輕ζ 提交于 2019-11-27 14:55:19
问题 I understand that using a BufferedReader (wrapping a FileReader) is going to be significantly slower than using a BufferedInputStream (wrapping a FileInputStream), because the raw bytes have to be converted to characters. But I don't understand why it is so much slower! Here are the two code samples that I'm using: BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(filename)); try { byte[] byteBuffer = new byte[bufferSize]; int numberOfBytes; do { numberOfBytes =

Get FileNotFoundException when initialising FileInputStream with File object

耗尽温柔 提交于 2019-11-27 07:47:40
问题 I am trying to initialise a FileInputStream object using a File object. I am getting a FileNotFound error on the line fis = new FileInputStream(file); This is strange since I have opened this file through the same method to do regex many times. My method is as follows: private BufferedInputStream fileToBIS(File file){ FileInputStream fis = null; BufferedInputStream bis =null; try { fis = new FileInputStream(file); bis = new BufferedInputStream(fis); } catch (FileNotFoundException e) { // TODO

Java BufferedReader back to the top of a text file?

好久不见. 提交于 2019-11-27 04:46:05
I currently have 2 BufferedReader s initialized on the same text file. When I'm done reading the text file with the first BufferedReader , I use the second one to make another pass through the file from the top. Multiple passes through the same file are necessary. I know about reset() , but it needs to be preceded with calling mark() and mark() needs to know the size of the file, something I don't think I should have to bother with. Ideas? Packages? Libs? Code? Thanks TJ What's the disadvantage of just creating a new BufferedReader to read from the top? I'd expect the operating system to cache

Adding characters to beginning and end of InputStream in Java

一笑奈何 提交于 2019-11-27 03:52:35
问题 I have an InputStream which I need to add characters to the beginning and end of, and should end up with another variable of type InputStream . How could I easily do this? 回答1: You want a SequenceInputStream and a couple of ByteArrayInputStreams. You can use String.getBytes to make the bytes for the latter. SequenceInputStream is ancient, so it's a little clunky to use: InputStream middle ; String beginning = "Once upon a time ...\n"; String end = "\n... and they lived happily ever after.";

Usage of BufferedInputStream

两盒软妹~` 提交于 2019-11-27 00:22:15
问题 Let me preface this post with a single caution. I am a total beginner when it comes to Java. I have been programming PHP on and off for a while, but I was ready to make a desktop application, so I decided to go with Java for various reasons. The application I am working on is in the beginning stages (less than 5 classes) and I need to read bytes from a local file. Typically, the files are currently less than 512kB (but may get larger in the future). Currently, I am using a FileInputStream to

Download binary file from OKHTTP

╄→尐↘猪︶ㄣ 提交于 2019-11-26 14:13:19
I am using OKHTTP client for networking in my android application. This example shows how to upload binary file. I would like to know how to get inputstream of binary file downloading with OKHTTP client. Here is the listing of the example : public class InputStreamRequestBody extends RequestBody { private InputStream inputStream; private MediaType mediaType; public static RequestBody create(final MediaType mediaType, final InputStream inputStream) { return new InputStreamRequestBody(inputStream, mediaType); } private InputStreamRequestBody(InputStream inputStream, MediaType mediaType) { this