UserApplication.java
Created with JBuilder
package gr.dim.NetworkProgramming_2;

import java.awt.image.BufferedImage;
import java.io.*;
import java.net.*;

import javax.imageio.ImageIO;
import javax.sound.sampled.*;
import javax.swing.*;

/**
 * <p>Title: Java socket programming</p>
 *
 * <p>Description: Ergasia sta diktia 2</p>
 *
 * <p>Copyright: Copyright (c) 2007</p>
 *
 * <p>Company: AUTH - Department of electric and computer engineering</p>
 *
 * @author Tzionas Dimitrios - 5168
 * @version 1.0
 */
public class UserApplication {
    int sinedria;

    ////////////////////////////////////////////////////////////////////////////
    String clientPortString;          // Client Listening Port
    String serverPortString;          // Server Listening Port
    String echo            ;          // Echo   Request   Code
    String image           ;          // Image  Result    Code
    String sound           ;          // Sound  Result    Code
    ////////////////////////////////////////////////////////////////////////////

    boolean showVideo;

    String soundWave;
    String sound_DPCM;
    String sound_AQ_DPCM;
    String song_DPCM;
    String song_AQ_DPCM;

    int delay               ;         // TimeOut
    int b                   ;

    String echo0 ;          // Echo   Request   Code

    int clientPort ;
    int serverPort ;
    byte[] hostIP     =  { (byte) 155, (byte) 207, (byte) 18, (byte) 208};

    int imagePacketLength;

    long echoDuration;
    long videoDuration;

    String imageBig  ;
    String imageSmall;

    FileOutputStream echoFile_EXXX;
    FileOutputStream echoFile_E000;
    FileOutputStream delayFile_EXXX;
    FileOutputStream delayFile_E000;
    FileOutputStream throughputFile_EXXX_8;
    FileOutputStream throughputFile_EXXX_16;
    FileOutputStream throughputFile_EXXX_32;
    FileOutputStream throughputFile_E000_8;
    FileOutputStream throughputFile_E000_16;
    FileOutputStream throughputFile_E000_32;
    FileOutputStream imageFile;
    FileOutputStream throughputVideoFile_8;
    FileOutputStream throughputVideoFile_16;
    FileOutputStream throughputVideoFile_32;

    FileOutputStream dpcmSongDiafFile;
    FileOutputStream dpcmSongDeigFile;
    FileOutputStream dpcmSoundDiafFile;
    FileOutputStream dpcmSoundDeigFile;

    FileOutputStream aqdpcmSongDiafFile;
    FileOutputStream aqdpcmSongDeigFile;
    FileOutputStream aqdpcmSoundDiafFile;
    FileOutputStream aqdpcmSoundDeigFile;

    FileOutputStream aqdpcmSongMeanFile;
    FileOutputStream aqdpcmSongStepFile;
    FileOutputStream aqdpcmSoundMeanFile;
    FileOutputStream aqdpcmSoundStepFile;

    FileOutputStream diafFile;
    FileOutputStream deigFile;
    FileOutputStream meanFile;
    FileOutputStream stepFile;
    FileOutputStream soundDiafFile;
    FileOutputStream soundDeigFile;



    FileOutputStream echoFile;
    FileOutputStream delayFile;
    FileOutputStream throughputFile_8;
    FileOutputStream throughputFile_16;
    FileOutputStream throughputFile_32;

    String         message;
    DatagramSocket sSocket;
    DatagramSocket rSocket;
    InetAddress    hostAddress;
    DatagramPacket sPacket;

    byte[][] myBuffer;



    public UserApplication() {

        //  Allakse afta pou einai mesa sto parakatw "koutaki"

        /*********************************************************************/
        /*********************************************************************/
        /*******/
        /*******/   sinedria = 444;
        /*******/
        /*******/   clientPortString = "38015";        // Client Listening Port
        /*******/   serverPortString = "48015";        // Server Listening Port
        /*******/   echo             = "E0733";        // Echo   Request   Code
        /*******/   image            = "M0587";        // Image  Result    Code
        /*******/   sound            = "V2394";        // Sound  Result    Code
        /*******/
        /*******/   showVideo = true;
        /*******/
        /*******/   soundWave = "999";   //   MUST be 3 digits
        /*******/
        /*******/   b         = 1;       // int b>1, antistrofo tou sintelesti kanonikopoiisis
        /*******/
        /*******/   echoDuration  = 12 * 60 * 1000;
        /*******/   videoDuration = 12 * 60 * 1000;
        /*******/
        /*******/   imagePacketLength = 128;  // epeidi allakse i ekfonisi
        /*******/
        /*******/
        /*********************************************************************/
        /*********************************************************************/

        delay               = 4 * 1000;         // TimeOut

        sound_DPCM    = sound        + "T" + soundWave;
        sound_AQ_DPCM = sound + "AQ" + "T" + soundWave;

        song_DPCM     = sound        + "F" + soundWave;
        song_AQ_DPCM  = sound + "AQ" + "F" + soundWave;

        imageBig   = image + "CAM=1";
        imageSmall = image + "CAM=2";

        echo0            = "E0000";          // Echo   Request   Code


        clientPort = Integer.parseInt(clientPortString);
        serverPort = Integer.parseInt(serverPortString);

    }



    public static void main(String[] args) throws SocketException {
        UserApplication app = new UserApplication();


        app.fileCreation();

        //app.video(app.imageBig);
        app.video(app.image);     //   imageSmall*/

        app.sound(app.song_AQ_DPCM);
        app.sound(app.song_DPCM);

        app.sound(app.sound_DPCM);
        app.sound(app.sound_AQ_DPCM);

        app.echo(app.echo);
        app.echo(app.echo0);


        System.out.print("\nTELOS");
        System.exit(-1);
    }


    /**
     * video
     *
     * @param imRequest String
     */
    private void video(String imRequest) {

        if ( (imRequest.length()==10))
            System.out.println("Video method started - " + imRequest.substring(5,10) + "\n\n");
        else
            System.out.println("Video method started - " + imRequest + "\n\n");

        long timeTotalStart, timeTotalEnd, timePacketStart, timePacketEnd=0;
        long timeStart_XXXsec=0 , timeEnd_XXXsec, dd=0;

        boolean isBig;

        double dtTotal=0;

        int secPacketCount = 0;
        int max            = 0;
        int min            = 10*1000;
        int dt             = 0;
        int success        = 0;
        int failure        = 0;
        int requestCounter = 0;
        int delayTotal     = 0;
        int xxx            = 1;

        boolean flagDelay;
        boolean flag_XXX_sec_passed;
        boolean flagPassed_8sec;
        boolean flagPassed_16sec;
        boolean flagStopChecking;
        boolean flagImageEnd;

        int[] mat8  = new int[8  / xxx];
        int[] mat16 = new int[16 / xxx];
        int[] mat32 = new int[32 / xxx];

        long[] time8  = new long[8  / xxx];
        long[] time16 = new long[16 / xxx];
        long[] time32 = new long[32 / xxx];


        String dtString = "";

        // GIA SEND
        try {
            sSocket = new DatagramSocket();
        } catch (SocketException ex4) {
        }
        byte[] txbuffer = imRequest.getBytes();
        //System.out.println(imRequest);

        try {
            hostAddress = InetAddress.getByAddress(hostIP);
        } catch (UnknownHostException ex) {
        }
        sPacket = new DatagramPacket(txbuffer, txbuffer.length, hostAddress, serverPort);

        // GIA RECEIVE
        try {
            rSocket = new DatagramSocket(clientPort);
        } catch (SocketException ex2) {
        }
        try {
            rSocket.setSoTimeout(delay);
        } catch (SocketException ex3) {
        }
        byte[] rxbuffer = new byte[imagePacketLength];///////////////////////////////////////////////////////////
        DatagramPacket rPacket = new DatagramPacket(rxbuffer, rxbuffer.length);

        ////////////////////////////////////////////////////
        ////////////////  Bromiki douleia  /////////////////
        ////////////////////////////////////////////////////

        timePacketStart = 0; // apla gia arxikopoiisi

        flagDelay            = false;
        flagPassed_8sec      = false;
        flagPassed_16sec     = false;
        flagStopChecking     = false;
        flag_XXX_sec_passed  = true;
        flagImageEnd         = false;

        int countFile = 0;

        int bytes = 0;

        ///////////////////////////////////////////////////////////////////////
        if ( (imRequest.length()==10) && (imRequest.substring(9,10).compareTo("1") == 0))
            isBig = true;
        else
            isBig = false;


        String path = "me";
        if ( isBig == true )
            path += "_big";
        path += ".jpg";
        File file = new File(path);
        BufferedImage image = null;
        try {
            image = ImageIO.read(file);
        } catch (IOException ex6) {
        }

        ImageIcon icon = new ImageIcon(image);
        JLabel label = new JLabel(icon);
        JFrame f = new JFrame();
        f.setTitle("Video Streaming !!!");
        //f.setUndecorated(true);
        //f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(label);
        f.pack();
        if ( isBig == true)
            f.setLocation(200, 150);//300, 250
        else
            f.setLocation(300, 250);//300, 250

        if (showVideo == true){
            f.setAlwaysOnTop(true);
            f.setVisible(true);

            long start = System.currentTimeMillis();

            boolean exitt = false;
            int meNum = 5;


            /**/while (System.currentTimeMillis() - start < 1 * 1000);

            start = System.currentTimeMillis();


            while (exitt == false) {

                path = "me_" + meNum ;
                if ( isBig == true)
                    path += "_big";
                path += ".jpg";

                File meee = new File(path);
                file = meee;
                image = null;
                try {
                    image = ImageIO.read(file);
                } catch (IOException ex6) {
                }

                icon.setImage(image);
                label = new JLabel(icon);
                f.getContentPane().add(label);
                f.pack();

                start = System.currentTimeMillis();
                /**/while (System.currentTimeMillis() - start < 1 * 1000);

                //System.out.println("bbb");
                if (meNum == 0)
                    exitt = true;
                meNum--;
            }
        }

        ///////////////////////////////////////////////////////////////////////

        timeTotalStart = timeTotalEnd = System.currentTimeMillis();

        while (timeTotalEnd - timeTotalStart < videoDuration) {


            countFile++;

            ///////////////////////////////////////////////////////////////////////////
            File throughputPath = new File(sinedria + "___image___" + countFile + ".jpg");
            System.out.println("\n" +      sinedria + "___image___" + countFile + ".jpg");

            try {
                imageFile = new FileOutputStream(throughputPath);
            } catch (FileNotFoundException ex666) {
                //System.out.println("File creation failed");
            }
            ///////////////////////////////////////////////////////////////////////////


            if (flagDelay == false)
                timePacketStart = System.currentTimeMillis();

            for (;;){
                try {
                    sSocket.send(sPacket);
                    System.out.println("\n\nEsteila gia image !!! _____ " + countFile);
                    requestCounter++;
                } catch (IOException ex1) {
                    System.out.println("\nDEN EFTASE TO REQUEST !!!!!");
                }
                break;
            }

            long a = System.currentTimeMillis();

            flagImageEnd = false;

            while (flagImageEnd == false){

                if (flag_XXX_sec_passed == true){
                    timeStart_XXXsec = System.currentTimeMillis();
                    flag_XXX_sec_passed = false;
                    bytes = 0;
                    secPacketCount = 0;
                }


                try {
                    rSocket.receive(rPacket);
                    if ( rPacket.getLength() < imagePacketLength )
                        flagImageEnd = true;
                    if (flagImageEnd == true)
                        System.out.println("flagImageEnd => " + flagImageEnd);


                    timePacketEnd = System.currentTimeMillis();

                    bytes+=rPacket.getLength();
                    //System.out.println(bytes);

                    message = new String(rxbuffer, 0, rPacket.getLength());
                    byte[] hilfe = new byte[rPacket.getLength()];
                    hilfe = rPacket.getData();
                    imageFile.write(hilfe);

                    success++;
                    secPacketCount++;

                    dt += timePacketEnd - timePacketStart;

                    delayTotal += dt;

                    flagDelay = false;

                } catch (Exception x) {
                    failure++;
                    flagDelay = true;
                    System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
                    System.out.println("%%%%%%%%%%%%%%%%   F A I L E D   !!!!!!!!!!!");
                    System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");

                    if (showVideo == true){
                        path = "failout2";
                        if (isBig == true)
                            path += "_big";
                        path += ".jpg";
                        File failoutPath = new File(path);

                        file = failoutPath;
                        image = null;
                        try {
                            image = ImageIO.read(file);
                        } catch (IOException ex35436) {
                        }
                        icon.setImage(image);
                        label = new JLabel(icon);
                        f.getContentPane().add(label);
                        f.pack();
                    }
                }
                System.out.println("xexexexexexexexe");

                timeTotalEnd = System.currentTimeMillis();


                //vStart - System.currentTimeMillis())/1000 );

                if ( (flagDelay == true) && (xxx == 1) ){
                    for (int i=1; i<=4; i++){                     ///////////////////////////
                        shiftMat(0, mat8);
                        shiftMat(0, mat16);
                        shiftMat(0, mat32);
                    }
                }

                if ((timeTotalEnd - timeTotalStart >= 32 * 1000)&&(flagStopChecking == false)) {
                    flagStopChecking = true;
                    System.out.println("\n###############################################################");
                    System.out.println("######################  32 PASSED !!!  ########################");
                    System.out.println("###############################################################\n");
                }
                else
                if ((timeTotalEnd - timeTotalStart >= 16 * 1000)&&(flagPassed_16sec==false)){
                    flagPassed_16sec = true;
                    System.out.println("\n###############################################################");
                    System.out.println("######################  16 PASSED !!!  ########################");
                    System.out.println("###############################################################\n");
                }
                else
                if ((timeTotalEnd - timeTotalStart >= 8 * 1000)&&(flagPassed_8sec==false)){
                    flagPassed_8sec = true;
                    System.out.println("\n###############################################################");
                    System.out.println("######################  8 PASSED !!!  #########################");
                    System.out.println("###############################################################\n");
                }


                timeEnd_XXXsec = System.currentTimeMillis();

                if (timeEnd_XXXsec - timeStart_XXXsec >= xxx * 1000){

                    dd= timeEnd_XXXsec - timeStart_XXXsec;

                    shiftMat(dd, time8);
                    shiftMat(dd, time16);
                    shiftMat(dd, time32);

                    shiftMat(bytes, mat8);
                    shiftMat(bytes, mat16);
                    shiftMat(bytes, mat32);

                    /*System.out.println("\n################### time 8 ##############");
                    for (int i=0; i<time8.length; i++)
                        System.out.print(time8[i] + "\t");*/

                    /*System.out.println("\n################### time 16 ##############");
                    for (int i=0; i<time16.length; i++)
                        System.out.print(time16[i] + "\t");*/
                    /*System.out.println("\n################### bytes 8 ##############");
                    for (int i=0; i<mat8.length; i++)
                        System.out.print(mat8[i] + "\t");*/
                    /*System.out.println("\n################### bytes 16 ##############");
                    for (int i=0; i<mat16.length; i++)
                        System.out.print(mat16[i] + "\t");*/


                    flag_XXX_sec_passed = true;
                    dtTotal = System.currentTimeMillis() - timeTotalStart;
                    //System.out.print("_______________________________________________________________");
                    //System.out.print("______________________secPacketCounter___");
                    //System.out.println(secPacketCount + "__________timePassed => " + (dtTotal/1000) + " sec");



                    if (flagStopChecking == true) {
                        System.out.println();
                        System.out.println("THROUPUT   8   16   32");
                        movingThroughPut(0, mat8 , time8 , throughputVideoFile_8);
                        movingThroughPut(0, mat16, time16, throughputVideoFile_16);
                        movingThroughPut(0, mat32, time32, throughputVideoFile_32);
                    }
                    else
                    if (flagPassed_16sec == true) {
                        System.out.println();
                        System.out.println("THROUPUT   8   16");
                        movingThroughPut(0, mat8 , time8 , throughputVideoFile_8);
                        movingThroughPut(0, mat16, time16, throughputVideoFile_16);
                    } else
                    if (flagPassed_8sec == true){
                        System.out.println();
                        System.out.println("THROUPUT   8");
                        movingThroughPut(0, mat8, time8, throughputVideoFile_8);
                    }
                    //System.out.println("BYTES  -  " + bytes);


                }   //   if (timeEnd_XXXsec - timeStart_XXXsec >= xxx * 1000){

            }   //   while (flagImageEnd == false){

            System.out.println("xouuuuuuuuuuu");

            long b = System.currentTimeMillis();
            //System.out.println("End of image " + countFile + "   ####   photo downloaded in  ->  " + (float)(b-a)/1000 + " sec");

            ////////////////////////////////////////////////  GIA REFRESH
            if (showVideo == true){
                file = throughputPath;
                image = null;
                try {
                    image = ImageIO.read(file);
                } catch (IOException ex6){
                    //System.out.println("EIKONA FATAL ERROR");
                }

                try {
                    icon.setImage(image);
                } catch (Exception e){
                    path = "failout";
                    if (isBig == true)
                        path += "_big";
                    path += ".jpg";
                    File failoutPath = new File(path);

                    file = failoutPath;
                    image = null;
                    try {
                        image = ImageIO.read(file);
                    } catch (IOException ex35436){
                    }
                    icon.setImage(image);
                    label = new JLabel(icon);
                    f.getContentPane().add(label);
                    f.pack();

                }

                label = new JLabel(icon);
                f.getContentPane().add(label);
                f.pack();

            }
            ////////////////////////////////////////////////////////////////////
            System.out.println("xiiiiiiiiiii");

        }   // while (timeTotalEnd - timeTotalStart < (60 *  /*10 * 60 * */ 1000))

        if (showVideo == true)
            f.setVisible(false);

        System.out.println("Video method ended\n\n");
    }




