Example usage for java.io DataInputStream read

List of usage examples for java.io DataInputStream read

Introduction

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

Prototype

public int read() throws IOException 

Source Link

Document

Reads the next byte of data from this input stream.

Usage

From source file:org.ow2.proactive.authentication.crypto.Credentials.java

/**
 * Retrieves a public key stored in a local file
 * <p>/*from ww  w  .  ja  va 2 s .  co  m*/
 * 
 * @param pubPath path to the public key on the local filesystem
 * @return the key encapsulated in a regular JCE container
 * @throws KeyException the key could not be retrieved or is malformed
 */
public static PublicKey getPublicKey(String pubPath) throws KeyException {
    byte[] bytes;
    File f = new File(pubPath);
    FileInputStream fin;

    String algo = "", tmp = "";

    // recover public key bytes
    try {
        fin = new FileInputStream(f);
        DataInputStream in = new DataInputStream(fin);
        int read, tot = 0;
        while ((read = in.read()) != '\n') {
            algo += (char) read;
            tot++;
        }
        tot++;
        while ((read = in.read()) != '\n') {
            tmp += (char) read;
            tot++;
        }
        tot++;

        bytes = new byte[(int) f.length() - tot];
        in.readFully(bytes);
        in.close();
    } catch (Exception e) {
        throw new KeyException("Could not retrieve public key from " + pubPath, e);
    }

    // reconstruct public key
    X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(bytes);
    PublicKey pubKey;
    KeyFactory keyFactory;

    try {
        keyFactory = KeyFactory.getInstance(algo);
    } catch (NoSuchAlgorithmException e) {
        throw new KeyException("Cannot initialize key factory", e);
    }

    try {
        pubKey = keyFactory.generatePublic(pubKeySpec);
    } catch (InvalidKeySpecException e) {
        throw new KeyException("Cannot re-generate public key", e);
    }

    return pubKey;
}

From source file:org.ow2.proactive.authentication.crypto.Credentials.java

/**
 * Creates a Credentials given its base64 encoded representation
 * // w w  w .jav  a 2 s  .  c o  m
 * @param base64enc the Credentials representation as a base64 encoded byte array,
 *  as returned by {@link Credentials#getBase64()}
 * @return the Credentials object corresponding the <code>base64en</code> representation
 * @throws KeyException
 */
public static Credentials getCredentialsBase64(byte[] base64enc) throws KeyException {
    String algo = "", cipher = "", tmp = "";
    byte[] data;
    byte[] aes;
    int size;
    byte[] asciiEnc;

    try {
        asciiEnc = Base64.decodeBase64(base64enc);
    } catch (Exception e) {
        throw new KeyException("Unable to decode base64 credentials", e);
    }

    try {
        DataInputStream in = new DataInputStream(new ByteArrayInputStream(asciiEnc));
        int read, tot = 0;
        while ((read = in.read()) != '\n') {
            if (read == -1)
                throw new KeyException("Failed to parse malformed credentials");
            algo += (char) read;
            tot++;
        }
        tot++;
        while ((read = in.read()) != '\n') {
            if (read == -1)
                throw new KeyException("Failed to parse malformed credentials");
            tmp += (char) read;
            tot++;
        }
        tot++;
        size = Integer.parseInt(tmp);
        while ((read = in.read()) != '\n') {
            if (read == -1)
                throw new KeyException("Failed to parse malformed credentials");
            cipher += (char) read;
            tot++;
        }
        tot++;
        aes = new byte[size / 8];
        for (int i = 0; i < size / 8; i++) {
            aes[i] = (byte) in.read();
            tot++;
        }

        data = new byte[asciiEnc.length - tot];
        in.readFully(data);
    } catch (Exception e) {
        throw new KeyException("Could not decode credentials", e);
    }

    return new Credentials(algo, size, cipher, aes, data);
}

From source file:com.max2idea.android.fwknop.Fwknop.java

