Example usage for java.io ByteArrayInputStream close

List of usage examples for java.io ByteArrayInputStream close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closing a ByteArrayInputStream has no effect.

Usage

From source file:org.apache.hadoop.mapreduce.task.reduce.TestFetcher.java

@Test
public void testCorruptedIFile() throws Exception {
    final int fetcher = 7;
    Path onDiskMapOutputPath = new Path(name.getMethodName() + "/foo");
    Path shuffledToDisk = OnDiskMapOutput.getTempPath(onDiskMapOutputPath, fetcher);
    fs = FileSystem.getLocal(job).getRaw();
    IFileWrappedMapOutput<Text, Text> odmo = new OnDiskMapOutput<Text, Text>(map1ID, mm, 100L, job, fetcher,
            true, fs, onDiskMapOutputPath);

    String mapData = "MAPDATA12345678901234567890";

    ShuffleHeader header = new ShuffleHeader(map1ID.toString(), 14, 10, 1);
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(bout);
    IFileOutputStream ios = new IFileOutputStream(dos);
    header.write(dos);/*w ww  .ja  v a2s.  com*/

    int headerSize = dos.size();
    try {
        ios.write(mapData.getBytes());
    } finally {
        ios.close();
    }

    int dataSize = bout.size() - headerSize;

    // Ensure that the OnDiskMapOutput shuffler can successfully read the data.
    MapHost host = new MapHost("TestHost", "http://test/url");
    ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
    try {
        // Read past the shuffle header.
        bin.read(new byte[headerSize], 0, headerSize);
        odmo.shuffle(host, bin, dataSize, dataSize, metrics, Reporter.NULL);
    } finally {
        bin.close();
    }

    // Now corrupt the IFile data.
    byte[] corrupted = bout.toByteArray();
    corrupted[headerSize + (dataSize / 2)] = 0x0;

    try {
        bin = new ByteArrayInputStream(corrupted);
        // Read past the shuffle header.
        bin.read(new byte[headerSize], 0, headerSize);
        odmo.shuffle(host, bin, dataSize, dataSize, metrics, Reporter.NULL);
        fail("OnDiskMapOutput.shuffle didn't detect the corrupted map partition file");
    } catch (ChecksumException e) {
        LOG.info("The expected checksum exception was thrown.", e);
    } finally {
        bin.close();
    }

    // Ensure that the shuffled file can be read.
    IFileInputStream iFin = new IFileInputStream(fs.open(shuffledToDisk), dataSize, job);
    try {
        iFin.read(new byte[dataSize], 0, dataSize);
    } finally {
        iFin.close();
    }
}

From source file:org.kuali.coeus.propdev.impl.budget.subaward.PropDevPropDevBudgetSubAwardServiceImpl.java

/**
 * updates the XMl with hashcode for the files
 *///from w  ww.j  a  v  a  2 s  .c o  m