    /**
     * echo
     *
     * @param echo String
     */
    private void echo(String echo) {

        System.out.println("Echo method started - " + echo + "\n\n");

        long timeTotalStart, timeTotalEnd, timePacketStart, timePacketEnd=0;
        long timeStart_XXXsec=0 , timeEnd_XXXsec, dd=0;

        double dtTotal=0;

        int secPacketCount = 0;
        int xTetragwno     = 0;
        int max            = 0;
        int min            = 10*1000;
        int dt             = 0;
        int success        = 0;
        int failure        = 0;
        int requestCounter = 0;
        int delayTotal     = 0;
        int xxx;

        int bytes = 0;

        boolean flagDelay;
        boolean flag_XXX_sec_passed;
        boolean flagPassed_8sec;
        boolean flagPassed_16sec;
        boolean flagStopChecking;

        float diaspora = 0;

        String echo2;

        echo2 = echo.substring(1,5);
        xxx = Integer.parseInt(echo2);

        if (xxx == 0){
            xxx = 1;
            echoFile          = echoFile_E000;
            delayFile         = delayFile_E000;
            throughputFile_8  = throughputFile_E000_8;
            throughputFile_16 = throughputFile_E000_16;
            throughputFile_32 = throughputFile_E000_32;
        }
        else{
            xxx = 8;
            echoFile          = echoFile_EXXX;
            delayFile         = delayFile_EXXX;
            throughputFile_8  = throughputFile_EXXX_8;
            throughputFile_16 = throughputFile_EXXX_16;
            throughputFile_32 = throughputFile_EXXX_32;
        }

        int[] mat8  = new int[8  / xxx];
        int[] mat16 = new int[16 / xxx];
        int[] mat32 = new int[32 / xxx];

        long[] time8  = new long[8  / xxx];
        long[] time16 = new long[16 / xxx];
        long[] time32 = new long[32 / xxx];


        String dtString = "";

        // GIA SEND
        try {
            sSocket = new DatagramSocket();
        } catch (SocketException ex4) {
        }
        //System.out.println(echo);
        byte[] txbuffer = echo.getBytes();

        try {
            hostAddress = InetAddress.getByAddress(hostIP);
        } catch (UnknownHostException ex) {
        }
        sPacket = new DatagramPacket(txbuffer, txbuffer.length, hostAddress, serverPort);

        // GIA RECEIVE
        try {
            rSocket = new DatagramSocket(clientPort);
        } catch (SocketException ex2) {
        }
        try {
            rSocket.setSoTimeout(delay);
        } catch (SocketException ex3) {
        }
        byte[] rxbuffer = new byte[32];
        DatagramPacket rPacket = new DatagramPacket(rxbuffer, rxbuffer.length);

        ////////////////////////////////////////////////////
        ////////////////  Bromiki douleia  /////////////////
        ////////////////////////////////////////////////////

        try {
            echoFile.write("Echo Request Code        =>        ".getBytes());
            echoFile.write(String.valueOf(echo).getBytes());
            echoFile.write(13);
            echoFile.write(10);
            echoFile.write(13);
            echoFile.write(10);
            echoFile.write(13);
            echoFile.write(10);
        } catch (IOException ex8) {
        }


        timePacketStart = 0; // apla gia arxikopoiisi
        timeTotalStart = timeTotalEnd = System.currentTimeMillis();

        flagDelay            = false;
        flagPassed_8sec      = false;
        flagPassed_16sec     = false;
        flagStopChecking     = false;
        flag_XXX_sec_passed  = true;

        while (timeTotalEnd - timeTotalStart < echoDuration) {

            if (flagDelay == false)
                timePacketStart = System.currentTimeMillis();

            if (flag_XXX_sec_passed == true){
                timeStart_XXXsec = System.currentTimeMillis();
                flag_XXX_sec_passed = false;
            }
            try {
                //System.out.println("stelnw request");
                sSocket.send(sPacket);
                requestCounter++;
            } catch (IOException ex1) {}

            try {
                rSocket.receive(rPacket);
                timePacketEnd = System.currentTimeMillis();
                bytes = rPacket.getLength();
                //System.out.println("packet length - " + bytes);
                message = new String(rxbuffer, 0, rPacket.getLength());
                ///**/System.out.print(message/**/+"\n"/**/);
                echoFile.write(message.getBytes(), 7, 10);
                echoFile.write(9);
                echoFile.write(message.getBytes(), 18, 8);
                echoFile.write(9);
                success++;
                secPacketCount++;

                dt += timePacketEnd - timePacketStart;

                if (dt>max)
                    max=dt;
                if (dt<min)
                    min=dt;
                delayTotal+=dt;
                xTetragwno+=dt*dt;
                timeTotalEnd = System.currentTimeMillis();
                flagDelay = false;

            } catch (Exception x) {
                failure++;
                flagDelay = true;
                System.out.println("F A I L E D !!!");
            }


            timeEnd_XXXsec = System.currentTimeMillis();


            if ( (flagDelay == true) && (xxx == 1) ){
                for (int i=1; i<=4; i++){                     ///////////////////////////
                    shiftMat(0, mat8);
                    shiftMat(0, mat16);
                    shiftMat(0, mat32);
                }
            }

            if ((timeTotalEnd - timeTotalStart >= 32 * 1000)&&(flagStopChecking == false)) {
                flagStopChecking = true;
                System.out.println("\n###############################################################");
                System.out.println("######################  32 PASSED !!!  ########################");
                System.out.println("###############################################################\n");
            }
            else
            if ((timeTotalEnd - timeTotalStart >= 16 * 1000)&&(flagPassed_16sec==false)){
                flagPassed_16sec = true;
                System.out.println("\n###############################################################");
                System.out.println("######################  16 PASSED !!!  ########################");
                System.out.println("###############################################################\n");
            }
            else
            if ((timeTotalEnd - timeTotalStart >= 8 * 1000)&&(flagPassed_8sec==false)){
                flagPassed_8sec = true;
                System.out.println("\n###############################################################");
                System.out.println("######################  8 PASSED !!!  #########################");
                System.out.println("###############################################################\n");
            }


            if (timeEnd_XXXsec - timeStart_XXXsec >= xxx * 1000){

                dd= timeEnd_XXXsec - timeStart_XXXsec;

                shiftMat(dd, time8);
                shiftMat(dd, time16);
                shiftMat(dd, time32);

                shiftMat(secPacketCount, mat8);
                shiftMat(secPacketCount, mat16);
                shiftMat(secPacketCount, mat32);



                //System.out.println("################### time 8 ##############");
                //for (int i=0; i<time8.length; i++)
                //    System.out.print(time8[i] + "\t");
                //
                //System.out.println("################### time 16 ##############");
                //for (int i=0; i<time16.length; i++)
                //    System.out.print(time16[i] + "\t");
                //System.out.println("\n################### paketa 8 ##############");
                //for (int i=0; i<mat8.length; i++)
                //    System.out.print(mat8[i] + "\t");
                //System.out.println("\n################### paketa 16 ##############");
                //for (int i=0; i<mat16.length; i++)
                //    System.out.print(mat16[i] + "\t");




                flag_XXX_sec_passed = true;
                dtTotal = System.currentTimeMillis() - timeTotalStart;
                System.out.print("_______________________________________________________________");
                System.out.print("______________________secPacketCounter___");
                System.out.println(secPacketCount + "__________timePassed => " + (dtTotal/1000) + " sec");



                if (flagStopChecking == true) {
                    movingThroughPut(bytes, mat8 , time8 , throughputFile_8);
                    movingThroughPut(bytes, mat16, time16, throughputFile_16);
                    movingThroughPut(bytes, mat32, time32, throughputFile_32);
                    //System.out.println("ThroughPut 8 16 32");
                }
                else
                if (flagPassed_16sec == true) {
                    movingThroughPut(bytes, mat8 , time8 , throughputFile_8);
                    movingThroughPut(bytes, mat16, time16, throughputFile_16);
                    //System.out.println("ThroughPut 8 16");
                } else
                if (flagPassed_8sec == true){
                    movingThroughPut(bytes, mat8, time8, throughputFile_8);
                    //System.out.println("ThroughPut 8");

                }
                secPacketCount = 0;
            }

            if (flagDelay == false){
                //System.out.println("\t\t\t\t" + dt);
                dtString+= String.valueOf(dt)+",";
                try {
                    echoFile.write(9);
                    echoFile.write(String.valueOf(dt).getBytes());
                    echoFile.write(13);
                    echoFile.write(10);
                    delayFile.write(String.valueOf(dt).getBytes());
                    delayFile.write(13);
                    delayFile.write(10);

                } catch (IOException ex5) {
                }
                dt=0;
            }
        }

        System.out.println("\n\nSuccess             => " + success);
        System.out.println("Failure             => "     + failure);
        System.out.println("Total Requests      => " + requestCounter);
        System.out.println("Failure probability => " + failure/(float)(success+failure)*100 + " %" );

        System.out.println("\nMax delay time      => " + max + " ms" );
        System.out.println("Min delay time      => " + min + " ms" );

        System.out.println("Total delay time    => " + delayTotal + " ms" );

        float mean = (float)delayTotal/(success);
        System.out.println("\nMean delay time     => " + mean + " ms" );

        diaspora = ( (float)xTetragwno/success ) - mean*mean;
        System.out.println("Diaspora            => " + diaspora + " ms" );
        System.out.println("Tipiki apoklisi     => " + Math.sqrt(diaspora) + " ms" );
        System.out.println("\n\n");

        try {
            echoFile.write(13);
            echoFile.write(10);
            echoFile.write(13);
            echoFile.write(10);
            echoFile.write("Success                      => ".getBytes());
            echoFile.write(String.valueOf(success).getBytes());
            echoFile.write(13);
            echoFile.write(10);
            echoFile.write("Failure                        => ".getBytes());
            echoFile.write(String.valueOf(failure).getBytes());
            echoFile.write(13);
            echoFile.write(10);
            echoFile.write("Total Requests          => ".getBytes());
            echoFile.write(String.valueOf((success + failure)).getBytes());
            echoFile.write(13);
            echoFile.write(10);
            echoFile.write(13);
            echoFile.write(10);
            echoFile.write("Failure probability    => ".getBytes());
            echoFile.write(String.valueOf(failure/(float)(success+failure)*100).getBytes());
            echoFile.write(" %".getBytes());
            echoFile.write(13);
            echoFile.write(10);

            echoFile.write(13);
            echoFile.write(10);
            echoFile.write(13);
            echoFile.write(10);
            echoFile.write("Max delay time          => ".getBytes());
            echoFile.write(String.valueOf(max).getBytes());
            echoFile.write(" ms".getBytes());
            echoFile.write(13);
            echoFile.write(10);
            echoFile.write("Min delay time          => ".getBytes());
            echoFile.write(String.valueOf(min).getBytes());
            echoFile.write(" ms".getBytes());
            echoFile.write(13);
            echoFile.write(10);
            echoFile.write("Total delay time        => ".getBytes());
            echoFile.write(String.valueOf(delayTotal).getBytes());
            echoFile.write(" ms".getBytes());
            echoFile.write(13);
            echoFile.write(10);
            echoFile.write(13);
            echoFile.write(10);

            echoFile.write(13);
            echoFile.write(10);
            echoFile.write("Mean delay time        => ".getBytes());
            echoFile.write(String.valueOf(mean).getBytes());
            echoFile.write(" ms".getBytes());
            echoFile.write(13);
            echoFile.write(10);
            echoFile.write("Diaspora                     => ".getBytes());
            echoFile.write(String.valueOf(diaspora).getBytes());
            echoFile.write(" ms".getBytes());
            echoFile.write(13);
            echoFile.write(10);
            echoFile.write("Tipiki apoklisi           => ".getBytes());
            echoFile.write(String.valueOf(Math.sqrt(diaspora)).getBytes());
            echoFile.write(" ms".getBytes());
            echoFile.write(13);
            echoFile.write(10);

        } catch (IOException ex6) {
        }




        try {
            echoFile.write(13);
            echoFile.write(10);
            echoFile.write(13);
            echoFile.write(10);
            echoFile.write(13);
            echoFile.write(10);
        } catch (IOException ex7) {
        }

        System.out.println("Echo method ended\n\n");

    }


