본문 바로가기
✨ Java/etc

자바(JAVA) - ByteArrayInputStream과 ByteArrayOutputStream

by 환풍 2023. 7. 24.
728x90

 

ByteArrayInputStream이란?

바이트 배열('byte[ ]')에 저장된 데이터를 읽기 위한 입력 스트림이다.
객체가 생성될 때, 읽어올 데이터를 가리키는 바이트 배열이 전달된다.

 

ByteArrayOutputStream이란?

바이트배열('byte[ ]')에 데이터를 쓰기 위한 출력 스트림이다.
객체가 생성될 때, 쓰여진 데이터를 저장할 바이트 배열이 생성되며 데이터를 기록할 때마다 해당 바이트 배열에 데이터가 쌓인다.

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package io1;
 
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Arrays;
 
public class IOEx1 {
 
    public static void main(String[] args) {
        byte[] inSrc= {0,1,2,3,4,5,6,7,8,9};
        byte[] outSrc = null;
        
        ByteArrayInputStream input = null;
        ByteArrayOutputStream output = null;
        
        input = new ByteArrayInputStream(inSrc);
        output = new ByteArrayOutputStream();
        
        int data = 0;
        
        while((data = input.read())!= -1) {
            //System.out.println(input.read());
            output.write(data); 
        }
        outSrc = output.toByteArray(); // outSrc에 output에 저장된 데이터가 복사됨
        
        System.out.println("Input Source : " + Arrays.toString(inSrc));
        System.out.println("Output Source : " + Arrays.toString(outSrc));
    }
}
cs

input.read( ) 메서드는 스트림으로부터 바이트 데이터를 읽어와서 정수 형태로 반환한다.

반환된 정수 값은 실제 읽어온 바이트 데이터에 해당하며, 0부터 255 사이의 값을 갖는다.

읽을 데이터가 없을 경우 '-1'을 반환한다.

 

output.write( ) 메서드는 ByteArrayOutputStream 객체에 데이터를 쓰는 메서드이다.

 

따라서 output.toByteArray( ) 를 호출하면, output에 쌓여있던 데이터를 바이트 배열로 얻을 수 있다.

 


 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package io1;
 
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Arrays;
 
public class IOEx2 {
 
    public static void main(String[] args) {
        
        byte[] inSrc = {0,1,2,3,4,5,6,7,8,9};
        byte[] outSrc = null;
        
        byte[] temp = new byte[10];
        
        ByteArrayInputStream input = null;
        ByteArrayOutputStream output = null;
        
        input = new ByteArrayInputStream(inSrc);
        output = new ByteArrayOutputStream();
        
        input.read(temp, 0, temp.length); // 읽어 온 데이터를 배열 temp에 담는다.
        output.write(temp,5 , 5); // temp[5]부터 5개의 데이터를 write한다.
        outSrc = output.toByteArray();
        
        System.out.println("Input Source : " + Arrays.toString(inSrc));
        System.out.println("temp : " + Arrays.toString(temp));
        System.out.println("Output Source : " + Arrays.toString(outSrc));
    }
}
cs

 


 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package io1;
 
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Arrays;
 
public class IOEx3 {
 
    public static void main(String[] args) {
        byte[] inSrc = {0,1,2,3,4,5,6,7,8,9};
        byte[] outSrc = null;
        
        byte[] temp = new byte[4];
        
        ByteArrayInputStream input = null;
        ByteArrayOutputStream output = null;
        
        input = new ByteArrayInputStream(inSrc);
        output = new ByteArrayOutputStream();
        
        System.out.println("Input Source : " + Arrays.toString(inSrc));
        
        try {
            while(input.available()>0) {
                input.read(temp);
                output.write(temp);
                System.out.println("temp : " + Arrays.toString(temp));
                
                outSrc = output.toByteArray();
                System.out.println("Output Source : " + Arrays.toString(outSrc));
            }
        }catch (Exception e) {
            // TODO: handle exception
        }
    }
}
cs

while( input.available()>0 )은 ByteArrayInputStream 객체에서 읽을 수 있는 데이터가 있는지 확인하는 조건이다.

available( ) 메서드는 ByteArrayInputStream 객체에서 읽을 수 있는 바이트 수를 반환한다.

즉, 해당 스트림으로부터 현재 읽을 수 있는 데이터의 양을 알려준다.

 

input.read(temp)는 temp 배열에 입력 스트림으로부터 최대 4바이트 크기만큼 데이터를 읽어왔다.

output.write(temp)는 temp 배열에 있는 데이터를 출력 스트림에 썼다.

 

outSrc에 output.toByteArray()를 통해 출력 스트림에 있는 데이터를 바이트 배열로 얻어오면서 출력했다.

 


 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package io1;
 
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Arrays;
 
public class IOEx4 {
    public static void main(String[] args) {
        
        byte[] inSrc = {0,1,2,3,4,5,6,7,8,9};
        byte[] outSrc = null;
        
        byte[] temp = new byte[4];
        
        ByteArrayInputStream input = null;
        ByteArrayOutputStream output = null;
        
        input = new ByteArrayInputStream(inSrc);
        output = new ByteArrayOutputStream();
        
        try {
            while(input.available()>0) {
                int len = input.read(temp); // 읽어온 데이터 개수 반환
                output.write(temp, 0, len); // 읽어온 만큼만 write함.
            }
        }catch (Exception e) {
            // TODO: handle exception
        }
        
        outSrc = output.toByteArray();
        
        System.out.println("Input Source : " + Arrays.toString(inSrc));
        System.out.println("temp : " + Arrays.toString(temp));
        System.out.println("Output Source : " + Arrays.toString(outSrc));
    }
}
 
cs

반응형

댓글