protected BudgetSubAwards updateXML(byte xmlContents[], Map fileMap, BudgetSubAwards budgetSubAwardBean,
        Budget budget) throws Exception {

    javax.xml.parsers.DocumentBuilderFactory domParserFactory = javax.xml.parsers.DocumentBuilderFactory
            .newInstance();
    javax.xml.parsers.DocumentBuilder domParser = domParserFactory.newDocumentBuilder();
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xmlContents);

    org.w3c.dom.Document document = domParser.parse(byteArrayInputStream);
    byteArrayInputStream.close();
    String namespace = null;
    String formName = null;
    if (document != null) {
        Node node;
        Element element = document.getDocumentElement();
        NamedNodeMap map = element.getAttributes();
        String namespaceHolder = element.getNodeName().substring(0, element.getNodeName().indexOf(':'));
        node = map.getNamedItem("xmlns:" + namespaceHolder);
        namespace = node.getNodeValue();
        FormMappingInfo formMappingInfo = formMappingService.getFormInfo(namespace);
        formName = formMappingInfo.getFormName();
        budgetSubAwardBean.setNamespace(namespace);
        budgetSubAwardBean.setFormName(formName);
    }

    String xpathEmptyNodes = "//*[not(node()) and local-name(.) != 'FileLocation' and local-name(.) != 'HashValue']";
    String xpathOtherPers = "//*[local-name(.)='ProjectRole' and local-name(../../.)='OtherPersonnel' and count(../NumberOfPersonnel)=0]";
    removeAllEmptyNodes(document, xpathEmptyNodes, 0);
    removeAllEmptyNodes(document, xpathOtherPers, 1);
    removeAllEmptyNodes(document, xpathEmptyNodes, 0);
    changeDataTypeForNumberOfOtherPersons(document);

    List<String> fedNonFedSubAwardForms = getFedNonFedSubawardForms();
    NodeList budgetYearList = XPathAPI.selectNodeList(document, "//*[local-name(.) = 'BudgetYear']");
    for (int i = 0; i < budgetYearList.getLength(); i++) {
        Node bgtYearNode = budgetYearList.item(i);
        String period = getValue(XPathAPI.selectSingleNode(bgtYearNode, "BudgetPeriod"));
        if (fedNonFedSubAwardForms.contains(namespace)) {
            Element newBudgetYearElement = copyElementToName((Element) bgtYearNode, bgtYearNode.getNodeName());
            bgtYearNode.getParentNode().replaceChild(newBudgetYearElement, bgtYearNode);
        } else {
            Element newBudgetYearElement = copyElementToName((Element) bgtYearNode,
                    bgtYearNode.getNodeName() + period);
            bgtYearNode.getParentNode().replaceChild(newBudgetYearElement, bgtYearNode);
        }
    }

    Node oldroot = document.removeChild(document.getDocumentElement());
    Node newroot = document.appendChild(document.createElement("Forms"));
    newroot.appendChild(oldroot);

    org.w3c.dom.NodeList lstFileName = document.getElementsByTagName("att:FileName");
    org.w3c.dom.NodeList lstFileLocation = document.getElementsByTagName("att:FileLocation");
    org.w3c.dom.NodeList lstMimeType = document.getElementsByTagName("att:MimeType");
    org.w3c.dom.NodeList lstHashValue = document.getElementsByTagName("glob:HashValue");

    org.w3c.dom.Node fileNode, hashNode, mimeTypeNode;
    org.w3c.dom.NamedNodeMap fileNodeMap, hashNodeMap;
    String fileName;
    byte fileBytes[];
    String contentId;
    List attachmentList = new ArrayList();

    for (int index = 0; index < lstFileName.getLength(); index++) {
        fileNode = lstFileName.item(index);

        Node fileNameNode = fileNode.getFirstChild();
        fileName = fileNameNode.getNodeValue();

        fileBytes = (byte[]) fileMap.get(fileName);

        if (fileBytes == null) {
            throw new RuntimeException("FileName mismatch in XML and PDF extracted file");
        }
        String hashVal = grantApplicationHashService.computeAttachmentHash(fileBytes);

        hashNode = lstHashValue.item(index);
        hashNodeMap = hashNode.getAttributes();

        Node temp = document.createTextNode(hashVal);
        hashNode.appendChild(temp);

        hashNode = hashNodeMap.getNamedItem("glob:hashAlgorithm");

        hashNode.setNodeValue(InfastructureConstants.HASH_ALGORITHM);

        fileNode = lstFileLocation.item(index);
        fileNodeMap = fileNode.getAttributes();
        fileNode = fileNodeMap.getNamedItem("att:href");

        contentId = fileNode.getNodeValue();
        String encodedContentId = cleanContentId(contentId);
        fileNode.setNodeValue(encodedContentId);

        mimeTypeNode = lstMimeType.item(0);
        String contentType = mimeTypeNode.getFirstChild().getNodeValue();

        BudgetSubAwardAttachment budgetSubAwardAttachmentBean = new BudgetSubAwardAttachment();
        budgetSubAwardAttachmentBean.setData(fileBytes);
        budgetSubAwardAttachmentBean.setName(encodedContentId);

        budgetSubAwardAttachmentBean.setType(contentType);
        budgetSubAwardAttachmentBean.setBudgetSubAward(budgetSubAwardBean);

        attachmentList.add(budgetSubAwardAttachmentBean);
    }

    budgetSubAwardBean.setBudgetSubAwardAttachments(attachmentList);

    javax.xml.transform.Transformer transformer = javax.xml.transform.TransformerFactory.newInstance()
            .newTransformer();
    transformer.setOutputProperty(javax.xml.transform.OutputKeys.INDENT, "yes");

    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    javax.xml.transform.stream.StreamResult result = new javax.xml.transform.stream.StreamResult(bos);
    javax.xml.transform.dom.DOMSource source = new javax.xml.transform.dom.DOMSource(document);

    transformer.transform(source, result);

    budgetSubAwardBean.setSubAwardXmlFileData(new String(bos.toByteArray()));

    bos.close();

    return budgetSubAwardBean;
}

