파일 복사 전용 함수. 

Package 안에 넣어두면 반복해서 쓸 수 있어 코드를 절약할 수 있음.

InputStream, OutputStream을 Close하는 옵션이 따로 있음.


package mp3example;


import java.io.InputStream;

import java.io.OutputStream;

public class CopyUtil {

public static void copy(InputStream in, OutputStream out, boolean inClose, boolean outClose)throws Exception{

byte[] buffer = new byte[1024*8];

while(true){

int count = in.read(buffer);

if(count == -1){ break; }

out.write(buffer,0, count);

}

out.flush();

if(inClose){

in.close();

}

if(outClose){

out.close();

}

}

}



Posted by 타다키치
,

추상클래스. 특정 클래스 안에서 불려진 후 소캣 연결을 맺고 파일, 문자 입출력을 맡음.

이같은 방식으로 코드를 절약.

메소드 두 개(sendMsg, receiveMsg)는 필요에 따라(문자나 파일을 보내고 받는 등) 오버라이드해야 함.




package mp3example;


import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.net.Socket;

public abstract class ClientAgent {

private String ip;

private int port;

protected Socket socket;

protected DataInputStream din;

protected DataOutputStream dos;

public ClientAgent(String ip, int port) {

super();

this.ip = ip;

this.port = port;

}

public final void doExecute()throws Exception{

try{

connect();

sendMsg();

receiveMsg();

}finally{

closeAll();

}

}

private void connect() throws Exception{

this.socket = new Socket(this.ip, this.port);

this.din = new DataInputStream(this.socket.getInputStream());

this.dos = new DataOutputStream(this.socket.getOutputStream());

}

protected abstract void sendMsg()throws Exception;

protected abstract void receiveMsg()throws Exception;

private void closeAll() {

if(this.din != null){

try{ din.close(); }catch(Exception e){}

}

if(this.dos != null){

try{ dos.close(); }catch(Exception e){}

}

if(this.socket != null){

try{ this.socket.close(); }catch(Exception e){}

}

}

}

Posted by 타다키치
,

DataIn & Out 간단한 예제

먼저 파일 제목을 보내고 나서 파일 스트림을 쏴주는 방식



package io4;

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.FileInputStream;

import java.io.InputStream;

import java.net.ServerSocket;

import java.net.Socket;


public class DataInEx {

//bad code

public static void main(String[] args) throws Exception{

ServerSocket server = new ServerSocket(5555);

System.out.println("ready server....");

Socket socket = server.accept();

System.out.println("client connected..." + socket);

DataOutputStream clientDos 

 = new DataOutputStream(socket.getOutputStream());

String title = "My Fathers Eyes.mp3";

clientDos.writeUTF(title);

InputStream in = new FileInputStream("C:\\zzz\\My Fathers Eyes.mp3");

byte[] buffer = new byte[1024*8];

while (true){

int count = in.read(buffer);

if(count == -1){

break;

}

clientDos.write(buffer,0, count);

}

clientDos.flush();

clientDos.close();

}

}



-------------------------------------------------------------------------------------------------


package io4;

import java.io.DataInputStream;

import java.io.FileOutputStream;

import java.net.Socket;


public class DataOutEx {

//bad code

public static void main(String[] args) throws Exception{

Socket socket = new Socket("127.0.0.1", 5555);

DataInputStream din =

new DataInputStream(socket.getInputStream());

String title = din.readUTF();

System.out.println(title);

FileOutputStream fos 

= new FileOutputStream("C:\\zzz\\copy_" +title);

byte[] buffer = new byte[1024*8];

while(true){

int count = din.read(buffer);

if(count == -1){ break;}

fos.write(buffer,0,count);

}

}

}

Posted by 타다키치
,