Advertisement
Guest User

Untitled

a guest
Nov 21st, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.17 KB | None | 0 0
  1. package project.serverside;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.OutputStream;
  6. import java.net.Socket;
  7.  
  8. public class ClientConnection implements Transmitable{
  9.  
  10.     private OutputStream outputStream;
  11.     private InputStream inputStream;
  12.     private boolean[] crash;
  13.  
  14.     ClientConnection(Socket socket,boolean[] crash){
  15.         this.crash = crash;
  16.         try {
  17.             this.inputStream = socket.getInputStream();
  18.             this.outputStream = socket.getOutputStream();
  19.         } catch (IOException e) {
  20.             e.printStackTrace();
  21.         }
  22.     }
  23.  
  24.     @Override
  25.     public void sendBytes(int action){
  26.         try {
  27.             outputStream.write(action);
  28.         } catch (IOException e) {
  29.             e.printStackTrace();
  30.         }
  31.     }
  32.     @Override
  33.     public void sendBytes(byte[] buffer){
  34.         try {
  35.             outputStream.write(buffer);
  36.         } catch (IOException e) {
  37.             e.printStackTrace();
  38.         }
  39.     }
  40.  
  41.     @Override
  42.     public void sendBytes(String text){
  43.         byte[] buffer = text.getBytes();
  44.         try {
  45.             outputStream.write(text.length());
  46.             outputStream.write(buffer);
  47.         } catch (IOException e) {
  48.             e.printStackTrace();
  49.         }
  50.     }
  51.     @Override
  52.     public int getNextAction(){
  53.         int action = 0;
  54.         try {
  55.             action = inputStream.read();
  56.             if (action == -1){
  57.                 crash[0] = true;
  58.                 return -1;
  59.             }
  60.         } catch (IOException e) {
  61.             e.printStackTrace();
  62.         }
  63.         return action;
  64.     }
  65.  
  66.     @Override
  67.     public byte[] getNextBytes() {
  68.         byte[] buffer = null;
  69.         int read;
  70.         try {
  71.             read = inputStream.read(buffer);
  72.             if (read == -1) {
  73.                 crash[0] = true;
  74.                 return null;
  75.             }
  76.         } catch (IOException e) {
  77.             e.printStackTrace();
  78.         }
  79.         return buffer;
  80.     }
  81.  
  82.     @Override
  83.     public String getNextString(int min,int max){
  84.         int read;
  85.         byte[] buffer;
  86.         String temp = null;
  87.         try {
  88.             read = inputStream.read();
  89.             if (read < min || read > max){
  90.                 crash[0] = true;
  91.                 return null;
  92.             }
  93.             buffer = new byte[read];
  94.             read = inputStream.read(buffer);
  95.             if (read != buffer.length){
  96.                 crash[0] = true;
  97.                 return null;
  98.             }
  99.             temp = new String(buffer);
  100.         } catch (IOException e) {
  101.             e.printStackTrace();
  102.         }
  103.         return temp;
  104.     }
  105.  
  106.     @Override
  107.     public String getNextString(){
  108.         int read;
  109.         byte[] buffer;
  110.         String temp = null;
  111.         try {
  112.             read = inputStream.read();
  113.             buffer = new byte[read];
  114.             read = inputStream.read(buffer);
  115.             if (read != buffer.length){
  116.                 crash[0] = true;
  117.                 return null;
  118.             }
  119.             temp = new String(buffer);
  120.         } catch (IOException e) {
  121.             e.printStackTrace();
  122.         }
  123.         return temp;
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement