Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package project.serverside;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.Socket;
- public class ClientConnection implements Transmitable{
- private OutputStream outputStream;
- private InputStream inputStream;
- private boolean[] crash;
- ClientConnection(Socket socket,boolean[] crash){
- this.crash = crash;
- try {
- this.inputStream = socket.getInputStream();
- this.outputStream = socket.getOutputStream();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- @Override
- public void sendBytes(int action){
- try {
- outputStream.write(action);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- @Override
- public void sendBytes(byte[] buffer){
- try {
- outputStream.write(buffer);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- @Override
- public void sendBytes(String text){
- byte[] buffer = text.getBytes();
- try {
- outputStream.write(text.length());
- outputStream.write(buffer);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- @Override
- public int getNextAction(){
- int action = 0;
- try {
- action = inputStream.read();
- if (action == -1){
- crash[0] = true;
- return -1;
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- return action;
- }
- @Override
- public byte[] getNextBytes() {
- byte[] buffer = null;
- int read;
- try {
- read = inputStream.read(buffer);
- if (read == -1) {
- crash[0] = true;
- return null;
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- return buffer;
- }
- @Override
- public String getNextString(int min,int max){
- int read;
- byte[] buffer;
- String temp = null;
- try {
- read = inputStream.read();
- if (read < min || read > max){
- crash[0] = true;
- return null;
- }
- buffer = new byte[read];
- read = inputStream.read(buffer);
- if (read != buffer.length){
- crash[0] = true;
- return null;
- }
- temp = new String(buffer);
- } catch (IOException e) {
- e.printStackTrace();
- }
- return temp;
- }
- @Override
- public String getNextString(){
- int read;
- byte[] buffer;
- String temp = null;
- try {
- read = inputStream.read();
- buffer = new byte[read];
- read = inputStream.read(buffer);
- if (read != buffer.length){
- crash[0] = true;
- return null;
- }
- temp = new String(buffer);
- } catch (IOException e) {
- e.printStackTrace();
- }
- return temp;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement