StackOverflow

7 respostas
oitathi

Oi pessoal! Estou com o seguinte problema: eu tenho um arquivo que está em binários de 0 à 256. cada quebra de linha acontece no com o char 10, então eu formo uma linha a sempre q encontro o caracter 10. E quando eu encontro o caracter 26, eu sei q o cabeçalho do meu arquivo terminou pra começar uma imagem. Isso foi definido.
porém, quando eu chamo o função readCharLine(), entra numa recursão, como eu quero, mas o programa soh faz 16 vezes e não até o final do arquivo, pois dah StackOverflow…
Alguém pode me ajudar?

public class ReadingMethods {
    
    public void readHeaderLine (RandomAccessFile fis) throws IOException {
        int s=0;
        String thisString="";
        char thisChar = (char) fis.read();
        while ((thisChar != 13) && (thisChar != 0)) {
            if (thisChar!=10){
                thisString=thisString+thisChar;
            }
            thisChar = (char) fis.read();
        }
        s=(int)thisString.charAt(thisString.length()-1);
        System.out.println(thisString);
        readHeader(fis, s);
    }

   public void readHeader (RandomAccessFile fis, int s) throws IOException{
       if (s==26){
          readCharLine(fis);
       }
       else{
           readHeaderLine(fis);
      }
  }

   public void readCharLine(RandomAccessFile fis) throws IOException {
        char thisChar = (char) fis.read();
        String hex;
        int bin;
            hex = Integer.toHexString((int)thisChar);
            bin = (int)thisChar;
            System.out.print(hex+"    ");
            System.out.println(bin);
            readMap(fis, bin);
    }



   public void readMap (RandomAccessFile fis, int s) throws IOException{
        if (s==0){
          System.out.println();
          System.out.println();
          System.out.println();
          System.out.println();
          readCharLine(fis); // faz 16 vezes e depois dah StackOverflowError

       }
       
        else{
            readCharLine(fis);
        }
   }
          
}

7 Respostas

romarcio

Recursividade é meio perigosa por isso mesmo.

O que parece é que vc está chamando infinitamente o método readCharLine(), pq tanto no IF quanto no ELSE do método readMap(), vc faz essa chamada. Não vai parar nunca.

oitathi

sim, mas pra dar StackOverflow, não teria que ler tudo q contem no arquivo e depois dar o erro? o erro aparece bem no começo…

romarcio

Não necessariamente.
A recursividade cria uma pilha de chamadas, quanto mais aumentar essa pilha, mas próxima do StackOverFlow vc estará. Cada chamada a recursividade ela precisa guardar em memória tudo que foi feito antes dessa chamada, e assim por diante.

Ao invés de fazer recursividade, vc não poderia trocar por um while? Já que no método readMap(), vc chama o método readCharline() sendo o s==0 ou não.

oitathi

eu mudei o método, pois quado ele achar um binario com o valor igual a 132 é pra ele parar e não fazer mais nada… mas mesmo assim tah dando StackOverflow!!! Por favor me ajudem!!!

public void readMap (RandomAccessFile fis, int s) throws IOException{
     
        if (s==0){
          System.out.println();
          System.out.println();
          System.out.println();
          System.out.println();
          readCharLine(fis);

       }

        if(s==132){
            //do nothing
        }
       
        else{
            readCharLine(fis);
        }
   
   }
romarcio

Para parar faz assim, o comando return vai fazer parar.

if(s==132){  
            return; 
        }
oitathi

Eu usei o while como vc falou e solucionei…

public void readCharLine(RandomAccessFile fis) throws IOException {
        char thisChar = (char) fis.read();
        String hex="";
        int bin=0;
        while(!(hex.equals("ffff"))){
            hex = Integer.toHexString((int)thisChar);
            bin = (int)thisChar;
            if(bin==0){
              System.out.println();
              System.out.println();
              System.out.println();
              System.out.println();
           }
            System.out.print(hex+"    ");
            System.out.println(bin);
           thisChar = (char) fis.read();
        }
     }

Obrigada!!!

romarcio

Legal que funcionou.

Criado 7 de fevereiro de 2011
Ultima resposta 8 de fev. de 2011
Respostas 7
Participantes 2