From source file:MixedRecordEnumerationExample.java

public void commandAction(Command command, Displayable displayable) {
    if (command == exit) {
        destroyApp(true);/*from  www .ja  v a2s.c o m*/
        notifyDestroyed();
    } else if (command == start) {
        try {
            recordstore = RecordStore.openRecordStore("myRecordStore", true);
        } catch (Exception error) {
            alert = new Alert("Error Creating", error.toString(), null, AlertType.WARNING);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert);
        }
        try {
            byte[] outputRecord;
            String outputString[] = { "First Record", "Second Record", "Third Record" };
            int outputInteger[] = { 15, 10, 5 };
            boolean outputBoolean[] = { true, false, true };
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            DataOutputStream outputDataStream = new DataOutputStream(outputStream);
            for (int x = 0; x < 3; x++) {
                outputDataStream.writeUTF(outputString[x]);
                outputDataStream.writeBoolean(outputBoolean[x]);
                outputDataStream.writeInt(outputInteger[x]);
                outputDataStream.flush();
                outputRecord = outputStream.toByteArray();
                recordstore.addRecord(outputRecord, 0, outputRecord.length);
            }
            outputStream.reset();
            outputStream.close();
            outputDataStream.close();
        } catch (Exception error) {
            alert = new Alert("Error Writing", error.toString(), null, AlertType.WARNING);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert);
        }
        try {
            StringBuffer buffer = new StringBuffer();
            byte[] byteInputData = new byte[300];
            ByteArrayInputStream inputStream = new ByteArrayInputStream(byteInputData);
            DataInputStream inputDataStream = new DataInputStream(inputStream);
            recordEnumeration = recordstore.enumerateRecords(null, null, false);
            while (recordEnumeration.hasNextElement()) {
                recordstore.getRecord(recordEnumeration.nextRecordId(), byteInputData, 0);
                buffer.append(inputDataStream.readUTF());
                buffer.append("\n");
                buffer.append(inputDataStream.readBoolean());
                buffer.append("\n");
                buffer.append(inputDataStream.readInt());
                buffer.append("\n");
                alert = new Alert("Reading", buffer.toString(), null, AlertType.WARNING);
                alert.setTimeout(Alert.FOREVER);
                display.setCurrent(alert);
            }
            inputStream.close();
        } catch (Exception error) {
            alert = new Alert("Error Reading", error.toString(), null, AlertType.WARNING);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert);
        }
        try {
            recordstore.closeRecordStore();
        } catch (Exception error) {
            alert = new Alert("Error Closing", error.toString(), null, AlertType.WARNING);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert);
        }
        if (RecordStore.listRecordStores() != null) {
            try {
                RecordStore.deleteRecordStore("myRecordStore");
                recordEnumeration.destroy();
            } catch (Exception error) {
                alert = new Alert("Error Removing", error.toString(), null, AlertType.WARNING);
                alert.setTimeout(Alert.FOREVER);
                display.setCurrent(alert);
            }
        }
    }
}

From source file:com.chenshu.compress.CompressTest.java

private byte[] getDecompressContent(StreamFactory factory, byte[] data) {
    byte[] dest = null;
    ByteArrayInputStream bin = null;
    InputStream in = null;//from w  w w .ja va 2  s .  c  o  m
    ByteArrayOutputStream os = null;
    try {
        bin = new ByteArrayInputStream(data);
        in = factory.getInputStream(bin);
        dest = new byte[src.length];
        // in.read(dest);
        os = new ByteArrayOutputStream(src.length);
        int count = 0;
        while ((count = in.read(dest)) != -1) {
            os.write(dest, 0, count);
        }
        dest = os.toByteArray();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (bin != null) {
            try {
                bin.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (os != null) {
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    assert src.length == dest.length;
    assert Arrays.equals(src, dest);
    assert new String(src, Charset.forName("UTF-8")).equals(new String(dest, Charset.forName("UTF-8")));
    return dest;
}

From source file:de.mpg.escidoc.services.exportmanager.Export.java

/**
 * Base method for getOutput* methods/*from  w  ww  .jav a 2 s  .com*/
 */
private void getOutputBase(String exportFormat, String outputFormat, String archiveFormat, String itemList,
        BufferedOutputStream bos) throws ExportManagerException, IOException {
    //
    Utils.checkCondition(!Utils.checkVal(itemList), "Empty item list");

    ExportFormatTypes exportFormatType = exportFormatType(exportFormat);
    Utils.checkCondition(exportFormatType == null, "Export format is not defined:" + exportFormat);

    boolean generateArchive = false;
    if (Utils.checkVal(archiveFormat)) {
        try {
            ArchiveFormats.valueOf(archiveFormat);
            generateArchive = true;
        } catch (Exception e) {
            throw new ExportManagerException("Archive format: " + archiveFormat + " is not supported");
        }

    }

    byte[] ba = null;
    if (exportFormatType == ExportFormatTypes.STRUCTURED) {
        try {
            ba = getStructuredExportHandler().getOutput(itemList, exportFormat);
        } catch (Exception e) {
            throw new ExportManagerException("Cannot export structured format", e);
        }
        // archived version has been asked
        // for the moment only for CSV format !
    } else if (exportFormatType == ExportFormatTypes.LAYOUT) {

        try {
            ba = getCitationStyleHandler().getOutput(itemList,
                    new ExportFormatVO(FormatType.LAYOUT, exportFormat, outputFormat));
        } catch (Exception e) {
            throw new ExportManagerException("Cannot export citation", e);
        }

    }

    // generate archive
    if (generateArchive) {
        // only CSV for the moment
        if ("CSV".equals(exportFormat))
            generateArchiveBase(exportFormat, archiveFormat, ba, itemList, null, bos);
    } else {
        ByteArrayInputStream bais = new ByteArrayInputStream(ba);
        BufferedInputStream bis = new BufferedInputStream(bais);

        writeFromStreamToStream(bis, bos);

        bis.close();
        bais.close();
    }

}

From source file:edu.unc.lib.dl.fedora.ManagementClient.java

public String writePremisEventsToFedoraObject(PremisEventLogger eventLogger, PID pid) throws FedoraException {
    Document dom = null;/*from  w  w w .  j  a va2  s .  c o m*/

    try {
        pidWriteLock.lock(pid);

        MIMETypedStream mts = this.getAccessClient().getDatastreamDissemination(pid, "MD_EVENTS", null);
        ByteArrayInputStream bais = new ByteArrayInputStream(mts.getStream());
        try {
            dom = new SAXBuilder().build(bais);
            bais.close();
        } catch (JDOMException e) {
            throw new IllegalRepositoryStateException("Cannot parse MD_EVENTS: " + pid, e);
        } catch (IOException e) {
            throw new Error(e);
        }
        eventLogger.appendLogEvents(pid, dom.getRootElement());
        String eventsLoc = this.upload(dom);
        String logTimestamp = this.modifyDatastreamByReference(pid, "MD_EVENTS", false, "adding PREMIS events",
                new ArrayList<String>(), "PREMIS Events", "text/xml", null, null, eventsLoc);
        return logTimestamp;
    } finally {
        pidWriteLock.unlock(pid);
    }
}

From source file:edu.unc.lib.dl.services.DigitalObjectManagerImpl.java

@Override
public void move(List<PID> moving, PID destination, String user, String message) throws IngestException {
    availableCheck();//from   ww  w.  j av a2s . c o  m
    PremisEventLogger logger = new PremisEventLogger(user);

    List<PID> destinationPath = this.getTripleStoreQueryService().lookupRepositoryAncestorPids(destination);
    if (destinationPath == null || destinationPath.size() == 0)
        throw new IngestException("Cannot find the destination folder: " + destinationPath);

    // destination is container
    List<URI> cmtypes = this.getTripleStoreQueryService().lookupContentModels(destination);
    if (!cmtypes.contains(ContentModelHelper.Model.CONTAINER.getURI())) {
        throw new IngestException(
                "The destination is not a folder: " + destinationPath + " " + destination.getPid());
    }

    for (PID pid : moving) {
        if (pid.equals(destination))
            throw new IngestException(
                    "The destination folder and one of the moving objects are the same: " + destination);
        for (PID destPid : destinationPath) {
            if (pid.equals(destPid))
                throw new IngestException(
                        "The destination folder is below one of the moving objects: " + destination);
        }
    }

    // check for duplicate PIDs in incoming list
    Set<PID> noDups = new HashSet<PID>(moving.size());
    noDups.addAll(moving);
    if (noDups.size() < moving.size()) {
        throw new IngestException("The list of moving PIDs contains duplicates");
    } else {
        noDups = null;
    }

    // this is the beginning of the transaction
    DateTime knownGoodTime = new DateTime();
    Throwable thrown = null;
    boolean relationshipsUpdated = false;
    boolean destContentInventoryUpdated = false;
    boolean eventsLoggedOkay = false;
    List<PID> oldParents = new ArrayList<PID>();
    List<PID> reordered = new ArrayList<PID>();
    try {
        // remove pids from old containers, add to new, log
        for (PID pid : moving) {
            PID oldParent = this.removeFromContainer(pid);
            oldParents.add(oldParent);
            this.getManagementClient().addObjectRelationship(destination,
                    ContentModelHelper.Relationship.contains.getURI().toString(), pid);
            logger.logEvent(Type.MIGRATION,
                    "object moved from Container " + oldParent.getPid() + " to " + destination.getPid(), pid);
        }

        // edit Contents XML of new parent container to append/insert
        Document newXML;
        Document oldXML;
        boolean exists = true;
        try {
            MIMETypedStream mts = this.getAccessClient().getDatastreamDissemination(destination, "MD_CONTENTS",
                    null);
            ByteArrayInputStream bais = new ByteArrayInputStream(mts.getStream());
            try {
                oldXML = new SAXBuilder().build(bais);
                bais.close();
            } catch (JDOMException e) {
                throw new IllegalRepositoryStateException("Cannot parse MD_CONTENTS: " + destination);
            } catch (IOException e) {
                throw new Error(e);
            }
        } catch (NotFoundException e) {
            oldXML = new Document();
            Element structMap = new Element("structMap", JDOMNamespaceUtil.METS_NS).addContent(
                    new Element("div", JDOMNamespaceUtil.METS_NS).setAttribute("TYPE", "Container"));
            oldXML.setRootElement(structMap);
            exists = false;
        }
        newXML = ContainerContentsHelper.addChildContentListInCustomOrder(oldXML, destination, moving,
                reordered);
        if (exists) {
            this.getManagementClient().modifyInlineXMLDatastream(destination, "MD_CONTENTS", false,
                    "adding " + moving.size() + " child resources to container", new ArrayList<String>(),
                    "List of Contents", newXML);
        } else {
            this.getManagementClient().addInlineXMLDatastream(destination, "MD_CONTENTS", false,
                    "added " + moving.size() + " child resources to container", new ArrayList<String>(),
                    "List of Contents", false, newXML);
        }
        destContentInventoryUpdated = true;
        // write the log events
        for (PID pid : moving) {
            this.managementClient.writePremisEventsToFedoraObject(logger, pid);
        }

        // send message for the operation
        if (this.getOperationsMessageSender() != null) {
            this.getOperationsMessageSender().sendMoveOperation(user, oldParents, destination, moving,
                    reordered);
        }
    } catch (FedoraException e) {
        thrown = e;
    } catch (RuntimeException e) {
        thrown = e;
    } finally {
        if (thrown != null) {
            // some stuff failed to move, log it
            StringBuffer sb = new StringBuffer();
            sb.append("An error occured during a move operation.\n")
                    .append("all contains relationships updated: ").append(relationshipsUpdated).append("\n")
                    .append("destination (").append(destination.getPid())
                    .append(") content inventory updated: ").append(destContentInventoryUpdated)
                    .append("events logged on moving objects: ").append(eventsLoggedOkay);
            this.dumpRollbackInfo(knownGoodTime, moving, sb.toString());
            throw new IngestException("There was a problem completing the move operation", thrown);
        }
    }
}

From source file:edu.unc.lib.dl.services.BatchIngestTask.java

/**
 * Updates the RELS-EXT contains relationships and the MD_CONTENTS datastream. Call this method last, after all other
 * transactions, it will roll itself back on failure and throw an IngestException.
 *
 * @param submitter//w ww .j  a v  a  2 s  .c o m
 *           the Agent that submitted this change
 * @param placements
 *           the container locations of new pids
 * @param container
 *           the container added to
 * @return the list of PIDs reordered by this change
 * @throws FedoraException
 */
private List<PID> addContainerContents(String submitter, Collection<ContainerPlacement> placements,
        PID container) throws FedoraException {
    List<PID> reordered = new ArrayList<PID>();

    // beginning of container meddling
    // TODO do this in 1 RELS-EXT edit
    for (ContainerPlacement p : placements) {
        if (container.equals(p.parentPID)) {
            this.getManagementClient().addObjectRelationship(container,
                    ContentModelHelper.Relationship.contains.getURI().toString(), p.pid);
        }
    }
    // edit Contents XML of parent container to append/insert
    Document newXML;
    Document oldXML;
    boolean exists = true;
    try {
        MIMETypedStream mts = this.accessClient.getDatastreamDissemination(container, "MD_CONTENTS", null);
        ByteArrayInputStream bais = new ByteArrayInputStream(mts.getStream());
        try {
            oldXML = new SAXBuilder().build(bais);
            bais.close();
        } catch (JDOMException e) {
            throw new IllegalRepositoryStateException("Cannot parse MD_CONTENTS: " + container);
        } catch (IOException e) {
            throw new Error(e);
        }
    } catch (NotFoundException e) {
        oldXML = new Document();
        Element structMap = new Element("structMap", JDOMNamespaceUtil.METS_NS)
                .addContent(new Element("div", JDOMNamespaceUtil.METS_NS).setAttribute("TYPE", "Container"));
        oldXML.setRootElement(structMap);
        exists = false;
    }
    newXML = ContainerContentsHelper.addChildContentAIPInCustomOrder(oldXML, container, placements, reordered);
    if (exists) {
        this.getManagementClient().modifyInlineXMLDatastream(container, "MD_CONTENTS", false,
                "adding child resource to container", new ArrayList<String>(), "List of Contents", newXML);
    } else {
        this.getManagementClient().addInlineXMLDatastream(container, "MD_CONTENTS", false,
                "added child resource to container", new ArrayList<String>(), "List of Contents", false,
                newXML);
    }
    // LOG CHANGES TO THE CONTAINER
    int children = placements.size();
    this.eventLogger.logEvent(PremisEventLogger.Type.INGESTION,
            "added " + children + " child object(s) to this container", container);
    this.getManagementClient().writePremisEventsToFedoraObject(this.eventLogger, container);
    return reordered;
}

From source file:J2MESortMixedRecordDataTypeExample.java

public void commandAction(Command command, Displayable displayable) {
    if (command == exit) {
        destroyApp(true);//from w ww . j a  va 2  s .  co m
        notifyDestroyed();
    } else if (command == start) {
        try {
            recordstore = RecordStore.openRecordStore("myRecordStore", true);
            byte[] outputRecord;
            String outputString[] = { "Mary", "Bob", "Adam" };
            int outputInteger[] = { 15, 10, 5 };
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            DataOutputStream outputDataStream = new DataOutputStream(outputStream);
            for (int x = 0; x < 3; x++) {
                outputDataStream.writeUTF(outputString[x]);
                outputDataStream.writeInt(outputInteger[x]);
                outputDataStream.flush();
                outputRecord = outputStream.toByteArray();
                recordstore.addRecord(outputRecord, 0, outputRecord.length);
                outputStream.reset();
            }
            outputStream.close();
            outputDataStream.close();

            String[] inputString = new String[3];
            byte[] byteInputData = new byte[300];
            ByteArrayInputStream inputStream = new ByteArrayInputStream(byteInputData);
            DataInputStream inputDataStream = new DataInputStream(inputStream);
            StringBuffer buffer = new StringBuffer();
            comparator = new Comparator();
            recordEnumeration = recordstore.enumerateRecords(null, comparator, false);
            while (recordEnumeration.hasNextElement()) {
                recordstore.getRecord(recordEnumeration.nextRecordId(), byteInputData, 0);
                buffer.append(inputDataStream.readUTF());
                buffer.append(inputDataStream.readInt());
                buffer.append("\n");
                inputDataStream.reset();
            }
            alert = new Alert("Reading", buffer.toString(), null, AlertType.WARNING);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert);
            inputDataStream.close();
            inputStream.close();
            recordstore.closeRecordStore();
            if (RecordStore.listRecordStores() != null) {
                RecordStore.deleteRecordStore("myRecordStore");
                comparator.compareClose();
                recordEnumeration.destroy();
            }
        } catch (Exception error) {
            alert = new Alert("Error Removing", error.toString(), null, AlertType.WARNING);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert);
        }
    }
}

From source file:es.sm2.openppm.front.servlets.AbstractGenericServlet.java

/**
 * Send file//from   w ww  .ja  va  2s . c  om
 * 
 * @param req
 * @param resp
 * @param file
 * @param fileName
 * @param contentType
 * @throws ServletException
 * @throws IOException
 */
protected void sendFile(HttpServletRequest req, HttpServletResponse resp, byte[] file, String fileName,
        String contentType) throws ServletException, IOException {

    resp.setContentType(contentType);
    resp.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
    resp.setContentLength(file.length);

    ByteArrayInputStream bais = new ByteArrayInputStream(file);
    OutputStream outs = resp.getOutputStream();

    int start = 0;
    int length = 4 * 1024; // Buffer 4KB
    byte[] buff = new byte[length];

    while (bais.read(buff, start, length) != -1) {
        outs.write(buff, start, length);
    }

    bais.close();
}