Example usage for java.io DataInputStream readFully

List of usage examples for java.io DataInputStream readFully

Introduction

In this page you can find the example usage for java.io DataInputStream readFully.

Prototype

public final void readFully(byte b[]) throws IOException 

Source Link

Document

See the general contract of the readFully method of DataInput .

Usage

From source file:com.athena.chameleon.engine.core.analyzer.parser.Parser.java

/**
 * <pre>// ww  w .  j a  v a2 s . c om
 * ?? ? ?  .
 * </pre>
 * @param file
 * @return
 * @throws IOException 
 */
protected String fileToString(String file) throws IOException {

    // return IOUtils.toString(file.toURI());

    String result = null;

    DataInputStream in = null;
    File f = new File(file);
    byte[] buffer = new byte[(int) f.length()];
    in = new DataInputStream(new FileInputStream(f));
    in.readFully(buffer);
    result = new String(buffer);
    IOUtils.closeQuietly(in);

    return result;
}

From source file:net.jradius.webservice.WebServiceListener.java

private byte[] getContent(DataInputStream reader, int clen) throws IOException {
    byte[] buf = new byte[clen];
    reader.readFully(buf);
    return buf;//from   w  w w.  j av a  2s .c  o m
}

From source file:org.sakaiproject.portal.charon.test.http.AnonPortalTest.java

@Override
protected void setUp() throws Exception {
    try {/*from w  ww.jav a 2  s  .  c  o m*/
        wc = new WebConversation();
        WebRequest req = new GetMethodWebRequest(TEST_URL);
        WebResponse resp = wc.getResponse(req);
        DataInputStream inputStream = new DataInputStream(resp.getInputStream());
        buffer = new byte[resp.getContentLength()];
        inputStream.readFully(buffer);
        visited = new HashMap<String, String>();
    } catch (Exception notfound) {
        enabled = false;
    }
}

From source file:org.wso2.carbon.identity.agent.onprem.userstore.security.JWTSecurityInterceptor.java

private boolean isValid(String jwtToken) {

    String[] jwtTokenValues = jwtToken.split("\\.");
    String jwtAssertion = null;/*from  w  ww.j a v a  2s. co m*/
    byte[] jwtSignature = null;

    if (jwtTokenValues.length > 0) {
        String value = new String(base64Url.decode(jwtTokenValues[0].getBytes()));
        JSONParser parser = new JSONParser();
        try {
            jsonHeaderObject = (JSONObject) parser.parse(value);
        } catch (ParseException e) {
            log.error("Error occurred while parsing JSON header ", e);
        }
    }

    if (jwtTokenValues.length > 1) {
        jwtAssertion = jwtTokenValues[0] + "." + jwtTokenValues[1];
    }

    if (jwtTokenValues.length > 2) {
        jwtSignature = base64Url.decode(jwtTokenValues[2].getBytes());
    }

    if (jwtAssertion != null && jwtSignature != null) {

        try {
            File publicKeyFile = new File(System.getProperty(CommonConstants.CARBON_HOME),
                    File.separator + PUBLIC_KEY_LOCATION);
            InputStream inStream = new FileInputStream(publicKeyFile);

            DataInputStream dis = new DataInputStream(inStream);
            byte[] keyBytes = new byte[(int) publicKeyFile.length()];
            dis.readFully(keyBytes);
            dis.close();
            String publicKeyPEM = new String(keyBytes);
            BASE64Decoder b64 = new BASE64Decoder();
            byte[] decoded = b64.decodeBuffer(publicKeyPEM);

            X509EncodedKeySpec spec = new X509EncodedKeySpec(decoded);
            KeyFactory kf = KeyFactory.getInstance("RSA");
            PublicKey publicKey = kf.generatePublic(spec);

            Signature signature = Signature.getInstance(getSignatureAlgorithm(jsonHeaderObject));
            signature.initVerify(publicKey);
            signature.update(jwtAssertion.getBytes());
            return signature.verify(jwtSignature);
        } catch (Exception e) {
            log.error("Error occurred while validating signature", e);
        }
    } else {
        log.warn("No signature exist in the request.");
        return false;
    }
    return false;
}

From source file:com.qut.middleware.spep.authn.bindings.impl.AuthnPostBindingImpl.java

public AuthnPostBindingImpl() {
    try {/*from  w w  w  .ja  va  2s  .  c  o  m*/
        InputStream inputStream = this.getClass().getResourceAsStream("samlPostRequestTemplate.html");
        DataInputStream dataInputStream = new DataInputStream(inputStream);

        byte[] document = new byte[dataInputStream.available()];
        dataInputStream.readFully(document);

        // Load the document as UTF-8 format and create a formatter for it.
        String samlResponseTemplate = new String(document, "UTF-8");
        this.samlMessageFormat = new MessageFormat(samlResponseTemplate);

        this.logger.info("Created AuthnPostBindingImpl successfully");
    } catch (IOException e) {
        throw new IllegalArgumentException("HTTP POST binding form could not be loaded due to an I/O error", e);
    }

}

From source file:com.galois.qrstream.MainActivity.java

private byte[] readFileUri(Uri uri) throws IOException {
    ContentResolver contentResolver = getContentResolver();
    AssetFileDescriptor fd = contentResolver.openAssetFileDescriptor(uri, "r");
    long fileLength = fd.getLength();
    Log.d(Constants.APP_TAG, "fd length " + fileLength);
    DataInputStream istream = new DataInputStream(contentResolver.openInputStream(uri));
    byte[] buf = new byte[(int) fileLength];
    istream.readFully(buf);
    return buf;//from w  w w.  j  ava 2s.  c o m
}

From source file:cn.xiongyihui.wificar.MjpegStream.java

public Bitmap readFrame(DataInputStream in) throws IOException {
    int mContentLength = -1;

    in.mark(FRAME_MAX_LENGTH);//from ww w  . j  av  a2  s. c  o  m
    int headerLen = getStartOfSequence(in, SOI_MARKER);
    in.reset();
    byte[] header = new byte[headerLen];
    in.readFully(header);
    try {
        mContentLength = parseContentLength(header);
    } catch (NumberFormatException nfe) {
        mContentLength = getEndOfSeqeunce(in, EOF_MARKER);
    }
    in.reset();
    byte[] frameData = new byte[mContentLength];
    in.skipBytes(headerLen);
    in.readFully(frameData);
    return BitmapFactory.decodeStream(new ByteArrayInputStream(frameData));
}

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
 *///from www.ja v  a  2  s  . com
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.apache.axiom.util.blob.WritableBlobTestBase.java

public void testMarkReset() throws IOException {
    byte[] sourceData1 = new byte[2000];
    byte[] sourceData2 = new byte[2000];
    random.nextBytes(sourceData1);/*from   w w w. j  a va2s.  com*/
    random.nextBytes(sourceData2);
    WritableBlob blob = createBlob();
    try {
        OutputStream out = blob.getOutputStream();
        out.write(sourceData1);
        out.write(sourceData2);
        out.close();
        DataInputStream in = new DataInputStream(blob.getInputStream());
        byte[] data1 = new byte[sourceData1.length];
        byte[] data2 = new byte[sourceData2.length];
        in.readFully(data1);
        in.mark(sourceData2.length);
        in.readFully(data2);
        in.reset();
        in.readFully(data2);
        assertTrue(Arrays.equals(sourceData1, data1));
        assertTrue(Arrays.equals(sourceData2, data2));
    } finally {
        releaseBlob(blob);
    }
}

From source file:org.getspout.spout.packet.PacketCacheFile.java

public void readData(DataInputStream input) throws IOException {
    this.fileName = PacketUtil.readString(input);
    this.plugin = PacketUtil.readString(input);
    compressed = input.readBoolean();/*from   w ww. ja v  a  2s .c o  m*/
    int size = input.readInt();
    this.fileData = new byte[size];
    input.readFully(this.fileData);
}