Example usage for java.io DataInputStream close

List of usage examples for java.io DataInputStream close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes this input stream and releases any system resources associated with the stream.

Usage

From source file:org.dcm4che2.tool.jpg2dcm.Jpg2Dcm.java

@SuppressWarnings("rawtypes")
public void convert(File jpgFile, File dcmFile, String patientName, String patientID, String studyuid,
        String seriesuid, int n) throws IOException {
    jpgHeaderLen = 0;/* w ww . jav  a 2s  .  c  o m*/
    jpgLen = (int) jpgFile.length();
    DataInputStream jpgInput = new DataInputStream(new BufferedInputStream(new FileInputStream(jpgFile)));
    try {
        DicomObject attrs = new BasicDicomObject();
        attrs.putString(Tag.SpecificCharacterSet, VR.CS, charset);
        for (Enumeration en = cfg.propertyNames(); en.hasMoreElements();) {
            String key = (String) en.nextElement();
            int[] tagPath = Tag.toTagPath(key);
            int last = tagPath.length - 1;
            VR vr = attrs.vrOf(tagPath[last]);
            if (vr == VR.SQ) {
                attrs.putSequence(tagPath);
            } else {
                attrs.putString(tagPath, vr, cfg.getProperty(key));
            }
        }
        if (noAPPn || missingRowsColumnsSamplesPMI(attrs)) {
            readHeader(attrs, jpgInput);
        }
        ensureUS(attrs, Tag.BitsAllocated, 8);
        ensureUS(attrs, Tag.BitsStored, attrs.getInt(Tag.BitsAllocated));
        ensureUS(attrs, Tag.HighBit, attrs.getInt(Tag.BitsStored) - 1);
        ensureUS(attrs, Tag.PixelRepresentation, 0);
        //ensureUID(attrs, Tag.StudyInstanceUID);
        //ensureUID(attrs, Tag.SeriesInstanceUID);
        ensureUID(attrs, Tag.SOPInstanceUID);

        Date now = new Date();
        attrs.putDate(Tag.InstanceCreationDate, VR.DA, now);
        attrs.putDate(Tag.InstanceCreationTime, VR.TM, now);
        attrs.initFileMetaInformation(transferSyntax);

        attrs.putDate(Tag.StudyDate, VR.DA, now);
        attrs.putDate(Tag.StudyTime, VR.TM, now);
        //attrs.putString( Tag.PatientName, VR.PN, patientName );
        attrs.putBytes(Tag.PatientName, VR.PN, patientName.getBytes(this.charset));
        attrs.putString(Tag.PatientID, VR.LO, patientID);

        attrs.putString(Tag.StudyInstanceUID, VR.UI, studyuid);
        attrs.putString(Tag.SeriesInstanceUID, VR.UI, seriesuid);
        attrs.putString(Tag.StudyID, VR.SH, "99");
        attrs.putString(Tag.SeriesNumber, VR.IS, "1");
        attrs.putString(Tag.InstanceNumber, VR.IS, String.valueOf(n));

        FileOutputStream fos = new FileOutputStream(dcmFile);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        DicomOutputStream dos = new DicomOutputStream(bos);
        try {
            dos.writeDicomFile(attrs);
            dos.writeHeader(Tag.PixelData, VR.OB, -1);
            dos.writeHeader(Tag.Item, null, 0);
            dos.writeHeader(Tag.Item, null, (jpgLen + 1) & ~1);
            dos.write(buffer, 0, jpgHeaderLen);
            int r;
            while ((r = jpgInput.read(buffer)) > 0) {
                dos.write(buffer, 0, r);
            }
            if ((jpgLen & 1) != 0) {
                dos.write(0);
            }
            dos.writeHeader(Tag.SequenceDelimitationItem, null, 0);
        } finally {
            dos.close();
        }
    } finally {
        jpgInput.close();
    }
}

From source file:AnalysisModule.DataAnalysis.java