    /**
     * movingThroughPut
     *
     * @param packetLength int
     * @param mat int[]
     * @param time long
     * @param throughputFile22 FileOutputStream
     */
    private void movingThroughPut(int packetLength, int[] mat, long[] time, FileOutputStream throughputFile22) {
        long count = 0;
        long totalTime =0;
        int  kind;
        String help;

        kind = mat.length;
        if (kind<8)
            kind*=8;

        for (int i=0; i<mat.length; i++)
            count+=mat[i];

        if (packetLength != 0)
            count = count * packetLength;

        for (int i=0; i<time.length; i++)
            totalTime+=time[i];

        count = (count * 8) * 1000 / totalTime;

        System.out.print("Moving Throughput ___ " + kind + " ");
        if (kind == 8)
            System.out.print(" ");

        System.out.println("__________ " + count + " bps                realTime   "+totalTime);

    help = String.valueOf(count);
        try {
            throughputFile22.write(help.getBytes());
            throughputFile22.write(13);
            throughputFile22.write(10);
        } catch (IOException ex) {
            System.out.println("en mpow throughput");
        }

    }

    /**
     * shiftMat
     *
     * @param secPacketCount int
     * @param mat int[]
     */
    private void shiftMat(int secPacketCount, int[] mat) {
        for (int i=mat.length-1;  i>0; i--)
            mat[i] = mat [i-1];
        mat[0] = secPacketCount;

    }


