728x90
Byte를 1024단위로 보내주는게 어느 하나의 규약이라고들 하시길래.. 한 번 만들어 봤습니다.
(사실 규약이겠어요?... 이게 안전하니까!)
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | import java.io.*; public class server { final static int maxBufferSize = 1024; public static void main(String[] args) { try { File file = new File("C:\\sendData\\netty-all-4.1.0.CR3.jar"); DataInputStream diStream = new DataInputStream(new FileInputStream(file)); long len = (int) file.length(); byte[] fileBytes = new byte[(int) len]; int read = 0; int numRead = 0; while (read < fileBytes.length && (numRead = diStream.read(fileBytes, read, fileBytes.length - read)) >= 0) { read = read + numRead; } if ((len / maxBufferSize) < 1) { System.out.println("바이트 배열이 " + maxBufferSize + " 이하입니다."); System.out.println("Single i length : " + len); } else { int bufferNumber = 1; int bufferMod = 0; byte multipleBuffer[][]; bufferMod = (int) (len % maxBufferSize); bufferNumber = (int) (len / maxBufferSize); multipleBuffer = new byte[bufferNumber + 1][1024]; System.out.println("bufferNumber : " + bufferNumber); System.out.println("bufferMod : " + bufferMod); System.out.println("바이트 배열이 1024 이상입니다. 2배열이상 생성됩니다."); long count = 0; for (int i = 0; i <= bufferNumber; i++) { if (i != bufferNumber) { for (int j = 0; j < maxBufferSize; j++) { multipleBuffer[i][j] = fileBytes[(int) count]; count++; } } else if (i == bufferNumber) { for (int j = 0; j < bufferMod; j++) { multipleBuffer[i][j] = fileBytes[(int) count]; count++; } } } System.out.println("multiple i length : " + multipleBuffer.length); System.out.println("multiple j length : " + multipleBuffer[multipleBuffer.length - 1].length); } } catch (Exception e) { e.printStackTrace(); } } } | cs |
1 2 3 4 5 | bufferNumber : 3140 bufferMod : 753 바이트 배열이 1024 이상입니다. 2배열이상 생성됩니다. multiple i length : 3141 multiple j length : 1024 | cs |
위의 개념을 다시 Socket 상에서 돌릴려면 이런식으로 이루어져야 하겠지요
물론 데이터는 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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | public boolean isMultipleByteorArray(String path) { File file = new File(path); long len = (int) file.length(); if ((len / maxBufferSize) < 1) { return false; } else { return true; } } public byte[] toByteArray(String path) { try { File file = new File(path); DataInputStream diStream = new DataInputStream(new FileInputStream(file)); long len = (int) file.length(); byte[] fileBytes = new byte[(int) len]; int read = 0; int numRead = 0; while (read < fileBytes.length && (numRead = diStream.read(fileBytes, read, fileBytes.length - read)) >= 0) { read = read + numRead; } diStream.close(); return fileBytes; } catch (Exception e) { e.printStackTrace(); } return null; } public String[] toSplitByteArray(String path) { try { // File file = new File("C:\\sendData\\netty-all-4.1.0.CR3.jar"); File file = new File(path); DataInputStream diStream = new DataInputStream(new FileInputStream(file)); long len = (int) file.length(); byte[] fileBytes = new byte[(int) len]; int read = 0; int numRead = 0; while (read < fileBytes.length && (numRead = diStream.read(fileBytes, read, fileBytes.length - read)) >= 0) { read = read + numRead; } diStream.close(); if ((len / maxBufferSize) >= 1) { int bufferNumber = 1; int bufferMod = 0; String multipleBuffer[]; bufferMod = (int) (len % maxBufferSize); bufferNumber = (int) (len / maxBufferSize); multipleBuffer = new String[bufferNumber + 1]; for (int i = 0; i <= bufferNumber; i++) { byte tempBytes[] = new byte[maxBufferSize]; System.out.println(i); if (i == 0) { System.arraycopy(fileBytes, 0, tempBytes, 0, maxBufferSize); } else if (i == bufferNumber) { System.arraycopy(fileBytes, maxBufferSize * i + 1, tempBytes, 0, bufferMod - 1); } else { System.arraycopy(fileBytes, maxBufferSize * i + 1, tempBytes, 0, maxBufferSize); } multipleBuffer[i] = new String(tempBytes); } return multipleBuffer; } } catch (Exception e) { e.printStackTrace(); } return null; } | cs |
보내는 곳에서는
이런식으루!!
1 2 3 4 5 6 7 8 9 10 11 12 13 | File file = new File(sendfilepath); if(isMultipleByteorArray(file.getAbsolutePath())) // Multiple Byte array { String[] fileBytes = toSplitByteArray(file.getAbsolutePath()); String header = "filelength:" + file.length() + "lengthEnd"; String ender = "fileEnd:" + file.getName(); ch.writeAndFlush(header.getBytes()); System.out.println("i : " + fileBytes.length); for (int i = 0; i < fileBytes.length; i++) { ch.writeAndFlush(fileBytes[i].getBytes()); } ch.writeAndFlush(ender.getBytes()); .... | cs |