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:dualcontrol.CryptoHandler.java

public void handle(DualControlKeyStoreSession dualControl, Socket socket) throws Exception {
    try {/*from  w w w. j  a  va  2  s .co m*/
        this.dualControl = dualControl;
        DataInputStream dis = new DataInputStream(socket.getInputStream());
        int length = dis.readShort();
        byte[] bytes = new byte[length];
        dis.readFully(bytes);
        String data = new String(bytes);
        String[] fields = data.split(":");
        String mode = fields[0];
        String alias = fields[1];
        this.dos = new DataOutputStream(socket.getOutputStream());
        if (mode.equals("GETKEY")) {
            if (enableGetKey) {
                SecretKey key = dualControl.loadKey(alias);
                dos.writeUTF(key.getAlgorithm());
                write(key.getEncoded());
            }
        } else {
            cipher(mode, alias, fields[2], fields[3], fields[4]);
        }
    } finally {
        dos.close();
    }
}

From source file:org.spoutcraft.client.packet.PacketEntityInformation.java

public void readData(DataInputStream input) throws IOException {
    int size = input.readInt();
    if (size > 0) {
        data = new byte[size];
        input.readFully(data);
    }//  w  w w . j  ava2 s  .  c  o  m
    compressed = input.readBoolean();
}

From source file:org.apache.synapse.util.TemporaryDataTest.java

public void testMarkReset() throws IOException {
    byte[] sourceData1 = new byte[2000];
    byte[] sourceData2 = new byte[2000];
    random.nextBytes(sourceData1);//from   ww w.  j a  v a  2s . com
    random.nextBytes(sourceData2);
    TemporaryData tmp = new TemporaryData(16, 512, "test", ".dat");
    OutputStream out = tmp.getOutputStream();
    out.write(sourceData1);
    out.write(sourceData2);
    out.close();
    DataInputStream in = new DataInputStream(tmp.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));
}

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

/**
 * Tests posting to an InputStream//from  ww w . j a v  a  2  s  .c om
 * 
 * @throws HttpException
 * @throws IOException
 */
public void testPostInputStream() throws HttpException, IOException {
    HttpClient client = new HttpClient();

    PostMethod postMethod = new PostMethod(getBaseURI() + "/providers/standard/inputstream");
    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.anarres.lzo.LzopStreamTest.java

public void testAlgorithm(LzoAlgorithm algorithm, byte[] orig) throws IOException {
    for (long flags : FLAGS) {
        try {//from   w  w  w  .  ja  va 2 s  .  co m
            LzoCompressor compressor = LzoLibrary.getInstance().newCompressor(algorithm, null);
            LOG.info("Compressing " + orig.length + " bytes using " + algorithm);

            // LOG.info("Original:   " + Arrays.toString(orig));

            ByteArrayOutputStream os = new ByteArrayOutputStream();
            LzopOutputStream cs = new LzopOutputStream(os, compressor, 256, flags);
            cs.write(orig);
            cs.close();

            // LOG.info("Compressed: OK.");

            FileUtils.writeByteArrayToFile(new File("temp.lzo"), os.toByteArray());

            // LzoDecompressor decompressor = LzoLibrary.getInstance().newDecompressor(algorithm, null);

            ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
            LzopInputStream us = new LzopInputStream(is);
            DataInputStream ds = new DataInputStream(us);
            byte[] uncompressed = new byte[orig.length];
            ds.readFully(uncompressed);

            // LOG.info("Output:     OK.");
            // LOG.info("Output:     " + Arrays.toString(uncompressed));

            assertArrayEquals(orig, uncompressed);
        } finally {
            System.out.flush();
            System.err.flush();
        }
    }
}

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

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

    PutMethod putMethod = new PutMethod(getBaseURI() + "/providers/standard/inputstream");
    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/inputstream");
    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.getspout.spout.packet.PacketCustomBlockChunkOverride.java

public void readData(DataInputStream input) throws IOException {
    chunkX = input.readInt();/*from w  w  w.  j  a  v  a  2 s .co  m*/
    chunkZ = input.readInt();
    int size = input.readInt();
    data = new byte[size];
    input.readFully(data);
}

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

/**
 * Tests putting and then getting a byte array.
 * /*  w  w w  .  j  ava2s  .c  o m*/
 * @throws HttpException
 * @throws IOException
 */
public void testPutInputStream() throws HttpException, IOException {
    HttpClient client = new HttpClient();

    PutMethod putMethod = new PutMethod(getBaseURI() + "/providers/standard/inputstream");
    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/inputstream");
    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();
    }
}

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

public void readData(DataInputStream input) throws IOException {
    int size = input.readInt();
    compressed = input.readBoolean();/*  ww w . j  a v a  2s .com*/
    if (size > 0) {
        data = new byte[size];
        input.readFully(data);
    }
}

From source file:com.athena.chameleon.engine.threadpool.task.BaseTask.java

/**
 * <pre>/*ww  w  .  j a  v a  2s.  c om*/
 * ?? ? ?  .
 * </pre>
 * @param file
 * @return
 */
protected String fileToString(String file) {
    String result = null;

    try {
        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);
    } catch (IOException e) {
        throw new RuntimeException("IO problem in fileToString", e);
    }

    return result;
}