protected void realAnalysis(ArrayList<Scenario> lstScenario) throws Exception {
    String delim = "[,]";
    DecimalFormat df = new DecimalFormat("00");

    FileInputStream tracesFIS;//from   w  w  w  .j a v a  2s.c o m
    DataInputStream tracesDIS;
    BufferedReader tracesBR;
    String tracesStr;

    for (Scenario scenario : lstScenario) {
        for (Topology topology : scenario.lstTopology) {
            Long[][] trafficMatrix = new Long[topology.getNumberOfSwitches()][topology.getNumberOfSwitches()];
            for (int i = 0; i < topology.getNumberOfSwitches(); i++) {
                for (int j = 0; j < topology.getNumberOfSwitches(); j++) {
                    trafficMatrix[i][j] = 0L;
                }
            }
            topology.setTrafficMatrix(trafficMatrix);
        }
    }

    for (Scenario scenario : lstScenario) {
        File diretorio = new File(scenario.traceFile);
        File files[] = diretorio.listFiles();
        if (files == null) {
            files = new File[1];
            files[0] = diretorio;
        } else {
            Arrays.sort(files);
        }

        for (File file : files) {

            if (file.isFile()) {

                //System.out.println("Trace #" + df.format(i));
                tracesFIS = new FileInputStream(file);
                tracesDIS = new DataInputStream(tracesFIS);
                tracesBR = new BufferedReader(new InputStreamReader(tracesDIS));

                tracesStr = tracesBR.readLine();

                while (tracesStr != null) {

                    String[] packetTokens = tracesStr.split(delim);

                    long time = parseLong(packetTokens[0]);
                    long srcIP = parseLong(packetTokens[1]);
                    long dstIP = parseLong(packetTokens[2]);

                    if (time - 21600000000L > scenario.startTime && time - 21600000000L < scenario.endTime) {

                        for (Topology topology : scenario.lstTopology) {

                            //Ingress Switch
                            int ingressSW = (int) ((topology.getBitmask() & srcIP) >> (32
                                    - topology.getNumberOfBitsForSwId()));

                            //Egress Switch
                            int egressSW = (int) ((topology.getBitmask() & dstIP) >> (32
                                    - topology.getNumberOfBitsForSwId()));

                            doCalculateMatrixElem(ingressSW, egressSW, topology);
                        }
                    }
                    tracesStr = tracesBR.readLine();
                }
                tracesBR.close();
                tracesDIS.close();
                tracesFIS.close();
            }
        }
        //        return lstMatrizRealanalysis;
    }

}

From source file:be.ibridge.kettle.trans.step.sortrows.SortRows.java

private Row getBuffer() {
    int i, f;/*from  www.j  av a 2  s  .  co  m*/
    int smallest;
    Row r1, r2;
    Row retval;

    // Open all files at once and read one row from each file...
    if (data.files.size() > 0 && (data.dis.size() == 0 || data.fis.size() == 0)) {
        logBasic("Opening " + data.files.size() + " tmp-files...");

        try {
            for (f = 0; f < data.files.size() && !isStopped(); f++) {
                FileObject fileObject = (FileObject) data.files.get(f);
                String filename = KettleVFS.getFilename(fileObject);
                if (log.isDetailed())
                    logDetailed("Opening tmp-file: [" + filename + "]");
                InputStream fi = fileObject.getContent().getInputStream();
                DataInputStream di;
                data.fis.add(fi);
                if (meta.getCompress()) {
                    GZIPInputStream gzfi = new GZIPInputStream(new BufferedInputStream(fi));
                    di = new DataInputStream(gzfi);
                    data.gzis.add(gzfi);
                } else {
                    di = new DataInputStream(fi);
                }
                data.dis.add(di);

                // How long is the buffer?
                int buffersize = di.readInt();

                if (log.isDetailed())
                    logDetailed("[" + filename + "] expecting " + buffersize + " rows...");

                if (buffersize > 0) {
                    // Read a row from each temp-file
                    Row metadata = (Row) data.rowMeta.get(f);
                    data.rowbuffer.add(new Row(di, metadata.size(), metadata)); // new row
                }
            }
        } catch (Exception e) {
            logError("Error reading back tmp-files : " + e.toString());
            e.printStackTrace();
        }
    }

    if (data.files.size() == 0) {
        if (data.buffer.size() > 0) {
            retval = (Row) data.buffer.get(0);
            data.buffer.remove(0);
        } else {
            retval = null;
        }
    } else {
        if (data.rowbuffer.size() == 0) {
            retval = null;
        } else {
            // We now have "filenr" rows waiting: which one is the smallest?
            //
            for (i = 0; i < data.rowbuffer.size() && !isStopped(); i++) {
                Row b = (Row) data.rowbuffer.get(i);
                if (log.isRowLevel())
                    logRowlevel("--BR#" + i + ": " + b.toString());
            }
            //

            smallest = 0;
            r1 = (Row) data.rowbuffer.get(smallest);
            for (f = 1; f < data.rowbuffer.size() && !isStopped(); f++) {
                r2 = (Row) data.rowbuffer.get(f);

                if (r2 != null && r2.compare(r1, data.fieldnrs, meta.getAscending()) < 0) {
                    smallest = f;
                    r1 = (Row) data.rowbuffer.get(smallest);
                }
            }
            retval = r1;

            data.rowbuffer.remove(smallest);
            if (log.isRowLevel())
                logRowlevel("Smallest row selected on [" + smallest + "] : " + retval);

            // now get another Row for position smallest

            FileObject file = (FileObject) data.files.get(smallest);
            DataInputStream di = (DataInputStream) data.dis.get(smallest);
            InputStream fi = (InputStream) data.fis.get(smallest);
            GZIPInputStream gzfi = (meta.getCompress()) ? (GZIPInputStream) data.gzis.get(smallest) : null;

            try {
                Row metadata = (Row) data.rowMeta.get(smallest);
                data.rowbuffer.add(smallest, new Row(di, metadata.size(), metadata));
            } catch (KettleFileException fe) // empty file or EOF mostly
            {
                try {
                    di.close();
                    fi.close();
                    if (gzfi != null)
                        gzfi.close();
                    file.delete();
                } catch (IOException e) {
                    logError("Unable to close/delete file #" + smallest + " --> " + file.toString());
                    setErrors(1);
                    stopAll();
                    return null;
                }

                data.files.remove(smallest);
                data.dis.remove(smallest);
                data.fis.remove(smallest);
                if (gzfi != null)
                    data.gzis.remove(smallest);
                data.rowMeta.remove(smallest);
            }
        }
    }
    return retval;
}