public static String sendHttpGet(String url) {
    HttpConnection hcon = null;// ww  w  .java 2s .co m
    DataInputStream dis = null;
    java.net.URL URL = null;
    try {
        URL = new java.net.URL(url);
    } catch (MalformedURLException ex) {
        Logger.getLogger(Fwknop.class.getName()).log(Level.SEVERE, null, ex);
    }
    StringBuffer responseMessage = new StringBuffer();

    try {
        // obtain a DataInputStream from the HttpConnection
        dis = new DataInputStream(URL.openStream());

        // retrieve the response from the server
        int ch;
        while ((ch = dis.read()) != -1) {
            responseMessage.append((char) ch);
        } //end while ( ( ch = dis.read() ) != -1 )
    } catch (Exception e) {
        e.printStackTrace();
        responseMessage.append(e.getMessage());
    } finally {
        try {
            if (hcon != null) {
                hcon.close();
            }
            if (dis != null) {
                dis.close();
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } //end try/catch
    } //end try/catch/finally
    return responseMessage.toString();
}

From source file:com.cyc.tool.distributedrepresentations.Word2VecSpaceFromFile.java

private String getVocabString(DataInputStream s) throws IOException {
    sb.setLength(0);/*from  w  w w.j a  v a  2 s.co m*/
    for (char ch = (char) s.read(); (!Character.isWhitespace(ch) && ch >= 0
            && ch <= 256); ch = (char) s.read()) {
        sb.append((char) ch);
    }
    return sb.toString();
}

From source file:com.cyc.tool.distributedrepresentations.Word2VecSpaceFromFile.java

private void getWordsAndSize(DataInputStream s) throws IOException {
    sb.setLength(0);//from ww w  .  j  a v a2  s.com
    for (char ch = (char) s.read(); ch != '\n'; ch = (char) s.read()) {
        sb.append(ch);
    }
    String[] parts = sb.toString().split("\\s+");
    words = Long.parseLong(parts[0]);
    setSize((int) Long.parseLong(parts[1]));
}

From source file:org.apache.wink.itest.standard.JAXRSFileTest.java

/**
 * Tests posting to a File entity parameter.
 * //from   w w  w  .  ja  va  2 s  . co  m
 * @throws HttpException
 * @throws IOException
 */
public void testPostFile() throws HttpException, IOException {
    HttpClient client = new HttpClient();

    PostMethod postMethod = new PostMethod(getBaseURI() + "/providers/standard/file");
    byte[] barr = new byte[1000];
    Random r = new Random();
    r.nextBytes(barr);
    postMethod.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(barr), "text/plain"));
    postMethod.addRequestHeader("Accept", "text/plain");
    try {
        client.executeMethod(postMethod);

        assertEquals(200, postMethod.getStatusCode());
        InputStream is = postMethod.getResponseBodyAsStream();

        byte[] receivedBArr = new byte[1000];
        DataInputStream dis = new DataInputStream(is);
        dis.readFully(receivedBArr);

        int checkEOF = dis.read();
        assertEquals(-1, checkEOF);
        for (int c = 0; c < barr.length; ++c) {
            assertEquals(barr[c], receivedBArr[c]);
        }
        assertEquals("text/plain", postMethod.getResponseHeader("Content-Type").getValue());
        assertEquals(1000,
                Integer.valueOf(postMethod.getResponseHeader("Content-Length").getValue()).intValue());
    } finally {
        postMethod.releaseConnection();
    }

    /* TODO : need to test that any temporary files created are deleted */
}

From source file:org.apache.wink.itest.standard.JAXRSFileTest.java

/**
 * Tests receiving an empty byte array.//from  w ww  .  j a  v a2s.  c o  m
 * 
 * @throws HttpException
 * @throws IOException
 */
public void testWithRequestAcceptHeaderWillReturnRequestedContentType() throws HttpException, IOException {
    HttpClient client = new HttpClient();

    PutMethod putMethod = new PutMethod(getBaseURI() + "/providers/standard/file");
    byte[] barr = new byte[1000];
    Random r = new Random();
    r.nextBytes(barr);
    putMethod.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(barr), "any/type"));
    try {
        client.executeMethod(putMethod);
        assertEquals(204, putMethod.getStatusCode());
    } finally {
        putMethod.releaseConnection();
    }

    GetMethod getMethod = new GetMethod(getBaseURI() + "/providers/standard/file");
    getMethod.addRequestHeader("Accept", "mytype/subtype");
    try {
        client.executeMethod(getMethod);
        assertEquals(200, getMethod.getStatusCode());
        InputStream is = getMethod.getResponseBodyAsStream();

        byte[] receivedBArr = new byte[1000];
        DataInputStream dis = new DataInputStream(is);
        dis.readFully(receivedBArr);

        int checkEOF = dis.read();
        assertEquals(-1, checkEOF);
        for (int c = 0; c < barr.length; ++c) {
            assertEquals(barr[c], receivedBArr[c]);
        }
        assertEquals("mytype/subtype", getMethod.getResponseHeader("Content-Type").getValue());
        assertEquals(barr.length,
                Integer.valueOf(getMethod.getResponseHeader("Content-Length").getValue()).intValue());
    } finally {
        getMethod.releaseConnection();
    }
}

From source file:org.apache.wink.itest.standard.JAXRSStreamingOutputTest.java

/**
 * Tests posting to a StreamingOutput and then returning StreamingOutput.
 * /* w  ww .j  av  a 2 s  . c  om*/
 * @throws HttpException
 * @throws IOException
 */