    /**
     * shiftMat
     *
     * @param num long
     * @param mat long[]
     */
    private void shiftMat(long num, long[] mat) {
        for (int i=mat.length-1;  i>0; i--)
            mat[i] = mat [i-1];
        mat[0] = num;

    }


    /**
     * Creates all the necessary files
     */
    private void fileCreation() {

        System.out.println("\nFile creation started");

        File echoPath__EXXX  = new File(sinedria + "___echo__EXXX.txt");
        File echoPath__E000  = new File(sinedria + "___echo__E000.txt");

        try {
            echoFile_EXXX = new FileOutputStream(echoPath__EXXX);
        } catch (FileNotFoundException ex) {
            System.out.println("File creation failed");
        }

        try {
            echoFile_E000 = new FileOutputStream(echoPath__E000);
        } catch (FileNotFoundException ex2) {
            System.out.println("File creation failed");
        }

        ///////////////////////////////////////////////////////////////////////

        File delayPath_EXXX = new File(sinedria + "___delay__EXXX.txt");
        File delayPath_E000 = new File(sinedria + "___delay__E000.txt");

        try {
                    delayFile_EXXX = new FileOutputStream(delayPath_EXXX);
                } catch (FileNotFoundException ex3) {
                    System.out.println("File creation failed");
                }

                try {
                    delayFile_E000 = new FileOutputStream(delayPath_E000);
                } catch (FileNotFoundException ex4) {
                    System.out.println("File creation failed");
        }

        ///////////////////////////////////////////////////////////////////////


        File throughputPath_E000_8  = new File(sinedria + "___throughput_E000_8.txt");
        File throughputPath_E000_16 = new File(sinedria + "___throughput_E000_16.txt");
        File throughputPath_E000_32 = new File(sinedria + "___throughput_E000_32.txt");

        try {
            throughputFile_E000_8 = new FileOutputStream(throughputPath_E000_8);
        } catch (FileNotFoundException ex5) {
            System.out.println("File creation failed");
        }

        try {
            throughputFile_E000_16 = new FileOutputStream(throughputPath_E000_16);
        } catch (FileNotFoundException ex6) {
            System.out.println("File creation failed");
        }

        try {
            throughputFile_E000_32 = new FileOutputStream(throughputPath_E000_32);
        } catch (FileNotFoundException ex7) {
            System.out.println("File creation failed");
        }

        /////////////////////////////////////////////////////////////////////////

        File throughputPath_EXXX_8  = new File(sinedria + "___throughput_EXXX_8.txt");
        File throughputPath_EXXX_16 = new File(sinedria + "___throughput_EXXX_16.txt");
        File throughputPath_EXXX_32 = new File(sinedria + "___throughput_EXXX_32.txt");


        try {
            throughputFile_EXXX_8  = new FileOutputStream(throughputPath_EXXX_8);
        } catch (FileNotFoundException ex8) {
            System.out.println("File creation failed");
        }

        try {
            throughputFile_EXXX_16 = new FileOutputStream(throughputPath_EXXX_16);
        } catch (FileNotFoundException ex9) {
            System.out.println("File creation failed");
        }

        try {
            throughputFile_EXXX_32 = new FileOutputStream(throughputPath_EXXX_32);
        } catch (FileNotFoundException ex10) {
            System.out.println("File creation failed");
        }

        ////////////////////////////////////////////////////////////////////////


        File throughputImagePath_8  = new File(sinedria + "___throughput_Image_8.txt");
        File throughputImagePath_16 = new File(sinedria + "___throughput_Image_16.txt");
        File throughputImagePath_32 = new File(sinedria + "___throughput_Image_32.txt");


        try {
            throughputVideoFile_8 = new FileOutputStream(throughputImagePath_8);
        } catch (FileNotFoundException ex10) {
            System.out.println("File creation failed");
        }
        try {
            throughputVideoFile_16 = new FileOutputStream(throughputImagePath_16);
        } catch (FileNotFoundException ex10) {
            System.out.println("File creation failed");
        }
        try {
            throughputVideoFile_32 = new FileOutputStream(throughputImagePath_32);
        } catch (FileNotFoundException ex10) {
            System.out.println("File creation failed");
        }

        ////////////////////////////////////////////////////////////////////////////

        File dpcmSongDiafPath  = new File(sinedria + "___dpcm_song__diafores.txt");
        File dpcmSongDeigPath  = new File(sinedria + "___dpcm_song__deigmata.txt");

        File dpcmSoundDiafPath = new File(sinedria + "___dpcm_sound__diafores.txt");
        File dpcmSoundDeigPath = new File(sinedria + "___dpcm_sound__deigmata.txt");

        try {
            dpcmSongDiafFile = new FileOutputStream(dpcmSongDiafPath);
        } catch (FileNotFoundException ex10) {
            System.out.println("File creation failed");
        }
        try {
            dpcmSongDeigFile = new FileOutputStream(dpcmSongDeigPath);
        } catch (FileNotFoundException ex10) {
            System.out.println("File creation failed");
        }
        try {
            dpcmSoundDiafFile = new FileOutputStream(dpcmSoundDiafPath);
        } catch (FileNotFoundException ex10) {
            System.out.println("File creation failed");
        }
        try {
            dpcmSoundDeigFile = new FileOutputStream(dpcmSoundDeigPath);
        } catch (FileNotFoundException ex10) {
            System.out.println("File creation failed");
        }

        ////////////////////////////////////////////////////////////////////////////

        File aqdpcmSongDiafPath  = new File(sinedria + "___aq_dpcm_song__diafores.txt");
        File aqdpcmSongDeigPath  = new File(sinedria + "___aq_dpcm_song__deigmata.txt");

        File aqdpcmSoundDiafPath = new File(sinedria + "___aq_dpcm_sound__diafores.txt");
        File aqdpcmSoundDeigPath = new File(sinedria + "___aq_dpcm_sound__deigmata.txt");

        try {
            aqdpcmSongDiafFile = new FileOutputStream(aqdpcmSongDiafPath);
        } catch (FileNotFoundException ex10) {
            System.out.println("File creation failed");
        }
        try {
            aqdpcmSongDeigFile = new FileOutputStream(aqdpcmSongDeigPath);
        } catch (FileNotFoundException ex10) {
            System.out.println("File creation failed");
        }
        try {
            aqdpcmSoundDiafFile = new FileOutputStream(aqdpcmSoundDiafPath);
        } catch (FileNotFoundException ex10) {
            System.out.println("File creation failed");
        }
        try {
            aqdpcmSoundDeigFile = new FileOutputStream(aqdpcmSoundDeigPath);
        } catch (FileNotFoundException ex10) {
            System.out.println("File creation failed");
        }

        ////////////////////////////////////////////////////////////////////////////

        File aqdpcmSongMeanPath  = new File(sinedria + "___aq_dpcm_song__mean.txt");
        File aqdpcmSongStepPath  = new File(sinedria + "___aq_dpcm_song__step.txt");

        File aqdpcmSoundMeanPath = new File(sinedria + "___aq_dpcm_sound__mean.txt");
        File aqdpcmSoundStepPath = new File(sinedria + "___aq_dpcm_sound__step.txt");

        try {
            aqdpcmSongMeanFile = new FileOutputStream(aqdpcmSongMeanPath);
        } catch (FileNotFoundException ex10) {
            System.out.println("File creation failed");
        }
        try {
            aqdpcmSongStepFile = new FileOutputStream(aqdpcmSongStepPath);
        } catch (FileNotFoundException ex10) {
            System.out.println("File creation failed");
        }
        try {
            aqdpcmSoundMeanFile = new FileOutputStream(aqdpcmSoundMeanPath);
        } catch (FileNotFoundException ex10) {
            System.out.println("File creation failed");
        }
        try {
            aqdpcmSoundStepFile = new FileOutputStream(aqdpcmSoundStepPath);
        } catch (FileNotFoundException ex10) {
            System.out.println("File creation failed");
        }



        System.out.println("File creation completed\n\n");

    }





