Skip to content

Instantly share code, notes, and snippets.

@Brutt
Last active September 27, 2018 08:50
Show Gist options
  • Save Brutt/32b8d398050a3987cf04d4d930f51458 to your computer and use it in GitHub Desktop.
Save Brutt/32b8d398050a3987cf04d4d930f51458 to your computer and use it in GitHub Desktop.
import java.io.IOException;
import java.io.InputStream;
public class BufferedInputStream extends InputStream {
private InputStream inputStream;
private byte[] buffer = new byte[5];
private int start;
private int count;
public BufferedInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
@Override
public int read() throws IOException {
if (start == count) {
count = inputStream.read(buffer);
start = 0;
}
if (count == -1) {
return -1;
}
int value = buffer[start];
start++;
return value;
}
@Override
public int read(byte[] b) throws IOException {
return super.read(b);
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
if (b.length < len){
throw new IndexOutOfBoundsException();
} else if (len == 0){
return 0;
}
int n = 0;
while(true) {
if (buffer.length < len - n) {
count = inputStream.read(buffer, off + n, buffer.length - n);
} else {
count = inputStream.read(buffer, off + n, len);
}
if (count == -1) {
return -1;
}
System.arraycopy(buffer, off + n, b, n, count);
}
}
@Override
public void close() throws IOException {
buffer = null;
inputStream.close();
}
}
package com.petrovskiy.Streams;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class BufferedInputStream extends InputStream {
private static final int DEFAULT_BUFFER_SIZE = 5;
private InputStream inputStream;
private byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
private int index;
private int count;
public BufferedInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
@Override
public int read() throws IOException {
if (index == count) {
count = inputStream.read(buffer);
index = 0;
}
if (count == -1) {
return -1;
}
int value = buffer[index];
index++;
return value;
}
@Override
public int read(byte[] b) throws IOException {
int value;
int indexB = 0;
while ((value = read()) != -1) {
if (indexB >= b.length){
return indexB;
}
b[indexB] = (byte) value;
indexB++;
}
return indexB;
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
if (b.length < len){
throw new IndexOutOfBoundsException("Length parameter is higher than array length!");
} else if (len == 0){
return 0;
}
int index = 0;
while(count != -1) {
if (buffer.length < len - index) {
count = inputStream.read(buffer, off + index, buffer.length - index);
} else {
count = inputStream.read(buffer, off + index, len);
}
System.arraycopy(buffer, index, b, index, count);
}
return -1;
}
@Override
public void close() throws IOException {
buffer = null;
inputStream.close();
}
}
class Test{
public static void main(String[] args) throws IOException {
InputStream inputStream = new BufferedInputStream(new FileInputStream("1.txt"));
byte[] res = new byte[7];
System.out.println(inputStream.read(res));
for (byte re : res) {
System.out.println((char)re);
}
}
}
import java.io.FileInputStream;
import java.io.IOException;
public class Test {
public static void main(String[] args) throws IOException {
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("1.txt"));
byte[] b = new byte[10];
bufferedInputStream.read(b,0,6);
for (byte b1 : b) {
System.out.println(b1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment