Skip to content

Commit

Permalink
Java 11 feature: readNBytes
Browse files Browse the repository at this point in the history
  • Loading branch information
gmuth committed Jul 28, 2023
1 parent a8a76f8 commit 64a6867
Showing 1 changed file with 13 additions and 22 deletions.
35 changes: 13 additions & 22 deletions src/main/java/ipp/IppPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,20 @@

// Author: Gerhard Muth

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URI;

class IppPrinter {

public static void main(final String[] args) {
try {
URI uri = new URI("ipp://localhost:8632/printers/laser");
// printer uri for PrinterSimulator v87 (Apple)
URI uri = new URI("ipp://localhost:8632/ipp/print/laser");
File file = new File("demo/A4-blank.pdf");
if (args.length > 0) {
uri = new URI(args[0]);
}
if (args.length > 1) {
file = new File(args[1]);
}
if (args.length > 0) uri = new URI(args[0]);
if (args.length > 1) file = new File(args[1]);
new IppPrinter(uri).printJob(file);

} catch (Exception exception) {
exception.printStackTrace(System.err);
}
Expand All @@ -41,7 +33,7 @@ public static void main(final String[] args) {
public void printJob(final File file) throws IOException {
System.out.printf("send %s to %s%n", file.getName(), uri);
String httpScheme = uri.getScheme().replace("ipp", "http");
URI httpUri = URI.create(String.format("%s:%s", httpScheme, uri.getSchemeSpecificPart()));
URI httpUri = URI.create(String.format("%s:%s", httpScheme, uri.getRawSchemeSpecificPart()));
HttpURLConnection httpUrlConnection = (HttpURLConnection) httpUri.toURL().openConnection();
httpUrlConnection.setConnectTimeout(5000);
httpUrlConnection.setDoOutput(true);
Expand All @@ -58,11 +50,10 @@ public void printJob(final File file) throws IOException {
writeAttribute(0x48, "attributes-natural-language", "en"); // natural-language tag
writeAttribute(0x45, "printer-uri", uri.toString()); // uri tag
dataOutputStream.writeByte(0x03); // end tag

// append document
// byte[] buffer = new byte[4096]; // Java <9
// for (int length; (length = documentInputStream.read(buffer)) != -1; outputStream.write(buffer, 0, length));
new FileInputStream(file).transferTo(dataOutputStream); // Java >= 9
new FileInputStream(file) {{
transferTo(dataOutputStream);
close();
}};

// check http response
if (httpUrlConnection.getResponseCode() != 200) {
Expand Down Expand Up @@ -110,6 +101,8 @@ public void printJob(final File file) throws IOException {
System.out.printf(" %s (0x%02X) = %s%n", name, tag, value);
} while (tag != (byte) 0x03); // end tag
// job-state -> https://tools.ietf.org/html/rfc8011#section-5.3.7
dataOutputStream.close();
dataInputStream.close();
}

private void writeAttribute(final Integer tag, final String name, final String value) throws IOException {
Expand All @@ -121,9 +114,7 @@ private void writeAttribute(final Integer tag, final String name, final String v
}

private String readStringValue() throws IOException {
byte[] valueBytes = new byte[dataInputStream.readShort()];
dataInputStream.read(valueBytes);
byte[] valueBytes = dataInputStream.readNBytes(dataInputStream.readShort());
return new String(valueBytes, "us-ascii");
// Java 11: return dataInputStream.readNBytes(dataInputStream.readShort());
}
}

0 comments on commit 64a6867

Please sign in to comment.