    /**
     * sound
     *
     * @param sound String
     */
    private void sound(String sound) {

        String path = "me_dj_buffer.JPG";
        File file = new File(path);
        BufferedImage image = null;
        try {
            image = ImageIO.read(file);
        } catch (IOException ex6) {
        }

        ImageIcon icon = new ImageIcon(image);
        JLabel label = new JLabel(icon);
        JFrame f = new JFrame();
        f.setTitle("Audio Streaming !!!");
        //f.setUndecorated(true);
        //f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(label);
        f.pack();
        f.setLocation(300, 250); //300, 250
        if (showVideo == true) {
            f.setAlwaysOnTop(true);
            f.setVisible(true);
        }

        int packetNo = Integer.parseInt(soundWave);
        int size1 = packetNo;
        int size2 = 0;

        AudioFormat linearPCM;

        if (sound.substring(5, 7).compareTo("AQ") == 0) {
            //System.out.println("\n" + sound);
            System.out.println("AQ-DPCM");
            size2 = 132;
            linearPCM = new AudioFormat(8000, 16, 1, true, false);
            if ( sound.substring(7,8).compareTo("T") == 0){
                System.out.println("Sound");
                diafFile = aqdpcmSoundDiafFile;
                deigFile = aqdpcmSoundDeigFile;
                meanFile = aqdpcmSoundMeanFile;
                stepFile = aqdpcmSoundStepFile;

            }
            else
            if ( sound.substring(7,8).compareTo("F") == 0){
                System.out.println("Song");
                diafFile = aqdpcmSongDiafFile;
                deigFile = aqdpcmSongDeigFile;
                meanFile = aqdpcmSongMeanFile;
                stepFile = aqdpcmSongStepFile;
            }
        } else {
            //System.out.println(sound);
            System.out.println("DPCM");
            size2 = 128;
            linearPCM = new AudioFormat(8000, 8, 1, true, false);
            if ( sound.substring(5,6).compareTo("T") == 0){
                System.out.println("Sound");
                diafFile = dpcmSoundDiafFile;
                deigFile = dpcmSoundDeigFile;
            }
            else
            if ( sound.substring(5,6).compareTo("F") == 0){
                System.out.println("Song");
                diafFile = dpcmSongDiafFile;
                deigFile = dpcmSongDeigFile;
            }
        }

        SourceDataLine lineOut = null;
        try {
            lineOut = AudioSystem.getSourceDataLine(linearPCM);
        } catch (LineUnavailableException ex) {
        }
        try {
            lineOut.open(linearPCM, 32000);   //   32000 audio buffer
        } catch (LineUnavailableException ex1) {
        }
        lineOut.start();

        myBuffer = new byte[size1][size2];

        ////////////////////////////////////////////////////////////////////////
        // GIA SEND
        try {
            sSocket = new DatagramSocket();
        } catch (SocketException ex4) {
        }

        byte[] txbuffer = sound.getBytes();

        try {
            hostAddress = InetAddress.getByAddress(hostIP);
        } catch (UnknownHostException ex) {
        }

        sPacket = new DatagramPacket(txbuffer, txbuffer.length, hostAddress,
                                     serverPort);

        // GIA RECEIVE
        try {
            rSocket = new DatagramSocket(clientPort);
        } catch (SocketException ex2) {
        }

        try {
            rSocket.setSoTimeout(800);
        } catch (SocketException ex3) {
        }

        byte[] rxbuffer = new byte[size2];
        DatagramPacket rPacket = new DatagramPacket(rxbuffer, rxbuffer.length);

        ///////////////////////////////////////////////////////////////////////



        ////////////////////////////////////////////////////
        ////////////////  Bromiki douleia  /////////////////
        ////////////////////////////////////////////////////

        int packetCounter  = 0;
        int receiveCounter = 0;

        while(true){
            try {
                sSocket.send(sPacket);
                packetCounter++;
                //System.out.println("\n\nEsteila gia sound !!! _____ " + packetCounter);
            } catch (IOException ex1) {
                /*System.out.println(
                        "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$%  Failed to send request");*/
                continue;
            }
            break;
        }

        //System.out.println("buffering...");
        for (int i = 0; i < size1; i++) {

            try {
                rSocket.receive(rPacket);
                receiveCounter++;
                System.out.println("OP paketaki sound !!! _____ " + receiveCounter);
                for (int j = 0; j < size2; j++) {
                    myBuffer[i][j] = rxbuffer[j];
                }

            } catch (IOException ex5) {
                /*System.out.println(
            "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  Failed to receive packet");*/
                //break;
            }

        }
        System.out.println("OP polla paketakia sound !!! _____ " + receiveCounter);

        System.out.println("size1          => " + size1);
        System.out.println("receiveCounter => " + receiveCounter);



        byte[] bufferOut;

        if (sound.substring(5, 7).compareTo("AQ") == 0) {
            //System.out.println("AQ-DPCM");
            bufferOut = aq_dpcm(receiveCounter);
        }
        else {
            //System.out.println("DPCM");
            bufferOut  = dpcm(receiveCounter, b);
        }






        ////////////////////////////////////////////////  GIA REFRESH
        if (showVideo == true) {
            path = "me_dj_kick.JPG";
            File file2 = new File(path);
            image = null;
            try {
                image = ImageIO.read(file2);
            } catch (IOException ex6) {
            }

            try {
                icon.setImage(image);
            } catch (Exception e) {
            }

            label = new JLabel(icon);
            f.getContentPane().add(label);
            f.pack();

        }
        ////////////////////////////////////////////////////////////////////

        lineOut.write(bufferOut , 0, bufferOut.length);

        lineOut.stop();
        lineOut.close();

        if (showVideo == true) {
            f.setVisible(false);
        }


        System.out.println("End of sound method\n\n");

    }


