Example usage for java.io ByteArrayInputStream read

List of usage examples for java.io ByteArrayInputStream read

Introduction

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

Prototype

public int read(byte b[]) throws IOException 

Source Link

Document

Reads some number of bytes from the input stream and stores them into the buffer array b.

Usage

From source file:org.tinygroup.weblayer.webcontext.parser.fileupload.TinyFileItem.java

public byte[] get() {
    if (isInMemory()) {
        if (cachedContent == null) {
            cachedContent = dbos.getMemoryData();
        }/*from   www .  j av  a 2  s  .  c  om*/
        return cachedContent;
    }
    byte[] fileData = new byte[(int) getSize()];
    ByteArrayInputStream byteInStream = null;
    try {
        byteInStream = new ByteArrayInputStream(dbos.getFileData());
        byteInStream.read(fileData);
    } catch (IOException e) {
        fileData = null;
    } finally {
        if (byteInStream != null) {
            try {
                byteInStream.close();
            } catch (IOException e) {
                // ignore
            }
        }
    }
    return fileData;
}

From source file:imc.jettyserver.servlets.Log4jInit.java

private void convertLogFile(File srcConfig, File destConfig, File logDir) {

    // Step 1 read config file into memory
    String srcDoc = "not initialized";
    try {/*from www . j  a  v  a2s  .  co m*/
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        FileInputStream is = new FileInputStream(srcConfig);

        byte[] buf = new byte[1024];
        int len;
        while ((len = is.read(buf)) > 0) {
            baos.write(buf, 0, len);
        }

        is.close();
        baos.close();
        srcDoc = new String(baos.toByteArray());
    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

    // Step 2 ; substitute Patterns
    String destDoc = srcDoc.replaceAll("loggerdir", logDir.getAbsolutePath().replaceAll("\\\\", "/"));

    // Step 3 ; write back to file
    try {
        ByteArrayInputStream bais = new ByteArrayInputStream(destDoc.getBytes());
        FileOutputStream fos = new FileOutputStream(destConfig);
        byte[] buf = new byte[1024];
        int len;
        while ((len = bais.read(buf)) > 0) {
            fos.write(buf, 0, len);
        }
        fos.close();
        bais.close();
    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:org.carlspring.strongbox.artifact.generator.NugetPackageGenerator.java

private void createRels(String id, ZipOutputStream zos) throws IOException {
    ZipEntry ze = new ZipEntry("_rels/.rels");
    zos.putNextEntry(ze);//www.j a v  a 2 s  .c  om

    ByteArrayInputStream is = new ByteArrayInputStream(String.format(RELS_CONTENT, id).getBytes());
    byte[] buffer = new byte[4096];
    int len;
    while ((len = is.read(buffer)) > 0) {
        zos.write(buffer, 0, len);
    }

    is.close();
    zos.closeEntry();
}

From source file:org.carlspring.strongbox.artifact.generator.NugetPackageGenerator.java

private void createMetadata(String id, String version, ZipOutputStream zos) throws IOException {
    ZipEntry ze = new ZipEntry("package/services/metadata/core-properties/metadata.psmdcp");
    zos.putNextEntry(ze);/*from  w ww  . j  a v  a2 s.  co m*/

    ByteArrayInputStream is = new ByteArrayInputStream(String.format(PSMDCP_CONTENT, id, version).getBytes());
    byte[] buffer = new byte[4096];
    int len;
    while ((len = is.read(buffer)) > 0) {
        zos.write(buffer, 0, len);
    }

    is.close();
    zos.closeEntry();
}

From source file:org.exist.http.servlets.Log4jInit.java

private void convertLogFile(File srcConfig, File destConfig, File logDir) {

    // Step 1 read config file into memory
    String srcDoc = "not initialized";
    try {//w  w w. j a  va 2  s . c om
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final FileInputStream is = new FileInputStream(srcConfig);

        final byte[] buf = new byte[1024];
        int len;
        while ((len = is.read(buf)) > 0) {
            baos.write(buf, 0, len);
        }

        is.close();
        baos.close();
        srcDoc = new String(baos.toByteArray());
    } catch (final FileNotFoundException ex) {
        ex.printStackTrace();
    } catch (final IOException ex) {
        ex.printStackTrace();
    }

    // Step 2 ; substitute Patterns
    final String destDoc = srcDoc.replaceAll("loggerdir", logDir.getAbsolutePath().replaceAll("\\\\", "/"));

    // Step 3 ; write back to file
    try {
        final ByteArrayInputStream bais = new ByteArrayInputStream(destDoc.getBytes());
        final FileOutputStream fos = new FileOutputStream(destConfig);
        final byte[] buf = new byte[1024];
        int len;
        while ((len = bais.read(buf)) > 0) {
            fos.write(buf, 0, len);
        }
        fos.close();
        bais.close();
    } catch (final FileNotFoundException ex) {
        ex.printStackTrace();
    } catch (final IOException ex) {
        ex.printStackTrace();
    }
}

From source file:org.geoserver.wfs.response.SpatiaLiteOutputFormatTest.java

/**
 * Test not null content.//ww w  .j a  v a 2s.c o m
 * @throws Exception
 */
public void testContentNotNull() throws Exception {
    MockHttpServletResponse resp = getAsServletResponse(
            "wfs?request=GetFeature&typeName=Points&outputFormat=spatialite");
    ByteArrayInputStream sResponse = getBinaryInputStream(resp);
    int dataLengh = sResponse.available();
    boolean contentNull = true;
    byte[] data = new byte[dataLengh];
    sResponse.read(data);
    for (byte aByte : data)
        if (aByte != 0) {
            contentNull = false;
            break;
        }
    assertFalse(contentNull);
}

From source file:org.i3xx.step.uno.impl.ScriptCacheImpl.java

/**
 * Computes a MD5-Digest to verify integrity
 * /*from   www . j a  v  a2  s.co  m*/
 * @throws NoSuchAlgorithmException 
 * @throws IOException 
 */
public void computeDigest() throws NoSuchAlgorithmException, IOException {
    MessageDigest md = MessageDigest.getInstance("MD5");

    for (int i = 0; i < buffer.length; i++) {
        ByteArrayInputStream in = new ByteArrayInputStream(buffer[i]);
        byte[] buf = new byte[1024];
        int c = 0;

        while ((c = in.read(buf)) > -1)
            md.update(buf, 0, c);
    } //for

    byte[] dig = md.digest();
    digest = new String(Hex.encodeHex(dig));
}

From source file:com.moez.QKSMS.mmssms.Transaction.java

private static Uri createPartImage(Context context, String id, byte[] imageBytes, String mimeType)
        throws Exception {
    ContentValues mmsPartValue = new ContentValues();
    mmsPartValue.put("mid", id);
    mmsPartValue.put("ct", mimeType);
    mmsPartValue.put("cid", "<" + System.currentTimeMillis() + ">");
    Uri partUri = Uri.parse("content://mms/" + id + "/part");
    Uri res = context.getContentResolver().insert(partUri, mmsPartValue);

    // Add data to part
    OutputStream os = context.getContentResolver().openOutputStream(res);
    ByteArrayInputStream is = new ByteArrayInputStream(imageBytes);
    byte[] buffer = new byte[256];

    for (int len = 0; (len = is.read(buffer)) != -1;) {
        os.write(buffer, 0, len);//from www  . j  a  v a2s  . c om
    }

    os.close();
    is.close();

    return res;
}

From source file:org.wapama.web.server.UUIDBasedRepositoryServlet.java

/**
 * This method populates the response with the contents of the model.
 * It expects two parameters to be passed via the request, uuid and profile.
 *//*from w w  w.j a v  a2s.  c  o m*/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String uuid = req.getParameter("uuid");
    if (uuid == null) {
        throw new ServletException("uuid parameter required");
    }
    IDiagramProfile profile = getProfile(req, req.getParameter("profile"));
    ByteArrayInputStream input = new ByteArrayInputStream(
            _repository.load(req, uuid, profile.getSerializedModelExtension()));
    byte[] buffer = new byte[4096];
    int read;

    while ((read = input.read(buffer)) != -1) {
        resp.getOutputStream().write(buffer, 0, read);
    }
}

From source file:org.eclipse.swt.snippets.Snippet319.java

public void go() {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 319");
    shell.setBounds(10, 10, 600, 200);//  ww  w  . ja v  a  2  s .c om

    /* Create SWT controls and add drag source */
    final Label swtLabel = new Label(shell, SWT.BORDER);
    swtLabel.setBounds(10, 10, 580, 50);
    swtLabel.setText("SWT drag source");
    DragSource dragSource = new DragSource(swtLabel, DND.DROP_COPY);
    dragSource.setTransfer(new MyTypeTransfer());
    dragSource.addDragListener(new DragSourceAdapter() {
        @Override
        public void dragSetData(DragSourceEvent event) {
            MyType object = new MyType();
            object.name = "content dragged from SWT";
            object.time = System.currentTimeMillis();
            event.data = object;
        }
    });

    /* Create AWT/Swing controls */
    Composite embeddedComposite = new Composite(shell, SWT.EMBEDDED);
    embeddedComposite.setBounds(10, 100, 580, 50);
    embeddedComposite.setLayout(new FillLayout());
    Frame frame = SWT_AWT.new_Frame(embeddedComposite);
    final JLabel jLabel = new JLabel("AWT/Swing drop target");
    frame.add(jLabel);

    /* Register the custom data flavour */
    final DataFlavor flavor = new DataFlavor(MIME_TYPE, "MyType custom flavor");
    /*
     * Note that according to jre/lib/flavormap.properties, the preferred way to
     * augment the default system flavor map is to specify the AWT.DnD.flavorMapFileURL
     * property in an awt.properties file.
     *
     * This snippet uses the alternate approach below in order to provide a simple
     * stand-alone snippet that demonstrates the functionality.  This implementation
     * works well, but if the instanceof check below fails for some reason when used
     * in a different context then the drop will not be accepted.
     */
    FlavorMap map = SystemFlavorMap.getDefaultFlavorMap();
    if (map instanceof SystemFlavorMap) {
        SystemFlavorMap systemMap = (SystemFlavorMap) map;
        systemMap.addFlavorForUnencodedNative(MIME_TYPE, flavor);
    }

    /* add drop target */
    DropTargetListener dropTargetListener = new DropTargetAdapter() {
        @Override
        public void drop(DropTargetDropEvent dropTargetDropEvent) {
            try {
                dropTargetDropEvent.acceptDrop(DnDConstants.ACTION_COPY);
                ByteArrayInputStream inStream = (ByteArrayInputStream) dropTargetDropEvent.getTransferable()
                        .getTransferData(flavor);
                int available = inStream.available();
                byte[] bytes = new byte[available];
                inStream.read(bytes);
                MyType object = restoreFromByteArray(bytes);
                String string = object.name + ": " + new Date(object.time).toString();
                jLabel.setText(string);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    new DropTarget(jLabel, dropTargetListener);

    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}