package com.ssiltda.printerapp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Stream {

    public String readStream(InputStream in) {
        BufferedReader reader = null;
        String res = null;
        StringBuffer response = new StringBuffer();
        try {
            reader = new BufferedReader(new InputStreamReader(in));
            String line = "";
            while ((line = reader.readLine()) != null) {
                response.append(line);
                res = response.toString();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return res;
    }

    public String tamDato(String cadena, int tam, String DoI){
        String datoFormateado = "", espacios = "";
        if (cadena.length()>tam){
            datoFormateado = cadena.substring(0,tam);
        }else if(cadena.length()<tam){
            for (int i = 0; tam-cadena.length()>=i; i++){
                espacios += " ";
            }
            if (DoI.equals("D")){
                datoFormateado = cadena+espacios;
            }else if(DoI.equals("I")){
                datoFormateado = espacios+cadena;
            }else{
                datoFormateado = cadena;
            }
        }else{
            datoFormateado = cadena;
        }
        return datoFormateado;
    }

    public String agregueEspacios(String cadena, int tamF){
        int dif = tamF-cadena.length();
        String espacios = "";
        for (int i = 0; dif>=i; i++){
            espacios = espacios+" ";
        }
        return espacios+cadena;
    }

    public String imprimaNumero (String num, int tama){
        int dif = tama-num.length();
        String espacios = "";
        for (int i = 0; dif<=i; i++){
            espacios = espacios+" ";
        }
        return espacios+num;
    }

    public String ajusteTam(String dato, int tam){
        String valor = "";
        if (dato.length()>tam){
            valor = dato.substring(0,tam);
        }else if(dato.length()<tam){
            valor = agregueEspacios(dato,tam);
        }
        return valor;
    }
}