    /**
     * dpcm
     *
     * @param receiveCounter int
     * @param bb int
     * @return byte[]
     */
    private byte[] dpcm(int receiveCounter, int bb) {

        byte[] bufferOut = new byte[128 * 2 * receiveCounter];
        //System.out.println("bufferOut => " + bufferOut.length);

        int help;
        int nibbleLS;
        int nibbleMS;

        String string;

        int size2 = myBuffer[0].length;
        //System.out.println("size2 => " + size2);

        int index = -1;

        for (int i = 0; i < receiveCounter; i++) {
            for (int j = 0; j < size2; j++) {

                help   = (int) myBuffer[i][j] ;   // bima 1 - ksexwrizw nibbles
                nibbleLS = (help      ) & 15;     //    15 -> 0000 1111
                nibbleMS = (help >>> 4) & 15;     //    15 -> 0000 1111

                index++;
                help = (nibbleMS - 8) * b;  //  OK  // bima 2 - afairw 8 // bima 3 - poll/zw me b
                try {
                    string = Integer.toString(help);
                    diafFile.write(string.getBytes());
                    diafFile.write(13);
                    diafFile.write(10);
                } catch (IOException ex) {
                }

                if (j > 0)
                    help = help + bufferOut[index - 1];  // bima 4 - anadromiki prosthesi
                try {
                    string = Integer.toString(help);
                    deigFile.write(string.getBytes());
                    deigFile.write(13);
                    deigFile.write(10);
                } catch (IOException ex1) {
                }
                bufferOut[index] = (byte) help;

                index++;
                help = (nibbleLS - 8) * bb;
                try {
                    string = Integer.toString(help);
                    diafFile.write(string.getBytes());
                    diafFile.write(13);
                    diafFile.write(10);
                } catch (IOException ex2) {
                }
                help = help + bufferOut[index - 1];
                try {
                    string = Integer.toString(help);
                    deigFile.write(string.getBytes());
                    deigFile.write(13);
                    deigFile.write(10);
                    bufferOut[index] = (byte) help;
                } catch (IOException ex3) {
                }

            }
        }

        int positive=0;
        int negative=0;
        int zero=0;

        for (int i=0; i<bufferOut.length; i++){
            if (bufferOut[i]>0)
                positive++;
            else
                if (bufferOut[i]<0)
                    negative++;
                else
                    zero++;
        }
        //System.out.println("positive => " + positive);
        //System.out.println("negative => " + negative);
        //System.out.println("zero     => " + zero);

        return bufferOut;
    }


