title: Read PDF File in Java - No External Libraries
body: <p>I've been writing a simple Java server over the last few weeks.</p>
  
  <p>First I wanted to display the the filesystem based on where you started the server.  For example, if you started the server in the <code>src</code> directory, opened up a browser, and went to localhost:5555 you would see the files and directories contained in <code>src</code>.  Each would be linked.  And I got that working fine.</p>
  
  <p>If you click a directory, it shows you its contents (just like I mentioned).  If you click a file, it reads the file and displays that file in plain text.  If you click an image, it serves that image.  This all happens in the browser and you can use the back button to return to the directory listings or file you were previously viewing.  This also works fine and uses no external libraries.</p>
  
  <p>This is the code I'm using to read a text file (using a reader):</p>
  
  <pre><code>private String readFile() {
      BufferedReader reader;
      String response = "";
      try {
          FileReader fileReader = new FileReader(requestedFile);
          reader = new BufferedReader(fileReader);
          String line;
          while ((line = reader.readLine()) != null) {
              response += line + "\n";
          }
          reader.close();
      } catch (FileNotFoundException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      }
      return response;
  }
  </code></pre>
  
  <p>This is the code I'm using to serve images (an input stream instead of a reader):</p>
  
  <pre><code>public byte[] getByteArray() throws IOException {
      byte[] byteArray = new byte[(int) requestedFile.length()];
      InputStream inputStream;
      String fileName = String.valueOf(requestedFile);
      inputStream = new BufferedInputStream(new FileInputStream(fileName));
      int bytesRead = 0;
      while (bytesRead &lt; byteArray.length) {
          int bytesRemaining = byteArray.length - bytesRead;
          int read = inputStream.read(byteArray, bytesRead, bytesRemaining);
          if (read &gt; 0) {
              bytesRead += read;
          }
      }
      inputStream.close();
      FilterOutputStream binaryOutputStream = new FilterOutputStream(outputStream);
      byte [] binaryHeaders = headers.getBytes();
      byte [] fullBinaryResponse = new byte[binaryHeaders.length + byteArray.length];
      System.arraycopy(binaryHeaders, 0, fullBinaryResponse, 0, binaryHeaders.length);
      System.arraycopy(byteArray, 0, fullBinaryResponse, binaryHeaders.length, byteArray.length);
      try {
          binaryOutputStream.write(fullBinaryResponse);
          binaryOutputStream.flush();
      } catch (IOException e) {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>What I'm attempting now is to serve PDFs.  If I have a PDF in one of the directories and I click it, it should open that PDF (in whatever default reader the browser uses).</p>
  
  <p>I've been Googling this topic and trying out a few things for a day or two and I can't seem to get it.  I find it strange that when I click on the PDF as my code currently is, the browser seems like it's opening the PDF but no text appears.  It's the standard in-browser PDF viewer that we're all used to seeing when we click a PDF link.  But there's no content.  It's just some blank pages.</p>
  
  <p>Can anyone help with this?  I'm not going to use an external library.  I just want to understand how to open the PDF file in Java.</p>
  
  <p>Thanks!</p>