title: Turn Chunk of Files into Pages in Java
body: <p>I have a code where i read a large file in chunks and transform it into JSON to send it to a Spring MVC controller, my method looks something like this, i pass where i want to start in the offset and the chunkSize in the noBytes, the result is passed to be transformed into JSON and  sent to my controller</p>
  
  <pre><code>  public String readByteBlock(File file, int offset, int noBytes) throws IOException {
      InputStream in = new FileInputStream(file);
      byte[] result = new byte[noBytes];
      in.skip(offset);
      in.read(result, 0, noBytes);
      return FileSaver.byteArrayToString(result, noBytes);
  }
  </code></pre>
  
  <p>I am having problem to make the next and previous pagination work in my controller</p>
  
  <pre><code>@RequestMapping("/page/{id}")
  @ResponseBody   
      File file = new File("C:/teste.txt");
      FileSaver fs = new FileSaver();
      int byteblock = 100;        
      int offset = 0;
      if(id.equals("next")){      
          offset = (int) req.getSession().getAttribute("prevEndPos"); 
          String s = fs.readByteBlock(file, offset, byteblock);       
          req.getSession().setAttribute("prevStartPos", offset);     
          req.getSession().setAttribute("prevEndPos", (offset + byteblock) );       
          return s;
      }
      if(id.equals("prev")){
          offset = (int) req.getSession().getAttribute("prevStartPos") - byteblock ; 
          if(offset &lt; 0 ){
              String s = fs.readByteBlock(file, 0, byteblock);
                return s;
          }
          String s = fs.readByteBlock(file, offset, byteblock);         
          req.getSession().setAttribute("prevStartPos", (offset - byteblock) );      
          return s;
      }
      String s = fs.readByteBlock(file, 0, byteblock);
      req.getSession().setAttribute("prevStartPos", offset);
      req.getSession().setAttribute("prevEndPos", (offset + byteblock));
      return s;
  </code></pre>
  
  <p>So each chunk of the file will be put into an html table in my application,i  saved the current block startByte and endByte into my session but when the user press the previous button on my page how do i know where the previous block started and ended? since the session will hold the current page and not the previous one</p>