From source file:eu.cloud4soa.soa.jaxrs.test.ApplicationDeploymentTest.java

public void deployApplication() throws FileNotFoundException {
    //        final String BASE_URI = "http://localhost:8080/frontend-dashboard-0.0.1-SNAPSHOT/services/REST/ApplicationDeploymentRS/deployApplication";
    final String BASE_URI = "http://localhost:8080/cloud4soa.soa/services/REST/ApplicationDeploymentRS/deployApplication";

    WebClient client = WebClient.create(BASE_URI);
    client.type("multipart/mixed").accept(MediaType.TEXT_PLAIN);
    //        ContentDisposition cd = new ContentDisposition("attachment;filename=image.jpg");
    //        Attachment att = new Attachment("root", imageInputStream, cd);

    //        ApplicationDeployment applicationDeploymentRS = (ApplicationDeployment) JAXRSClientFactory.create("http://localhost:8080/books", ApplicationDeployment.class);
    //        WebClient.client(applicationDeploymentRS).type("multipart/mixed");

    URL fileURL = this.getClass().getClassLoader().getResource("SimpleWar.war");
    if (fileURL == null)
        throw new FileNotFoundException("SimpleWar.war");

    ByteArrayOutputStream bas = new ByteArrayOutputStream();

    File file = new File(fileURL.getPath());
    file.length();//from  w  w w.  j a v  a2 s.c o  m
    FileInputStream fis = new FileInputStream(file);
    BufferedInputStream bis = new BufferedInputStream(fis);
    DataInputStream dis = new DataInputStream(bis);

    //Calculate digest from InputStream
    //        InputStream tempIs = new FileInputStream(file);
    String tempFileDigest = null;
    try {
        FileInputStream tempFis = new FileInputStream(file);
        tempFileDigest = DigestUtils.sha256Hex(tempFis);
    } catch (IOException ex) {
        Logger.getLogger(ApplicationDeploymentTest.class.getName()).log(Level.SEVERE, null, ex);
    }

    //        JSONObject applicationInstanceJsonObj=new JSONObject();
    //        try {
    //            applicationInstanceJsonObj.put("acronym","App1");
    //            applicationInstanceJsonObj.put("applicationcode","001");
    //            applicationInstanceJsonObj.put("programminglanguage","Java");
    //        } catch (JSONException ex) {
    //            ex.printStackTrace();
    //        }
    //
    //        System.out.println(applicationInstanceJsonObj);

    JSONObject applicationInstanceJsonObj = new JSONObject();
    try {
        applicationInstanceJsonObj.put("acronym", "SimpleWar");
        applicationInstanceJsonObj.put("archiveFileName", "app");
        applicationInstanceJsonObj.put("archiveExtensionName", ".war");
        applicationInstanceJsonObj.put("digest", tempFileDigest);
        applicationInstanceJsonObj.put("sizeQuantity", file.length());
        applicationInstanceJsonObj.put("version", "2");
        //            applicationInstanceJsonObj.put("","");
    } catch (JSONException ex) {
        Logger.getLogger(ApplicationDeploymentTest.class.getName()).log(Level.SEVERE, null, ex);
    }

    System.out.println(applicationInstanceJsonObj);

    JSONObject paaSInstanceJsonObj = new JSONObject();
    try {
        //            paaSInstanceJsonObj.put("providerTitle","CloudBees");
        paaSInstanceJsonObj.put("providerTitle", "Beanstalk");
    } catch (JSONException ex) {
        Logger.getLogger(ApplicationDeploymentTest.class.getName()).log(Level.SEVERE, null, ex);
    }

    System.out.println(paaSInstanceJsonObj);

    ProviderFactory.getSharedInstance().registerUserProvider(new JSONProvider());
    // POST the request
    //        Response response = applicationDeploymentRS.deployApplication(dis, applicationInstanceJsonObj, paaSInstanceJsonObj);
    Attachment att1 = new Attachment("applicationInstance", "application/json",
            applicationInstanceJsonObj.toString());
    Attachment att2 = new Attachment("paaSInstance", "application/json", paaSInstanceJsonObj.toString());
    Attachment att3 = new Attachment("applicationArchive", "application/octet-stream", dis);
    List<Attachment> listAttch = new LinkedList<Attachment>();
    listAttch.add(att1);
    listAttch.add(att2);
    listAttch.add(att3);
    Response response = client.post(new MultipartBody(listAttch));
    if (Response.Status.fromStatusCode(response.getStatus()) == Response.Status.ACCEPTED)
        try {
            System.out.println(
                    "Response Status : " + IOUtils.readStringFromStream((InputStream) response.getEntity()));
        } catch (IOException ex) {
            Logger.getLogger(ApplicationDeploymentTest.class.getName()).log(Level.SEVERE, null, ex);
        }

    try {
        fis.close();
        bis.close();
        dis.close();
    } catch (IOException ex) {
        Logger.getLogger(ApplicationDeploymentTest.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:it.classhidra.core.controller.bean.java

public boolean initJsonPart(HttpServletRequest request) throws bsControllerException {
    boolean isJson = false;
    HashMap parameters = new HashMap();
    DataInputStream in = null;
    try {/*from   w ww  .j  a  va2  s.  c o m*/
        in = new DataInputStream(request.getInputStream());
        int formDataLength = request.getContentLength();

        byte dataBytes[] = new byte[formDataLength];
        int bytesRead = 0;
        int totalBytesRead = 0;
        while (totalBytesRead < formDataLength && totalBytesRead > -1) {
            bytesRead = in.read(dataBytes, totalBytesRead, formDataLength);
            totalBytesRead += bytesRead;
        }

        String json = new String(dataBytes, 0, dataBytes.length).trim();
        if (json.charAt(0) == '{' && json.charAt(json.length() - 1) == '}')
            isJson = true;
        if (isJson) {
            if (json.charAt(0) == '{' && json.length() > 0)
                json = json.substring(1, json.length());
            if (json.charAt(json.length() - 1) == '}' && json.length() > 0)
                json = json.substring(0, json.length() - 1);
            StringTokenizer st = new StringTokenizer(json, ",");
            while (st.hasMoreTokens()) {
                String pair = st.nextToken();
                StringTokenizer st1 = new StringTokenizer(pair, ":");
                String key = null;
                String value = null;
                if (st1.hasMoreTokens())
                    key = st1.nextToken();
                if (st1.hasMoreTokens())
                    value = st1.nextToken();
                if (key != null && value != null) {
                    key = key.trim();
                    if (key.charAt(0) == '"' && key.length() > 0)
                        key = key.substring(1, key.length());
                    if (key.charAt(key.length() - 1) == '"' && key.length() > 0)
                        key = key.substring(0, key.length() - 1);
                    value = value.trim();
                    if (value.charAt(0) == '"' && value.length() > 0)
                        value = value.substring(1, value.length());
                    if (value.charAt(value.length() - 1) == '"' && value.length() > 0)
                        value = value.substring(0, value.length() - 1);
                    parameters.put(key, value);
                }
            }
        }

    } catch (Exception e) {

    } finally {
        try {
            in.close();
        } catch (Exception ex) {
        }
    }

    if (isJson)
        initPartFromMap(parameters);

    return isJson;
}

From source file:com.max2idea.android.limbo.main.LimboActivity.java

public static String sendHttpGet(String url) {
    HttpConnection hcon = null;/*from  ww  w .  j  a v a 2s . c  o m*/
    DataInputStream dis = null;
    java.net.URL URL = null;
    try {
        URL = new java.net.URL(url);
    } catch (MalformedURLException ex) {
        Logger.getLogger(LimboActivity.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:tachyon.master.EditLog.java

/**
 * Load one edit log.// w ww.  j  a  v a2  s.c  o m
 * 
 * @param info The Master Info
 * @param path The path of the edit log
 * @throws IOException
 */
public static void loadSingleLog(MasterInfo info, String path) throws IOException {
    UnderFileSystem ufs = UnderFileSystem.get(path, info.getTachyonConf());

    DataInputStream is = new DataInputStream(ufs.open(path));
    JsonParser parser = JsonObject.createObjectMapper().getFactory().createParser(is);

    while (true) {
        EditLogOperation op;
        try {
            op = parser.readValueAs(EditLogOperation.class);
            LOG.debug("Read operation: {}", op);
        } catch (IOException e) {
            // Unfortunately brittle, but Jackson rethrows EOF with this message.
            if (e.getMessage().contains("end-of-input")) {
                break;
            } else {
                throw e;
            }
        }

        sCurrentTId = op.mTransId;
        try {
            switch (op.mType) {
            case ADD_BLOCK: {
                info.opAddBlock(op.getInt("fileId"), op.getInt("blockIndex"), op.getLong("blockLength"),
                        op.getLong("opTimeMs"));
                break;
            }
            case ADD_CHECKPOINT: {
                info._addCheckpoint(-1, op.getInt("fileId"), op.getLong("length"),
                        new TachyonURI(op.getString("path")), op.getLong("opTimeMs"));
                break;
            }
            case CREATE_FILE: {
                info._createFile(op.getBoolean("recursive"), new TachyonURI(op.getString("path")),
                        op.getBoolean("directory"), op.getLong("blockSizeByte"), op.getLong("creationTimeMs"));
                break;
            }
            case COMPLETE_FILE: {
                info._completeFile(op.get("fileId", Integer.class), op.getLong("opTimeMs"));
                break;
            }
            case SET_PINNED: {
                info._setPinned(op.getInt("fileId"), op.getBoolean("pinned"), op.getLong("opTimeMs"));
                break;
            }
            case RENAME: {
                info._rename(op.getInt("fileId"), new TachyonURI(op.getString("dstPath")),
                        op.getLong("opTimeMs"));
                break;
            }
            case DELETE: {
                info._delete(op.getInt("fileId"), op.getBoolean("recursive"), op.getLong("opTimeMs"));
                break;
            }
            case CREATE_RAW_TABLE: {
                info._createRawTable(op.getInt("tableId"), op.getInt("columns"), op.getByteBuffer("metadata"));
                break;
            }
            case UPDATE_RAW_TABLE_METADATA: {
                info.updateRawTableMetadata(op.getInt("tableId"), op.getByteBuffer("metadata"));
                break;
            }
            case CREATE_DEPENDENCY: {
                info._createDependency(op.get("parents", new TypeReference<List<Integer>>() {
                }), op.get("children", new TypeReference<List<Integer>>() {
                }), op.getString("commandPrefix"), op.getByteBufferList("data"), op.getString("comment"),
                        op.getString("framework"), op.getString("frameworkVersion"),
                        op.get("dependencyType", DependencyType.class), op.getInt("dependencyId"),
                        op.getLong("creationTimeMs"));
                break;
            }
            default:
                throw new IOException("Invalid op type " + op);
            }
        } catch (SuspectedFileSizeException e) {
            throw new IOException(e);
        } catch (BlockInfoException e) {
            throw new IOException(e);
        } catch (FileDoesNotExistException e) {
            throw new IOException(e);
        } catch (FileAlreadyExistException e) {
            throw new IOException(e);
        } catch (InvalidPathException e) {
            throw new IOException(e);
        } catch (TachyonException e) {
            throw new IOException(e);
        } catch (TableDoesNotExistException e) {
            throw new IOException(e);
        }
    }

    is.close();
    ufs.close();
}

From source file:hu.sztaki.lpds.pgportal.portlets.file.LFCFileStoragePortlet.java

@Override
public void serveResource(ResourceRequest request, ResourceResponse response)
        throws PortletException, IOException {
    //  PortletSession ps=request.getPortletSession();

    //Download Process

    String filelist = "";
    if (request.getParameter("selected_file") != null
            && !request.getParameter("selected_file").equals(new String(""))) {
        filelist = request.getParameter("selected_file");
    }/*from ww  w .java  2  s .c  o m*/

    String userId = getUserName(request);
    String lfcHost = RemoteFileManagerSingleton.getInstance().getCurrentBrowser(userId).getLfcHost();
    String gridName = RemoteFileManagerSingleton.getInstance().getCurrentBrowser(userId).getGridName();
    String certPath = PropertyLoader.getInstance().getProperty("portal.prefix.dir") + "users" + "/" + userId;
    /*event.getTextFieldBean("newName").setValue("");
    event.getTextFieldBean("dirName").setValue("");
         *
         */
    if (!userCertCheckMG(userId, gridName)) {
        /*   response.setRenderParameter("message", "No valid certificate for " + gridName);
           viewGridResources(request, getSelectedGridForUser(userId), getSelectedVOForUser(userId));
                
           response.setRenderParameter("showErrorButton", "0");
           //response.setRenderParameter("showFileBrowser", "0");
           response.setRenderParameter("userID", userId);
                
           //setNextState(req, "file/File.jsp");
           return;
         *
         */
    }

    if (gridName.equals("All") || lfcHost == null || lfcHost.equals("")) {
        /*   response.setRenderParameter("message", "Select a VO and an LFCHOST");
           viewGridResources(request, getSelectedGridForUser(userId), getSelectedVOForUser(userId));
           response.setRenderParameter("showFileBrowser", "0");
           response.setRenderParameter("userID", userId);
                
           //setNextState(req, "file/File.jsp");
           return;
         *
         */
    }
    String path = "/grid/" + gridName;
    String fileName = "";

    if (((filelist == null) || filelist.equals(new String("")))) { // A file is not selected from the list

        //fillFileList(getCurrentBrowser(userId).getLastSelectedItem().getItems(), fileList, "");
        for (int i = 0; i < RemoteFileManagerSingleton.getInstance().getCurrentBrowser(userId)
                .getSelectedItems().size(); i++) {
            path = path + "/" + RemoteFileManagerSingleton.getInstance().getCurrentBrowser(userId)
                    .getSelectedItems().get(i);
        }
        /*   response.setRenderParameter("path", path);
           response.setRenderParameter("showFileBrowser", "1");
           viewGridResources(request, getSelectedGridForUser(userId), getSelectedVOForUser(userId));
           response.setRenderParameter("message", "A file is not selected");
           response.setRenderParameter("userID", userId);
                
           //setNextState(req, "file/File.jsp");
         *
                
           return;
         *
         */

        response.getWriter().write(
                "<br><br><table align=\"center\" width=\"100%\" bgcolor=\"lightblue\" ><tr align=\"center\"><td>");
        response.getWriter().write(
                "<strong>ERROR! <br><br>No file selected!<br><br> Please use \"back\" button in your browser to go back to the previous page.</strong>");
        response.getWriter().write("</td></tr></table>");

        return;

    }
    // extract the name and '+' or' -', indicating whether the item is a directory or a file
    String[] names = filelist.split("@");

    fileName = names[names.length - 1];

    /*  NO DIRECTORY DOWNLOAD */

    if (names[0].charAt(0) != '-' || names[0].trim().length() == 0) { // Selected item is not a file

        response.getWriter().write(
                "<br><br><table align=\"center\" width=\"100%\" bgcolor=\"lightblue\" ><tr align=\"center\"><td>");
        response.getWriter().write(
                "<strong>ERROR! <br><br>Selected entry is not a file!<br><br> Please use \"back\" button in your browser to go back to the previous page.</strong>");
        response.getWriter().write("</td></tr></table>");

        return;
        //response.getWriter().write("0");

        /*//fillFileList(getCurrentBrowser(userId).getLastSelectedItem().getItems(), fileList, "");
        for(int i=0;i<RemoteFileManagerSingleton.getInstance().getCurrentBrowser(userId).getSelectedItems().size();i++) {
           path = path + "/" + RemoteFileManagerSingleton.getInstance().getCurrentBrowser(userId).getSelectedItems().get(i);
        }
        response.setRenderParameter("path", path);
        response.setRenderParameter("showFileBrowser", "1");
        viewGridResources(request, getSelectedGridForUser(userId), getSelectedVOForUser(userId));
        response.setRenderParameter("userID", userId);
                
        //setNextState(req, "file/File.jsp");
        return;
                 *
                 */
    }
    String tempdir = System.getProperty("java.io.tmpdir") + "/users";
    String argument = "";
    // find the path of the selected item
    for (int i = 0; i < RemoteFileManagerSingleton.getInstance().getCurrentBrowser(userId).getSelectedItems()
            .size(); i++)
        argument = argument + " "
                + RemoteFileManagerSingleton.getInstance().getCurrentBrowser(userId).getSelectedItems().get(i);
    Process p;
    for (int i = 0; i < RemoteFileManagerSingleton.getInstance().getCurrentBrowser(userId).getSelectedItems()
            .size(); i++) {
        path = path + "/"
                + RemoteFileManagerSingleton.getInstance().getCurrentBrowser(userId).getSelectedItems().get(i);
    }
    ArrayList fList = new ArrayList();
    try {

        // save the file to the portal server
        fList.add(fileName);
        VOInfoRequester inforeq = new VOInfoRequester();
        String bdii = inforeq.getInfos(gridName).getBdii();

        p = Runtime.getRuntime()
                .exec("/bin/bash " + scripts_path + "fileDownload.sh " + scripts_path + " " + bdii + " "
                        + userId + " " + tempdir + " " + certPath + " " + lfcHost + "  " + " " + gridName + " "
                        + path + " " + fileName);
        p.waitFor();
        ///response.setRenderParameter("isDirectory", "0");
    } catch (Exception ex) {
        response.setContentType("text/plain");

        response.getWriter().write(
                "<br><br><table align=\"center\" width=\"100%\" bgcolor=\"lightblue\" ><tr align=\"center\"><td>");
        response.getWriter().write(
                "<strong>ERROR! <br><br>File cannot be downloaded!<br><br> Please use \"back\" button in your browser to go back to the previous page.</strong>");
        response.getWriter().write("</td></tr></table>");
        return;
    }

    RemoteFileManagerSingleton.getInstance().putFileList(userId, fList);
    /*response.setRenderParameter("path", path);
    response.setRenderParameter("fileNames", "true");
    response.setRenderParameter("fileName", fileName);
            
    response.setRenderParameter("userID", userId);
    response.setRenderParameter("nextJSP","/WEB-INF/jsp/file/fileDownload.jsp");
    */

    // sending file back to the browser
    File f = new File(tempdir + "/" + userId + "/fileDownloadDir/" + fileName);
    if (!f.exists()) {
        response.setContentType("text/plain");
        response.getWriter().write(
                "<br><br><table align=\"center\" width=\"100%\" bgcolor=\"lightblue\" ><tr align=\"center\"><td>");
        response.getWriter().write(
                "<strong>ERROR! <br><br>File cannot be downloaded!<br><br> Please use \"back\" button in your browser to go back to the previous page.</strong>");
        response.getWriter().write("</td></tr></table>");
        return;
    }
    int length = 0;

    //
    //  Set the response and go!
    //
    //
    response.setContentType("application/x-download");

    response.addProperty("Content-Disposition", "inline;  filename=" + fileName);

    response.setContentLength((int) f.length());
    //response.setHeader( "Content-Disposition", "attachment; filename=\"" + original_filename + "\"" );

    //
    //  Stream to the requester.
    //
    byte[] bbuf = new byte[10 * 1024];

    DataInputStream in = new DataInputStream(new FileInputStream(f));

    while ((in != null) && ((length = in.read(bbuf)) != -1)) {
        response.getPortletOutputStream().write(bbuf, 0, length);
    }

    in.close();
    response.getPortletOutputStream().flush();
    response.getPortletOutputStream().close();

    //END of Download Process

    // FileUploadProgressListener listener=(FileUploadProgressListener)
    //((PortletFileUpload)ps.getAttribute("upload",ps.APPLICATION_SCOPE)).getProgressListener();
    //      response.getWriter().write(lisener.getFileuploadstatus());

}

From source file:com.splout.db.dnode.HttpFileExchanger.java

@Override
public void handle(HttpExchange exchange) throws IOException {
    DataInputStream iS = null;
    FileOutputStream writer = null;
    File dest = null;/*from   w  ww  . j  ava2  s  .  co  m*/

    String tablespace = null;
    Integer partition = null;
    Long version = null;

    try {
        iS = new DataInputStream(new GZIPInputStream(exchange.getRequestBody()));
        String fileName = exchange.getRequestHeaders().getFirst("filename");
        tablespace = exchange.getRequestHeaders().getFirst("tablespace");
        partition = Integer.valueOf(exchange.getRequestHeaders().getFirst("partition"));
        version = Long.valueOf(exchange.getRequestHeaders().getFirst("version"));

        dest = new File(
                new File(tempDir,
                        DNodeHandler.getLocalStoragePartitionRelativePath(tablespace, partition, version)),
                fileName);

        // just in case, avoid copying the same file concurrently
        // (but we also shouldn't avoid this in other levels of the app)
        synchronized (currentTransfersMonitor) {
            if (currentTransfers.containsKey(dest.toString())) {
                throw new IOException("Incoming file already being transferred - " + dest);
            }
            currentTransfers.put(dest.toString(), new Object());
        }

        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        if (dest.exists()) {
            dest.delete();
        }

        writer = new FileOutputStream(dest);
        byte[] buffer = new byte[config.getInt(FetcherProperties.DOWNLOAD_BUFFER)];

        Checksum checkSum = new CRC32();

        // 1- Read file size
        long fileSize = iS.readLong();
        log.debug("Going to read file [" + fileName + "] of size: " + fileSize);
        // 2- Read file contents
        long readSoFar = 0;

        do {
            long missingBytes = fileSize - readSoFar;
            int bytesToRead = (int) Math.min(missingBytes, buffer.length);
            int read = iS.read(buffer, 0, bytesToRead);
            checkSum.update(buffer, 0, read);
            writer.write(buffer, 0, read);
            readSoFar += read;
            callback.onProgress(tablespace, partition, version, dest, fileSize, readSoFar);
        } while (readSoFar < fileSize);

        // 3- Read CRC
        long expectedCrc = iS.readLong();
        if (expectedCrc == checkSum.getValue()) {
            log.info("File [" + dest.getAbsolutePath() + "] received -> Checksum -- " + checkSum.getValue()
                    + " matches expected CRC [OK]");
            callback.onFileReceived(tablespace, partition, version, dest);
        } else {
            log.error("File received [" + dest.getAbsolutePath() + "] -> Checksum -- " + checkSum.getValue()
                    + " doesn't match expected CRC: " + expectedCrc);
            callback.onBadCRC(tablespace, partition, version, dest);
            dest.delete();
        }
    } catch (Throwable t) {
        log.error(t);
        callback.onError(t, tablespace, partition, version, dest);
        if (dest != null && dest.exists()
                && !t.getMessage().contains("Incoming file already being transferred")) {
            dest.delete();
        }
    } finally {
        if (writer != null) {
            writer.close();
        }
        if (iS != null) {
            iS.close();
        }
        if (dest != null) {
            currentTransfers.remove(dest.toString());
        }
    }
}

From source file:com.panet.imeta.trans.steps.sort.SortRows.java

private Object[] getBuffer() throws KettleValueException {
    Object[] retval;/*from  w  w  w  .j a  v  a 2  s.  co  m*/

    // Open all files at once and read one row from each file...
    if (data.files.size() > 0 && (data.dis.size() == 0 || data.fis.size() == 0)) {
        if (log.isBasic())
            logBasic("Opening " + data.files.size() + " tmp-files...");

        try {
            for (int f = 0; f < data.files.size() && !isStopped(); f++) {
                FileObject fileObject = (FileObject) data.files.get(f);
                String filename = KettleVFS.getFilename(fileObject);
                if (log.isDetailed())
                    logDetailed("Opening tmp-file: [" + filename + "]");
                InputStream fi = KettleVFS.getInputStream(fileObject);
                DataInputStream di;
                data.fis.add(fi);
                if (data.compressFiles) {
                    GZIPInputStream gzfi = new GZIPInputStream(new BufferedInputStream(fi));
                    di = new DataInputStream(gzfi);
                    data.gzis.add(gzfi);
                } else {
                    di = new DataInputStream(new BufferedInputStream(fi, 50000));
                }
                data.dis.add(di);

                // How long is the buffer?
                int buffersize = data.bufferSizes.get(f);

                if (log.isDetailed())
                    logDetailed("[" + filename + "] expecting " + buffersize + " rows...");

                if (buffersize > 0) {
                    Object[] row = (Object[]) data.outputRowMeta.readData(di);
                    data.rowbuffer.add(row); // new row from input stream
                    data.tempRows.add(new RowTempFile(row, f));
                }
            }

            // Sort the data row buffer
            Collections.sort(data.tempRows, data.comparator);
        } catch (Exception e) {
            logError("Error reading back tmp-files : " + e.toString());
            logError(Const.getStackTracker(e));
        }
    }

    if (data.files.size() == 0) {
        if (data.getBufferIndex < data.buffer.size()) {
            retval = (Object[]) data.buffer.get(data.getBufferIndex);
            data.getBufferIndex++;
        } else {
            retval = null;
        }
    } else {
        if (data.rowbuffer.size() == 0) {
            retval = null;
        } else {
            // We now have "filenr" rows waiting: which one is the smallest?
            //
            if (log.isRowLevel()) {
                for (int i = 0; i < data.rowbuffer.size() && !isStopped(); i++) {
                    Object[] b = (Object[]) data.rowbuffer.get(i);
                    logRowlevel("--BR#" + i + ": " + data.outputRowMeta.getString(b));
                }
            }

            RowTempFile rowTempFile = data.tempRows.remove(0);
            retval = rowTempFile.row;
            int smallest = rowTempFile.fileNumber;

            // now get another Row for position smallest

            FileObject file = (FileObject) data.files.get(smallest);
            DataInputStream di = (DataInputStream) data.dis.get(smallest);
            InputStream fi = (InputStream) data.fis.get(smallest);
            GZIPInputStream gzfi = (data.compressFiles) ? (GZIPInputStream) data.gzis.get(smallest) : null;

            try {
                Object[] row2 = (Object[]) data.outputRowMeta.readData(di);
                RowTempFile extra = new RowTempFile(row2, smallest);

                int index = Collections.binarySearch(data.tempRows, extra, data.comparator);
                if (index < 0) {
                    data.tempRows.add(index * (-1) - 1, extra);
                } else {
                    data.tempRows.add(index, extra);
                }
            } catch (KettleFileException fe) // empty file or EOF mostly
            {
                try {
                    di.close();
                    fi.close();
                    if (gzfi != null)
                        gzfi.close();
                    file.delete();
                } catch (IOException e) {
                    logError("Unable to close/delete file #" + smallest + " --> " + file.toString());
                    setErrors(1);
                    stopAll();
                    return null;
                }

                data.files.remove(smallest);
                data.dis.remove(smallest);
                data.fis.remove(smallest);

                if (gzfi != null)
                    data.gzis.remove(smallest);

                // Also update all file numbers in in data.tempRows if they are larger than smallest.
                //
                for (RowTempFile rtf : data.tempRows) {
                    if (rtf.fileNumber > smallest)
                        rtf.fileNumber--;
                }

            } catch (SocketTimeoutException e) {
                throw new KettleValueException(e); // should never happen on local files
            }
        }
    }
    return retval;
}