-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2dfa180
commit 633c2b0
Showing
6 changed files
with
172 additions
and
36 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package com.mercurys.handlers; | ||
|
||
import java.io.*; | ||
import java.nio.file.*; | ||
import java.text.*; | ||
import java.util.Calendar; | ||
|
||
public class PDFHandler { | ||
|
||
private DataInputStream inputStream; | ||
private DataOutputStream outputStream; | ||
private String pdfDownloadPath; | ||
|
||
public PDFHandler(DataOutputStream outputStream) { | ||
this.outputStream = outputStream; | ||
} | ||
|
||
public PDFHandler(DataInputStream inputStream) { | ||
this.inputStream = inputStream; | ||
} | ||
|
||
public static void main(String[] args) { | ||
try { | ||
PDFHandler printerOfPDFs = new PDFHandler(new DataOutputStream(new FileOutputStream( | ||
"/home/raghav/Documents/Java/Testing.txt"))); | ||
PDFHandler downloaderOfPDFS = new PDFHandler(new DataInputStream(new FileInputStream( | ||
"/home/raghav/Documents/Java/Testing.txt"))); | ||
|
||
printerOfPDFs.sendPDFFile( | ||
"/home/raghav/Documents/Books/Non Fiction/Plane-Trigonometry-Part-I.pdf"); | ||
System.out.println("PDF read and printed to output"); | ||
downloaderOfPDFS.downloadPDF(); | ||
System.out.println("PDF downloaded"); | ||
} catch (FileNotFoundException e) { | ||
e.printStackTrace(); | ||
System.exit(0); | ||
} | ||
} | ||
|
||
public void sendPDFFile(final String pdfFileName) { | ||
try { | ||
this.outputStream.writeInt(getPDFAsByteArray(pdfFileName).length); | ||
this.outputStream.write(getPDFAsByteArray(pdfFileName)); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
private byte[] getPDFAsByteArray(String fileName) throws IOException { | ||
Path pathToPDF = Paths.get(fileName); | ||
return Files.readAllBytes(pathToPDF); | ||
} | ||
|
||
public void downloadPDF() { | ||
try { | ||
byte[] fileToDownload = readPDFByteArray(); | ||
pdfDownloadPath = setDownloadLocation(); | ||
writePDFToDownloadLocation(fileToDownload); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
private byte[] readPDFByteArray() throws IOException { | ||
byte[] pdfAsBytes = new byte[inputStream.readInt()]; | ||
inputStream.readFully(pdfAsBytes); | ||
return pdfAsBytes; | ||
} | ||
|
||
private void writePDFToDownloadLocation(byte[] fileAsByteArray) throws IOException { | ||
createDirectoriesToDownloadLocation(); | ||
Path pathToFile = Paths.get(pdfDownloadPath); | ||
Files.write(pathToFile, fileAsByteArray); | ||
} | ||
|
||
private void createDirectoriesToDownloadLocation() throws IOException { | ||
String s = System.getProperty("file.separator"); | ||
Files.createDirectories(Paths.get( | ||
pdfDownloadPath.substring(0, pdfDownloadPath.lastIndexOf(s)) + s)); | ||
} | ||
|
||
private String setDownloadLocation() { | ||
final String s = System.getProperty("file.separator"); | ||
return MessageFormat.format("{0}{1}Documents{1}Mercurys{1}ReceivedFile{2}.pdf", | ||
System.getProperty("user.home"), s, getFormattedSimpleDate()); | ||
} | ||
|
||
private String getFormattedSimpleDate() { | ||
return new SimpleDateFormat("ddMMyyyy-HHmmss").format(Calendar.getInstance().getTime()); | ||
} | ||
|
||
public String getPdfDownloadPath() { | ||
return pdfDownloadPath; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters