List of usage examples for java.io DataInputStream readUnsignedShort
public final int readUnsignedShort() throws IOException
readUnsignedShort
method of DataInput
. From source file:org.apache.fop.afp.apps.FontPatternExtractor.java
/** * Extracts the Type1 PFB file from the given AFP outline font. * @param file the AFP file to read from * @param targetDir the target directory where the PFB file is to be placed. * @throws IOException if an I/O error occurs */// www . ja v a 2 s. c om public void extract(File file, File targetDir) throws IOException { InputStream in = new java.io.FileInputStream(file); try { MODCAParser parser = new MODCAParser(in); ByteArrayOutputStream baout = new ByteArrayOutputStream(); UnparsedStructuredField strucField; while ((strucField = parser.readNextStructuredField()) != null) { if (strucField.getSfTypeID() == 0xD3EE89) { byte[] sfData = strucField.getData(); println(strucField.toString()); HexDump.dump(sfData, 0, printStream, 0); baout.write(sfData); } } ByteArrayInputStream bin = new ByteArrayInputStream(baout.toByteArray()); DataInputStream din = new DataInputStream(bin); long len = din.readInt() & 0xFFFFFFFFL; println("Length: " + len); din.skip(4); //checksum int tidLen = din.readUnsignedShort() - 2; byte[] tid = new byte[tidLen]; din.readFully(tid); String filename = new String(tid, "ISO-8859-1"); int asciiCount1 = countUSAsciiCharacters(filename); String filenameEBCDIC = new String(tid, "Cp1146"); int asciiCount2 = countUSAsciiCharacters(filenameEBCDIC); println("TID: " + filename + " " + filenameEBCDIC); if (asciiCount2 > asciiCount1) { //Haven't found an indicator if the name is encoded in EBCDIC or not //so we use a trick. filename = filenameEBCDIC; } if (!filename.toLowerCase().endsWith(".pfb")) { filename = filename + ".pfb"; } println("Output filename: " + filename); File out = new File(targetDir, filename); OutputStream fout = new java.io.FileOutputStream(out); try { IOUtils.copyLarge(din, fout); } finally { IOUtils.closeQuietly(fout); } } finally { IOUtils.closeQuietly(in); } }
From source file:org.dcm4che2.tool.jpg2dcm.Jpg2Dcm.java
private void readHeader(DicomObject attrs, DataInputStream jpgInput) throws IOException { if (jpgInput.read() != FF || jpgInput.read() != SOI || jpgInput.read() != FF) { throw new IOException("JPEG stream does not start with FF D8 FF"); }//from ww w . ja va 2s . co m int marker = jpgInput.read(); int segmLen; boolean seenSOF = false; buffer[0] = (byte) FF; buffer[1] = (byte) SOI; buffer[2] = (byte) FF; buffer[3] = (byte) marker; jpgHeaderLen = 4; while (marker != SOS) { segmLen = jpgInput.readUnsignedShort(); if (buffer.length < jpgHeaderLen + segmLen + 2) { growBuffer(jpgHeaderLen + segmLen + 2); } buffer[jpgHeaderLen++] = (byte) (segmLen >>> 8); buffer[jpgHeaderLen++] = (byte) segmLen; jpgInput.readFully(buffer, jpgHeaderLen, segmLen - 2); if ((marker & 0xf0) == SOF && marker != DHT && marker != DAC) { seenSOF = true; int p = buffer[jpgHeaderLen] & 0xff; int y = ((buffer[jpgHeaderLen + 1] & 0xff) << 8) | (buffer[jpgHeaderLen + 2] & 0xff); int x = ((buffer[jpgHeaderLen + 3] & 0xff) << 8) | (buffer[jpgHeaderLen + 4] & 0xff); int nf = buffer[jpgHeaderLen + 5] & 0xff; attrs.putInt(Tag.SamplesPerPixel, VR.US, nf); if (nf == 3) { attrs.putString(Tag.PhotometricInterpretation, VR.CS, "YBR_FULL_422"); attrs.putInt(Tag.PlanarConfiguration, VR.US, 0); } else { attrs.putString(Tag.PhotometricInterpretation, VR.CS, "MONOCHROME2"); } attrs.putInt(Tag.Rows, VR.US, y); attrs.putInt(Tag.Columns, VR.US, x); attrs.putInt(Tag.BitsAllocated, VR.US, p > 8 ? 16 : 8); attrs.putInt(Tag.BitsStored, VR.US, p); attrs.putInt(Tag.HighBit, VR.US, p - 1); attrs.putInt(Tag.PixelRepresentation, VR.US, 0); } if (noAPPn & (marker & 0xf0) == APP) { jpgLen -= segmLen + 2; jpgHeaderLen -= 4; } else { jpgHeaderLen += segmLen - 2; } if (jpgInput.read() != FF) { throw new IOException("Missing SOS segment in JPEG stream"); } marker = jpgInput.read(); buffer[jpgHeaderLen++] = (byte) FF; buffer[jpgHeaderLen++] = (byte) marker; } if (!seenSOF) { throw new IOException("Missing SOF segment in JPEG stream"); } }
From source file:org.dcm4che3.tool.jpg2dcm.Jpg2Dcm.java
private void readHeader(Attributes attrs, DataInputStream jpgInput) throws IOException { if (jpgInput.read() != FF || jpgInput.read() != SOI || jpgInput.read() != FF) { throw new IOException("JPEG stream does not start with FF D8 FF"); }//w w w .ja va 2 s. c om int marker = jpgInput.read(); int segmLen; boolean seenSOF = false; buffer[0] = (byte) FF; buffer[1] = (byte) SOI; buffer[2] = (byte) FF; buffer[3] = (byte) marker; jpgHeaderLen = 4; while (marker != SOS) { segmLen = jpgInput.readUnsignedShort(); if (buffer.length < jpgHeaderLen + segmLen + 2) { growBuffer(jpgHeaderLen + segmLen + 2); } buffer[jpgHeaderLen++] = (byte) (segmLen >>> 8); buffer[jpgHeaderLen++] = (byte) segmLen; jpgInput.readFully(buffer, jpgHeaderLen, segmLen - 2); if ((marker & 0xf0) == SOF && marker != DHT && marker != DAC) { seenSOF = true; int p = buffer[jpgHeaderLen] & 0xff; int y = ((buffer[jpgHeaderLen + 1] & 0xff) << 8) | (buffer[jpgHeaderLen + 2] & 0xff); int x = ((buffer[jpgHeaderLen + 3] & 0xff) << 8) | (buffer[jpgHeaderLen + 4] & 0xff); int nf = buffer[jpgHeaderLen + 5] & 0xff; attrs.setInt(Tag.SamplesPerPixel, VR.US, nf); if (nf == 3) { attrs.setString(Tag.PhotometricInterpretation, VR.CS, "YBR_FULL_422"); attrs.setInt(Tag.PlanarConfiguration, VR.US, 0); } else { attrs.setString(Tag.PhotometricInterpretation, VR.CS, "MONOCHROME2"); } attrs.setInt(Tag.Rows, VR.US, y); attrs.setInt(Tag.Columns, VR.US, x); attrs.setInt(Tag.BitsAllocated, VR.US, p > 8 ? 16 : 8); attrs.setInt(Tag.BitsStored, VR.US, p); attrs.setInt(Tag.HighBit, VR.US, p - 1); attrs.setInt(Tag.PixelRepresentation, VR.US, 0); } if (noAPPn & (marker & 0xf0) == APP) { jpgLen -= segmLen + 2; jpgHeaderLen -= 4; } else { jpgHeaderLen += segmLen - 2; } if (jpgInput.read() != FF) { throw new IOException("Missing SOS segment in JPEG stream"); } marker = jpgInput.read(); buffer[jpgHeaderLen++] = (byte) FF; buffer[jpgHeaderLen++] = (byte) marker; } if (!seenSOF) { throw new IOException("Missing SOF segment in JPEG stream"); } }
From source file:org.echocat.jomon.net.dns.DnsServer.java
public void TCPclient(Socket s) { try {//from w w w.j a v a2 s . c o m final int inLength; final DataInputStream dataIn; final DataOutputStream dataOut; final byte[] in; final InputStream is = s.getInputStream(); dataIn = new DataInputStream(is); inLength = dataIn.readUnsignedShort(); in = new byte[inLength]; dataIn.readFully(in); final Message query; byte[] response; try { query = new Message(in); response = generateReply(query, in, in.length, s); if (response == null) { return; } } catch (final IOException ignored) { response = formerrMessage(in); } dataOut = new DataOutputStream(s.getOutputStream()); dataOut.writeShort(response.length); dataOut.write(response); } catch (final IOException e) { LOG.warn("TCPclient(" + addrport(s.getLocalAddress(), s.getLocalPort()) + ").", e); } finally { try { s.close(); } catch (final IOException ignored) { } } }
From source file:org.jboss.windup.decorator.archive.JVMVersionDecorator.java
@Override public void processMeta(ZipMetadata meta) { try {//from w w w . ja v a 2 s. c o m ZipEntry entry; Enumeration<?> e = meta.getZipFile().entries(); // locate a random class entry... while (e.hasMoreElements()) { entry = (ZipEntry) e.nextElement(); if (StringUtils.endsWith(entry.getName(), ".class")) { String version = null; DataInputStream in = null; try { in = new DataInputStream(meta.getZipFile().getInputStream(entry)); // ignore Java "magic" number. in.readInt(); int minor = in.readUnsignedShort(); int major = in.readUnsignedShort(); switch (major) { case ClassFile.JAVA_1: version = "1.1"; break; case ClassFile.JAVA_2: version = "1.2"; break; case ClassFile.JAVA_3: version = "1.3"; break; case ClassFile.JAVA_4: version = "1.4"; break; case ClassFile.JAVA_5: version = "5.0"; break; case ClassFile.JAVA_6: version = "6.0"; break; case ClassFile.JAVA_7: version = "7.0"; break; default: LOG.warn("No version mapping for: " + version); } version = version + "." + minor; } finally { in.close(); } JVMBuildVersionResult vr = new JVMBuildVersionResult(); vr.setJdkBuildVersion(version); meta.getDecorations().add(vr); if (LOG.isDebugEnabled()) { LOG.debug("Built with: " + version); } break; } } } catch (Exception e) { LOG.error("Exception getting JDK version.", e); } }
From source file:org.neo4j.ogm.metadata.ClassInfo.java
public ClassInfo(InputStream inputStream) throws IOException { DataInputStream dataInputStream = new DataInputStream(new BufferedInputStream(inputStream, 1024)); // Magic/*from w ww . j ava2 s.c o m*/ if (dataInputStream.readInt() != 0xCAFEBABE) { return; } dataInputStream.readUnsignedShort(); //minor version dataInputStream.readUnsignedShort(); // major version ConstantPool constantPool = new ConstantPool(dataInputStream); // Access flags int flags = dataInputStream.readUnsignedShort(); isInterface = (flags & 0x0200) != 0; isAbstract = (flags & 0x0400) != 0; isEnum = (flags & 0x4000) != 0; className = constantPool.lookup(dataInputStream.readUnsignedShort()).replace('/', '.'); String sce = constantPool.lookup(dataInputStream.readUnsignedShort()); if (sce != null) { directSuperclassName = sce.replace('/', '.'); } interfacesInfo = new InterfacesInfo(dataInputStream, constantPool); fieldsInfo = new FieldsInfo(dataInputStream, constantPool); methodsInfo = new MethodsInfo(dataInputStream, constantPool); annotationsInfo = new AnnotationsInfo(dataInputStream, constantPool); new ClassValidator(this).validate(); }
From source file:org.opensc.pkcs15.token.impl.CardOSToken.java
@Override public TokenFile select(int path) throws IOException { if (this.currentFile == null) throw new IOException("No current DF selected."); // SELECT FILE, P1=0x00, P2=0x00, ID -> select EF or DF CommandAPDU cmd = new CommandAPDU(0x00, 0xA4, 0x00, 0x00, PathHelper.idToPath(path), DEFAULT_LE); try {// w ww. j a va 2 s . co m ResponseAPDU resp = this.channel.transmit(cmd); DataInputStream dis = getSelectFileData(resp); long bodySize = -1; long fileSize = -1; int acRead = TokenFileAcl.AC_ALWAYS; int acUpdate = TokenFileAcl.AC_ALWAYS; int acAppend = TokenFileAcl.AC_ALWAYS; int acDeactivate = TokenFileAcl.AC_ALWAYS; int acActivate = TokenFileAcl.AC_ALWAYS; int acDelete = TokenFileAcl.AC_ALWAYS; int acAdmin = TokenFileAcl.AC_ALWAYS; int acIncrease = TokenFileAcl.AC_ALWAYS; int acDecrease = TokenFileAcl.AC_ALWAYS; int tag; while ((tag = dis.read()) >= 0) { int n = dis.read(); if (n < 0) break; switch (tag) { case 0x80: if (n != 2) throw new IOException("Invalid length [" + n + "] of FCI tag 0x80."); fileSize = dis.readUnsignedShort(); break; case 0x83: if (n != 2) throw new IOException("Invalid length [" + n + "] of FCI tag 0x83."); int tpath = dis.readUnsignedShort(); if (tpath != path) throw new IOException("File ID [" + PathHelper.formatID(tpath) + "] reported by SELECT FILE differs from requested ID [" + PathHelper.formatID(path) + "]."); break; case 0x81: if (n != 2) throw new IOException("Invalid length [" + n + "] of FCI tag 0x81."); bodySize = dis.readUnsignedShort(); break; case 0x86: if (n >= 1) acRead = dis.read(); if (n >= 2) acUpdate = dis.read(); if (n >= 3) acAppend = dis.read(); if (n >= 4) acDeactivate = dis.read(); if (n >= 5) acActivate = dis.read(); if (n >= 6) acDelete = dis.read(); if (n >= 7) acAdmin = dis.read(); if (n >= 8) acIncrease = dis.read(); if (n >= 9) acDecrease = dis.read(); if (n != 9 && n != 8) log.warn("Invalid length [" + n + "] of FCI tag 0x86 for EF."); if (n > 9) dis.skipBytes(n - 9); break; default: byte[] tmp = new byte[n]; dis.readFully(tmp); log.warn("skipping FCI tag [0x" + Integer.toHexString(tag) + "], data [" + Util.asHex(tmp) + "]."); } } if (fileSize >= 0) this.currentFile = new EF(new TokenPath(this.currentFile.getPath(), path), fileSize, acRead, acUpdate, acAppend, acDeactivate, acActivate, acDelete, acAdmin, acIncrease, acDecrease); else if (bodySize >= 0) this.currentFile = new DF(new TokenPath(this.currentFile.getPath(), path), bodySize, acRead, acUpdate, acAppend, acDeactivate, acActivate, acDelete, acAdmin, acIncrease); else throw new IOException("No 0x80 or 0x81 tag specified in order to distinguish between DF an EF."); return this.currentFile; } catch (CardException e) { throw new PKCS15Exception("Error sending SELECT FILE", e); } }
From source file:org.opensc.pkcs15.token.impl.CardOSToken.java
private DF selectDFInternal(CommandAPDU cmd, TokenPath targetPath) throws IOException { try {//from w ww . java 2 s .com ResponseAPDU resp = this.channel.transmit(cmd); DataInputStream dis = getSelectFileData(resp); long bodySize = 0; int acLifeCycle = TokenFileAcl.AC_ALWAYS; int acUpdate = TokenFileAcl.AC_ALWAYS; int acAppend = TokenFileAcl.AC_ALWAYS; int acDeactivate = TokenFileAcl.AC_ALWAYS; int acActivate = TokenFileAcl.AC_ALWAYS; int acDelete = TokenFileAcl.AC_ALWAYS; int acAdmin = TokenFileAcl.AC_ALWAYS; int acCreate = TokenFileAcl.AC_ALWAYS; int tag; while ((tag = dis.read()) >= 0) { int n = dis.read(); if (n < 0) break; switch (tag) { case 0x81: if (n != 2) throw new IOException("Invalid length [" + n + "] of FCI tag 0x81."); bodySize = dis.readUnsignedShort(); break; case 0x83: if (n != 2) throw new IOException("Invalid length [" + n + "] of FCI tag 0x83."); int tpath = dis.readUnsignedShort(); if (tpath != targetPath.getTailID()) throw new IOException("File ID [" + PathHelper.formatID(tpath) + "] reported by SELECT FILE differs from requested ID [" + PathHelper.formatID(targetPath.getTailID()) + "]."); break; case 0x86: if (n >= 1) acLifeCycle = dis.read(); if (n >= 2) acUpdate = dis.read(); if (n >= 3) acAppend = dis.read(); if (n >= 4) acDeactivate = dis.read(); if (n >= 5) acActivate = dis.read(); if (n >= 6) acDelete = dis.read(); if (n >= 7) acAdmin = dis.read(); if (n >= 8) acCreate = dis.read(); if (n != 8) log.warn("Invalid length [" + n + "] of FCI tag 0x86 for DF."); if (n > 8) dis.skipBytes(n - 8); break; default: byte[] tmp = new byte[n]; dis.readFully(tmp); log.warn("skipping FCI tag [0x" + Integer.toHexString(tag) + "], data [" + Util.asHex(tmp) + "]."); } } DF df = new DF(targetPath, bodySize, acLifeCycle, acUpdate, acAppend, acDeactivate, acActivate, acDelete, acAdmin, acCreate); this.currentFile = df; return df; } catch (CardException e) { throw new PKCS15Exception("Error sending select MF", e); } }
From source file:org.opensc.pkcs15.token.impl.CardOSToken.java
@Override public EF selectEF(int path) throws IOException { if (this.currentFile == null) throw new IOException("No current DF selected."); // SELECT FILE, P1=0x02, P2=0x00, no data -> select EF CommandAPDU cmd = new CommandAPDU(0x00, 0xA4, 0x02, 0x00, PathHelper.idToPath(path), DEFAULT_LE); try {/*from www .j av a 2s . c om*/ ResponseAPDU resp = this.channel.transmit(cmd); DataInputStream dis = getSelectFileData(resp); long fileSize = 0; int acRead = TokenFileAcl.AC_ALWAYS; int acUpdate = TokenFileAcl.AC_ALWAYS; int acAppend = TokenFileAcl.AC_ALWAYS; int acDeactivate = TokenFileAcl.AC_ALWAYS; int acActivate = TokenFileAcl.AC_ALWAYS; int acDelete = TokenFileAcl.AC_ALWAYS; int acAdmin = TokenFileAcl.AC_ALWAYS; int acIncrease = TokenFileAcl.AC_ALWAYS; int acDecrease = TokenFileAcl.AC_ALWAYS; int tag; while ((tag = dis.read()) >= 0) { int n = dis.read(); if (n < 0) break; switch (tag) { case 0x80: if (n != 2) throw new IOException("Invalid length [" + n + "] of FCI tag 0x80."); fileSize = dis.readUnsignedShort(); break; case 0x83: if (n != 2) throw new IOException("Invalid length [" + n + "] of FCI tag 0x83."); int tpath = dis.readUnsignedShort(); if (tpath != path) throw new IOException("File ID [" + PathHelper.formatID(tpath) + "] reported by SELECT FILE differs from requested ID [" + PathHelper.formatID(path) + "]."); break; case 0x86: if (n >= 1) acRead = dis.read(); if (n >= 2) acUpdate = dis.read(); if (n >= 3) acAppend = dis.read(); if (n >= 4) acDeactivate = dis.read(); if (n >= 5) acActivate = dis.read(); if (n >= 6) acDelete = dis.read(); if (n >= 7) acAdmin = dis.read(); if (n >= 8) acIncrease = dis.read(); if (n >= 9) acDecrease = dis.read(); if (n != 9) log.warn("Invalid length [" + n + "] of FCI tag 0x86 for EF."); if (n > 9) dis.skipBytes(n - 9); break; default: byte[] tmp = new byte[n]; dis.readFully(tmp); log.warn("skipping FCI tag [0x" + Integer.toHexString(tag) + "], data [" + Util.asHex(tmp) + "]."); } } EF ef = new EF(new TokenPath(this.currentFile.getPath(), path), fileSize, acRead, acUpdate, acAppend, acDeactivate, acActivate, acDelete, acAdmin, acIncrease, acDecrease); this.currentFile = ef; return ef; } catch (CardException e) { throw new PKCS15Exception("Error sending select MF", e); } }
From source file:org.opensc.pkcs15.token.impl.CardOSToken.java
@Override public MF selectMF() throws IOException { // SELECT FILE, P1=0x00, P2=0x00, no data -> select MF CommandAPDU cmd = new CommandAPDU(0x00, 0xA4, 0x00, 0x00, DEFAULT_LE); try {// www.j ava 2s . co m ResponseAPDU resp = this.channel.transmit(cmd); DataInputStream dis = getSelectFileData(resp); long bodySize = 0; int acLifeCycle = TokenFileAcl.AC_ALWAYS; int acUpdate = TokenFileAcl.AC_ALWAYS; int acAppend = TokenFileAcl.AC_ALWAYS; int acDeactivate = TokenFileAcl.AC_ALWAYS; int acActivate = TokenFileAcl.AC_ALWAYS; int acDelete = TokenFileAcl.AC_ALWAYS; int acAdmin = TokenFileAcl.AC_ALWAYS; int acCreate = TokenFileAcl.AC_ALWAYS; int acExecute = TokenFileAcl.AC_ALWAYS; int acAllocate = TokenFileAcl.AC_ALWAYS; int tag; while ((tag = dis.read()) >= 0) { int n = dis.read(); if (n < 0) break; switch (tag) { case 0x81: if (n != 2) throw new IOException("Invalid length [" + n + "] of FCI tag 0x81."); bodySize = dis.readUnsignedShort(); break; case 0x83: if (n != 2) throw new IOException("Invalid length [" + n + "] of FCI tag 0x83."); int tpath = dis.readUnsignedShort(); if (tpath != PathHelper.MF_ID) throw new IOException("File ID [" + PathHelper.formatID(tpath) + "] reported by SELECT FILE differs from requested ID [" + PathHelper.formatID(PathHelper.MF_ID) + "]."); break; case 0x86: if (n >= 1) acLifeCycle = dis.read(); if (n >= 2) acUpdate = dis.read(); if (n >= 3) acAppend = dis.read(); if (n >= 4) acDeactivate = dis.read(); if (n >= 5) acActivate = dis.read(); if (n >= 6) acDelete = dis.read(); if (n >= 7) acAdmin = dis.read(); if (n >= 8) acCreate = dis.read(); if (n >= 9) acExecute = dis.read(); if (n >= 10) acAllocate = dis.read(); if (n != 10) log.warn("Invalid length [" + n + "] of FCI tag 0x86 for MF."); if (n > 10) dis.skipBytes(n - 10); break; default: byte[] tmp = new byte[n]; dis.readFully(tmp); log.warn("skipping FCI tag [0x" + Integer.toHexString(tag) + "], data [" + Util.asHex(tmp) + "]."); } } MF mf = new MF(PathHelper.MF_PATH, bodySize, acLifeCycle, acUpdate, acAppend, acDeactivate, acActivate, acDelete, acAdmin, acCreate, acExecute, acAllocate); this.currentFile = mf; return mf; } catch (CardException e) { throw new PKCS15Exception("Error sending select MF", e); } }