public void testPostStreamingOutput() throws HttpException, IOException {
    HttpClient client = new HttpClient();

    PostMethod postMethod = new PostMethod(getBaseURI() + "/providers/standard/streamingoutput");
    byte[] barr = new byte[100000];
    Random r = new Random();
    r.nextBytes(barr);
    postMethod.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(barr), "text/plain"));
    postMethod.addRequestHeader("Accept", "text/plain");
    try {
        client.executeMethod(postMethod);

        assertEquals(200, postMethod.getStatusCode());
        InputStream is = postMethod.getResponseBodyAsStream();

        byte[] receivedBArr = new byte[barr.length];
        DataInputStream dis = new DataInputStream(is);
        dis.readFully(receivedBArr);

        int checkEOF = dis.read();
        assertEquals(-1, checkEOF);
        for (int c = 0; c < barr.length; ++c) {
            assertEquals(barr[c], receivedBArr[c]);
        }
        assertEquals("text/plain", postMethod.getResponseHeader("Content-Type").getValue());
        Header contentLengthHeader = postMethod.getResponseHeader("Content-Length");
        assertNull(contentLengthHeader == null ? "null" : contentLengthHeader.getValue(), contentLengthHeader);
    } finally {
        postMethod.releaseConnection();
    }
}

From source file:org.apache.wink.itest.standard.JAXRSStreamingOutputTest.java

/**
 * Tests receiving a StreamingOutput with a non-standard content-type.
 * /* ww w  .j  a  v a 2s .  c  o m*/
 * @throws HttpException
 * @throws IOException
 */
public void testWithRequestAcceptHeaderWillReturnRequestedContentType() throws HttpException, IOException {
    HttpClient client = new HttpClient();

    PutMethod putMethod = new PutMethod(getBaseURI() + "/providers/standard/streamingoutput");
    byte[] barr = new byte[100000];
    Random r = new Random();
    r.nextBytes(barr);
    putMethod.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(barr), "any/type"));
    try {
        client.executeMethod(putMethod);
        assertEquals(204, putMethod.getStatusCode());
    } finally {
        putMethod.releaseConnection();
    }

    GetMethod getMethod = new GetMethod(getBaseURI() + "/providers/standard/streamingoutput");
    getMethod.addRequestHeader("Accept", "mytype/subtype");
    try {
        client.executeMethod(getMethod);
        assertEquals(200, getMethod.getStatusCode());
        InputStream is = getMethod.getResponseBodyAsStream();

        byte[] receivedBArr = new byte[barr.length];
        DataInputStream dis = new DataInputStream(is);
        dis.readFully(receivedBArr);

        int checkEOF = dis.read();
        assertEquals(-1, checkEOF);
        for (int c = 0; c < barr.length; ++c) {
            assertEquals(barr[c], receivedBArr[c]);
        }
        assertEquals("mytype/subtype", getMethod.getResponseHeader("Content-Type").getValue());
        Header contentLengthHeader = getMethod.getResponseHeader("Content-Length");
        assertNull(contentLengthHeader == null ? "null" : contentLengthHeader.getValue(), contentLengthHeader);
    } finally {
        getMethod.releaseConnection();
    }
}

From source file:org.apache.wink.itest.standard.JAXRSStreamingOutputTest.java

/**
 * Tests putting and then getting a StreamingOutput.
 * /* w  w w  .j a va 2  s.  c  om*/
 * @throws HttpException
 * @throws IOException
 */
public void testPutStreamngOutput() throws HttpException, IOException {
    HttpClient client = new HttpClient();

    PutMethod putMethod = new PutMethod(getBaseURI() + "/providers/standard/streamingoutput");
    byte[] barr = new byte[100000];
    Random r = new Random();
    r.nextBytes(barr);
    putMethod.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(barr), "bytes/array"));
    try {
        client.executeMethod(putMethod);
        assertEquals(204, putMethod.getStatusCode());
    } finally {
        putMethod.releaseConnection();
    }

    GetMethod getMethod = new GetMethod(getBaseURI() + "/providers/standard/streamingoutput");
    try {
        client.executeMethod(getMethod);
        assertEquals(200, getMethod.getStatusCode());
        InputStream is = getMethod.getResponseBodyAsStream();

        byte[] receivedBArr = new byte[barr.length];
        DataInputStream dis = new DataInputStream(is);
        dis.readFully(receivedBArr);

        int checkEOF = dis.read();
        assertEquals(-1, checkEOF);
        for (int c = 0; c < barr.length; ++c) {
            assertEquals(barr[c], receivedBArr[c]);
        }

        String contentType = (getMethod.getResponseHeader("Content-Type") == null) ? null
                : getMethod.getResponseHeader("Content-Type").getValue();
        assertNotNull(contentType, contentType);
        Header contentLengthHeader = getMethod.getResponseHeader("Content-Length");
        assertNull(contentLengthHeader == null ? "null" : contentLengthHeader.getValue(), contentLengthHeader);
    } finally {
        getMethod.releaseConnection();
    }
}