    /**
     * aq_dpcm
     *
     * @param receiveCounter int
     * @return byte[]
     */
    private byte[] aq_dpcm(int receiveCounter) {

        byte[] bufferOut = new byte[2* (128 * 2) * receiveCounter];
        //System.out.println("bufferOut => " + bufferOut.length);

        int[][] samples = new int[receiveCounter][128 * 2];
        //System.out.println("samples => " + samples.length);

        int[] mean = new int[receiveCounter];
        int[] step = new int[receiveCounter];

        int help;
        int nibbleLS;
        int nibbleMS;

        String string;

        //System.out.println("size2 => " + myBuffer[0].length);   //   ola OK ws edw

        ////////////////////////////////////////////////////////////////////////

        int index  = -1;
        int bb;

        for (int i = 0; i < receiveCounter; i++) {
                                                       // bima 1 - briskw mean/step
            mean[i] = 256 * myBuffer[i][1] + myBuffer[i][0];   //   256*msb+lsb
            step[i] = 256 * myBuffer[i][3] + myBuffer[i][2];   //   256*msb+lsb;

            try {
                string = Integer.toString(mean[i]);
                meanFile.write(string.getBytes());
                meanFile.write(13);
                meanFile.write(10);
            } catch (IOException ex) {
            }

            try {
                string = Integer.toString(step[i]);
                stepFile.write(string.getBytes());
                stepFile.write(13);
                stepFile.write(10);
            } catch (IOException ex1) {
            }


            index = -1;

            for (int j = 4; j < myBuffer[0].length; j++) {
                bb = step[i];

                help     = (int) myBuffer[i][j];       // bima 2 - xwrizw se nibbles // bima 3 afairw to 8
                nibbleLS = ( (  help        & 15 )-8 )*bb;  //  bima 4 - poll/zw me b=step
                nibbleMS = ( (( help >>> 4) & 15 )-8 )*bb;

                samples[i][++index] = nibbleMS;
                samples[i][++index] = nibbleLS;
            }
        } // for (int i = 0; i < receiveCounter; i++)

        for (int m=0; m<samples.length; m++){
            for (int n = 0; n < samples[0].length; n++) {
                try {
                    string = Integer.toString(samples[m][n]);
                    diafFile.write(string.getBytes());
                    diafFile.write(13);
                    diafFile.write(10);
                } catch (IOException ex) {
                }

                if (n>0)
                    samples[m][n] += samples[m][n - 1];


            }
        }
        /////////////////////
        index  = -1;
        int max = - 999999999;
        int min =   999999999;

        for (int m=0; m<samples.length; m++)
            for (int n = 0; n < samples[0].length; n++) {
                samples[m][n] += mean[m];
                try {
                    string = Integer.toString(samples[m][n]);
                    deigFile.write(string.getBytes());
                    deigFile.write(13);
                    deigFile.write(10);
                } catch (IOException ex) {
                }

                bufferOut[++index] = (byte)  samples[m][n];
                //System.out.println("samples => " + samples[m][n]);
                //System.out.println("buffLS  => " + bufferOut[index]);
                bufferOut[++index] = (byte) (samples[m][n] >>> 8);
                //System.out.println("buffMS  => " + bufferOut[index]);
                if (samples[m][n]>max)
                    max = samples[m][n];
                else
                if (samples[m][n]<min)
                    min = samples[m][n];
            }
        //System.out.println("max => " + max);
        //System.out.println("min => " + min);
        //System.out.println("aq-dpcm");


        int positive=0;
        int negative=0;
        int zero=0;
        int help34;

        for (int i=0; i<bufferOut.length; i=i+2){
            help34 = 256*bufferOut[i]+bufferOut[i+1];
            if (help34>0)
                positive++;
            else
            if (help34<0)
                negative++;
            else
                zero++;
        }
        //System.out.println("positive => " + positive);
        //System.out.println("negative => " + negative);
        //System.out.println("zero     => " + zero);

        return bufferOut;
    }


}


UserApplication.java
Created with JBuilder