Example usage for java.io DataInputStream DataInputStream

List of usage examples for java.io DataInputStream DataInputStream

Introduction

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

Prototype

public DataInputStream(InputStream in) 

Source Link

Document

Creates a DataInputStream that uses the specified underlying InputStream.

Usage

From source file:de.hybris.platform.jobs.RemovedItemPKProcessorTest.java

@Test
public void testSkipofDeleted() {
    final PK one = PK.createFixedUUIDPK(102, 1);
    final PK two = PK.createFixedUUIDPK(102, 2);
    final PK three = PK.createFixedUUIDPK(102, 3);

    final MediaModel mediaPk = new MediaModel();
    final DataInputStream dis = new DataInputStream(buildUpStream(one, two, three));

    Mockito.when(model.getItemPKs()).thenReturn(mediaPk);
    Mockito.when(mediaService.getStreamFromMedia(mediaPk)).thenReturn(dis);
    Mockito.when(model.getItemsDeleted()).thenReturn(Integer.valueOf(10));

    iterator.init(model);//from w  ww  . j  ava2s . c  om

    Assert.assertFalse("All iterations should be skipped ", iterator.hasNext());
}

From source file:IOUtilities.java

/**
 * Returns a DataInputStream object based on the given InputStream.
 * If the given InputStream is already an instance of DataInputStream,
 * the same (given) InputStream is casted to DataInputStream and returned,
 * otherwise, a new wrapper DataInputStream is created and returned.
 *///from w w  w  .j  av a2 s . c o  m

public static DataInputStream maybeCreateDataInputStream(InputStream in) {
    if (in instanceof DataInputStream)
        return (DataInputStream) in;
    else
        return new DataInputStream(in);
}

From source file:org.freeeed.ep.web.controller.FileDownloadController.java

@Override
public ModelAndView execute() {
    String result = (String) valueStack.get("file");
    String resultFile = workDir + File.separator + result;

    File toDownload = new File(resultFile);

    try {/*from  ww w .java 2 s . c  om*/
        int length = 0;
        ServletOutputStream outStream = response.getOutputStream();
        String mimetype = "application/octet-stream";

        response.setContentType(mimetype);
        response.setContentLength((int) toDownload.length());
        String fileName = toDownload.getName();

        // sets HTTP header
        response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");

        byte[] byteBuffer = new byte[1024];
        DataInputStream in = new DataInputStream(new FileInputStream(toDownload));

        // reads the file's bytes and writes them to the response stream
        while ((in != null) && ((length = in.read(byteBuffer)) != -1)) {
            outStream.write(byteBuffer, 0, length);
        }

        in.close();
        outStream.close();
    } catch (Exception e) {
        log.error("Problem sending cotent", e);
        valueStack.put("error", true);
    }

    return new ModelAndView(WebConstants.DOWNLOAD_RESULT);
}

From source file:dualcontrol.CryptoHandler.java

public void handle(DualControlKeyStoreSession dualControl, Socket socket) throws Exception {
    try {//from   w ww  .  j  a  v  a  2  s.  c o  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:de.citec.sc.corpus.CorpusLoader.java

private List<String> readFileAsList(File file) {
    List<String> content = new ArrayList<>();
    try {//from ww w .  j  av  a  2  s  . c  o m
        FileInputStream fstream = new FileInputStream(file);
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;

        while ((strLine = br.readLine()) != null) {
            content.add(strLine);
        }
        in.close();
    } catch (Exception e) {
        System.err.println("Error reading the file: " + file.getPath() + "\n" + e.getMessage());
    }

    return content;
}

From source file:ImageLoaderMIDlet.java

public Image loadImage(String url) throws IOException {
    HttpConnection hpc = null;/* ww  w. j  ava2 s  .c om*/
    DataInputStream dis = null;
    try {
        hpc = (HttpConnection) Connector.open(url);
        int length = (int) hpc.getLength();
        byte[] data = new byte[length];
        dis = new DataInputStream(hpc.openInputStream());
        dis.readFully(data);
        return Image.createImage(data, 0, data.length);
    } finally {
        if (hpc != null)
            hpc.close();
        if (dis != null)
            dis.close();
    }
}

From source file:com.github.ambry.commons.BlobId.java

/**
 * Re-constructs existing blobId by deserializing from BlobId "string"
 *
 * @param id of Blob as output by BlobId.getID()
 * @param clusterMap of the cluster that the blob id belongs to
 * @throws IOException/*  w ww .  j a  v a  2  s.c om*/
 */
public BlobId(String id, ClusterMap clusterMap) throws IOException {
    this(new DataInputStream(new ByteBufferInputStream(ByteBuffer.wrap(Base64.decodeBase64(id)))), clusterMap);
}

From source file:cit360.sandbox.BackEndMenu.java

public static final void smtpExample() {
    // declaration section:
    // smtpClient: our client socket
    // os: output stream
    // is: input stream
    Socket smtpSocket = null;//from w  w w .  j  av a  2s.  co  m
    DataOutputStream os = null;
    DataInputStream is = null;
    // Initialization section:
    // Try to open a socket on port 25
    // Try to open input and output streams
    try {
        smtpSocket = new Socket("localhost", 25);
        os = new DataOutputStream(smtpSocket.getOutputStream());
        is = new DataInputStream(smtpSocket.getInputStream());
    } catch (UnknownHostException e) {
        System.err.println("Did not recognize server: localhost");
    } catch (IOException e) {
        System.err.println("Was not able to open I/O connection to: localhost");
    }
    // If everything has been initialized then we want to write some data
    // to the socket we have opened a connection to on port 25
    if (smtpSocket != null && os != null && is != null) {
        try {

            os.writeBytes("HELO\n");
            os.writeBytes("MAIL From: david.banks0889@gmail.com\n");
            os.writeBytes("RCPT To: david.banks0889@gmail.com\n");
            os.writeBytes("DATA\n");
            os.writeBytes("From: david.banks0889@gmail.com\n");
            os.writeBytes("Subject: TEST\n");
            os.writeBytes("Hi there\n"); // message body
            os.writeBytes("\n.\n");
            os.writeBytes("QUIT");
            // keep on reading from/to the socket till we receive the "Ok" from SMTP,
            // once we received that then we want to break.
            String responseLine;
            while ((responseLine = is.readLine()) != null) {
                System.out.println("Server: " + responseLine);
                if (responseLine.contains("Ok")) {
                    break;
                }
            }
            // clean up:
            // close the output stream
            // close the input stream
            // close the socket
            os.close();
            is.close();
            smtpSocket.close();
        } catch (UnknownHostException e) {
            System.err.println("Trying to connect to unknown host: " + e);
        } catch (IOException e) {
            System.err.println("IOException:  " + e);
        }
    }
}

From source file:com.koda.common.lcm.Tool.java

public static Object fromFile(String fileName) throws IOException {
    FileInputStream fis = new FileInputStream(fileName);
    DataInputStream dis = new DataInputStream(fis);
    String s = dis.readUTF();/*from   w w  w . j  ava 2s.com*/
    dis.close();
    return undoObject(s);
}

From source file:com.fabernovel.alertevoirie.data.CategoryProvider.java

@Override
public boolean onCreate() {
    // load json data
    DataInputStream in = new DataInputStream(getContext().getResources().openRawResource(R.raw.categories));
    try {/*w  w  w  . jav  a2s. com*/
        byte[] buffer = new byte[in.available()];
        in.read(buffer);
        categories = (JSONObject) new JSONTokener(new String(buffer)).nextValue();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        Log.e("Alerte Voirie", "JSON error", e);
    }
    return true;
}