Example usage for java.lang OutOfMemoryError getMessage

List of usage examples for java.lang OutOfMemoryError getMessage

Introduction

In this page you can find the example usage for java.lang OutOfMemoryError getMessage.

Prototype

public String getMessage() 

Source Link

Document

Returns the detail message string of this throwable.

Usage

From source file:im.vector.util.BugReporter.java

/**
 * Send a bug report./*from  ww w .j  a v a2s.  c o  m*/
 *
 * @param context         the application context
 * @param withDevicesLogs true to include the device logs
 * @param withCrashLogs   true to include the crash logs
 */
private static void sendBugReport(final Context context, final boolean withDevicesLogs,
        final boolean withCrashLogs, final String bugDescription, final IMXBugReportListener listener) {
    new AsyncTask<Void, Integer, String>() {
        @Override
        protected String doInBackground(Void... voids) {
            File bugReportFile = new File(context.getApplicationContext().getFilesDir(), "bug_report");

            if (bugReportFile.exists()) {
                bugReportFile.delete();
            }

            String serverError = null;
            FileWriter fileWriter = null;

            try {
                fileWriter = new FileWriter(bugReportFile);
                JsonWriter jsonWriter = new JsonWriter(fileWriter);
                jsonWriter.beginObject();

                // android bug report
                jsonWriter.name("user_agent").value("Android");

                // logs list
                jsonWriter.name("logs");
                jsonWriter.beginArray();

                // the logs are optional
                if (withDevicesLogs) {
                    List<File> files = org.matrix.androidsdk.util.Log.addLogFiles(new ArrayList<File>());
                    for (File f : files) {
                        if (!mIsCancelled) {
                            jsonWriter.beginObject();
                            jsonWriter.name("lines").value(convertStreamToString(f));
                            jsonWriter.endObject();
                            jsonWriter.flush();
                        }
                    }
                }

                if (!mIsCancelled && (withCrashLogs || withDevicesLogs)) {
                    jsonWriter.beginObject();
                    jsonWriter.name("lines").value(getLogCatError());
                    jsonWriter.endObject();
                    jsonWriter.flush();
                }

                jsonWriter.endArray();

                jsonWriter.name("text").value(bugDescription);

                String version = "";

                if (null != Matrix.getInstance(context).getDefaultSession()) {
                    version += "User : " + Matrix.getInstance(context).getDefaultSession().getMyUserId() + "\n";
                }

                version += "Phone : " + Build.MODEL.trim() + " (" + Build.VERSION.INCREMENTAL + " "
                        + Build.VERSION.RELEASE + " " + Build.VERSION.CODENAME + ")\n";
                version += "Vector version: " + Matrix.getInstance(context).getVersion(true) + "\n";
                version += "SDK version:  " + Matrix.getInstance(context).getDefaultSession().getVersion(true)
                        + "\n";
                version += "Olm version:  "
                        + Matrix.getInstance(context).getDefaultSession().getCryptoVersion(context, true)
                        + "\n";

                jsonWriter.name("version").value(version);

                jsonWriter.endObject();
                jsonWriter.close();

            } catch (Exception e) {
                Log.e(LOG_TAG, "doInBackground ; failed to collect the bug report data " + e.getMessage());
                serverError = e.getLocalizedMessage();
            } catch (OutOfMemoryError oom) {
                Log.e(LOG_TAG, "doInBackground ; failed to collect the bug report data " + oom.getMessage());
                serverError = oom.getMessage();

                if (TextUtils.isEmpty(serverError)) {
                    serverError = "Out of memory";
                }
            }

            try {
                if (null != fileWriter) {
                    fileWriter.close();
                }
            } catch (Exception e) {
                Log.e(LOG_TAG, "doInBackground ; failed to close fileWriter " + e.getMessage());
            }

            if (TextUtils.isEmpty(serverError) && !mIsCancelled) {

                // the screenshot is defined here
                // File screenFile = new File(VectorApp.mLogsDirectoryFile, "screenshot.jpg");
                InputStream inputStream = null;
                HttpURLConnection conn = null;
                try {
                    inputStream = new FileInputStream(bugReportFile);
                    final int dataLen = inputStream.available();

                    // should never happen
                    if (0 == dataLen) {
                        return "No data";
                    }

                    URL url = new URL(context.getResources().getString(R.string.bug_report_url));
                    conn = (HttpURLConnection) url.openConnection();
                    conn.setDoInput(true);
                    conn.setDoOutput(true);
                    conn.setUseCaches(false);
                    conn.setRequestMethod("POST");
                    conn.setRequestProperty("Content-Type", "application/json");
                    conn.setRequestProperty("Content-Length", Integer.toString(dataLen));
                    // avoid caching data before really sending them.
                    conn.setFixedLengthStreamingMode(inputStream.available());

                    conn.connect();

                    DataOutputStream dos = new DataOutputStream(conn.getOutputStream());

                    byte[] buffer = new byte[8192];

                    // read file and write it into form...
                    int bytesRead;
                    int totalWritten = 0;

                    while (!mIsCancelled && (bytesRead = inputStream.read(buffer, 0, buffer.length)) > 0) {
                        dos.write(buffer, 0, bytesRead);
                        totalWritten += bytesRead;
                        publishProgress(totalWritten * 100 / dataLen);
                    }

                    dos.flush();
                    dos.close();

                    int mResponseCode;

                    try {
                        // Read the SERVER RESPONSE
                        mResponseCode = conn.getResponseCode();
                    } catch (EOFException eofEx) {
                        mResponseCode = HttpURLConnection.HTTP_INTERNAL_ERROR;
                    }

                    // if the upload failed, try to retrieve the reason
                    if (mResponseCode != HttpURLConnection.HTTP_OK) {
                        serverError = null;
                        InputStream is = conn.getErrorStream();

                        if (null != is) {
                            int ch;
                            StringBuilder b = new StringBuilder();
                            while ((ch = is.read()) != -1) {
                                b.append((char) ch);
                            }
                            serverError = b.toString();
                            is.close();

                            // check if the error message
                            try {
                                JSONObject responseJSON = new JSONObject(serverError);
                                serverError = responseJSON.getString("error");
                            } catch (JSONException e) {
                                Log.e(LOG_TAG, "doInBackground ; Json conversion failed " + e.getMessage());
                            }

                            // should never happen
                            if (null == serverError) {
                                serverError = "Failed with error " + mResponseCode;
                            }

                            is.close();
                        }
                    }
                } catch (Exception e) {
                    Log.e(LOG_TAG,
                            "doInBackground ; failed with error " + e.getClass() + " - " + e.getMessage());
                    serverError = e.getLocalizedMessage();

                    if (TextUtils.isEmpty(serverError)) {
                        serverError = "Failed to upload";
                    }
                } catch (OutOfMemoryError oom) {
                    Log.e(LOG_TAG, "doInBackground ; failed to send the bug report " + oom.getMessage());
                    serverError = oom.getLocalizedMessage();

                    if (TextUtils.isEmpty(serverError)) {
                        serverError = "Out ouf memory";
                    }

                } finally {
                    try {
                        if (null != conn) {
                            conn.disconnect();
                        }
                    } catch (Exception e2) {
                        Log.e(LOG_TAG, "doInBackground : conn.disconnect() failed " + e2.getMessage());
                    }
                }

                if (null != inputStream) {
                    try {
                        inputStream.close();
                    } catch (Exception e) {
                        Log.e(LOG_TAG, "doInBackground ; failed to close the inputStream " + e.getMessage());
                    }
                }
            }
            return serverError;
        }

        @Override
        protected void onProgressUpdate(Integer... progress) {
            super.onProgressUpdate(progress);

            if (null != listener) {
                try {
                    listener.onProgress((null == progress) ? 0 : progress[0]);
                } catch (Exception e) {
                    Log.e(LOG_TAG, "## onProgress() : failed " + e.getMessage());
                }
            }
        }

        @Override
        protected void onPostExecute(String reason) {
            if (null != listener) {
                try {
                    if (mIsCancelled) {
                        listener.onUploadCancelled();
                    } else if (null == reason) {
                        listener.onUploadSucceed();
                    } else {
                        listener.onUploadFailed(reason);
                    }
                } catch (Exception e) {
                    Log.e(LOG_TAG, "## onPostExecute() : failed " + e.getMessage());
                }
            }
        }
    }.execute();
}

From source file:com.bah.lucene.BlockCacheDirectoryFactoryV1.java

public BlockCacheDirectoryFactoryV1(Configuration configuration, long totalNumberOfBytes) {
    // setup block cache
    // 134,217,728 is the slab size, therefore there are 16,384 blocks
    // in a slab when using a block size of 8,192
    int numberOfBlocksPerSlab = 16384;
    int blockSize = BlockDirectory.BLOCK_SIZE;
    int slabCount = configuration.getInt(BLUR_SHARD_BLOCKCACHE_SLAB_COUNT, -1);
    slabCount = getSlabCount(slabCount, numberOfBlocksPerSlab, blockSize, totalNumberOfBytes);
    Cache cache;//from  w w  w  . j a  v a 2 s  . c  om
    if (slabCount >= 1) {
        BlockCache blockCache;
        boolean directAllocation = configuration.getBoolean(BLUR_SHARD_BLOCKCACHE_DIRECT_MEMORY_ALLOCATION,
                true);

        int slabSize = numberOfBlocksPerSlab * blockSize;
        LOG.info(MessageFormat.format(
                "Number of slabs of block cache [{0}] with direct memory allocation set to [{1}]", slabCount,
                directAllocation));
        LOG.info(MessageFormat.format(
                "Block cache target memory usage, slab size of [{0}] will allocate [{1}] slabs and use ~[{2}] bytes",
                slabSize, slabCount, ((long) slabCount * (long) slabSize)));

        try {
            long totalMemory = (long) slabCount * (long) numberOfBlocksPerSlab * (long) blockSize;
            blockCache = new BlockCache(directAllocation, totalMemory, slabSize);
        } catch (OutOfMemoryError e) {
            if ("Direct buffer memory".equals(e.getMessage())) {
                System.err.println(
                        "The max direct memory is too low.  Either increase by setting (-XX:MaxDirectMemorySize=<size>g -XX:+UseLargePages) or disable direct allocation by (blur.shard.blockcache.direct.memory.allocation=false) in blur-site.properties");
                System.exit(1);
            }
            throw e;
        }
        cache = new BlockDirectoryCache(blockCache);
    } else {
        cache = BlockDirectory.NO_CACHE;
    }
    _cache = cache;
}

From source file:mx.com.gaby.service.AsynchronousService.java

public void procesar() {
    final Thread.UncaughtExceptionHandler h = new Thread.UncaughtExceptionHandler() {
        public void uncaughtException(Thread th, Throwable ex) {
            System.out.println("Uncaught exception: " + ex);
            System.out.println("TErminar procesamiento aqu: " + ex);

            resultDto.setProcesando(false);
            resultDto.setIndex(0);/*www .java  2  s  .  c o  m*/
            resultDto.setMessage("Uncaught exception - Error de procesamiento dentro del hilo");
            resultDto.setTotalRecords(0);
        }
    };

    try {
        /*Thread t = */
        (new Thread(new Runnable() {
            public void run() {
                Thread.currentThread().setUncaughtExceptionHandler(h);

                enProceso = true;
                resultDto.setProcesando(true);
                resultDto.setTotalRecords(100);

                try {
                    int h = 0;
                    for (h = 1; h <= 100; h++) {
                        resultDto.setIndex(h);
                        resultDto.setMessage("Enviados " + h + " de 100");

                        System.out.println(h + ".- Ejecutando ...");

                        try {
                            for (int i = 1; i <= 100; i++) {
                                Long maxMemory = Runtime.getRuntime().maxMemory();
                                int[] matrix = new int[(int) (maxMemory + 1)];
                                for (int j = 0; j < matrix.length; ++j) {
                                    matrix[j] = j + 1;
                                }
                            }
                        } catch (OutOfMemoryError e) {
                            System.out.println(" : Memory Use : " + e.getMessage());
                        }
                    }

                    Thread.sleep(2000);

                    resultDto.setProcesando(false);
                } catch (InterruptedException ie) {
                    System.out.println("InterruptedException run");

                    resultDto.setProcesando(false);
                    resultDto.setIndex(0);
                    resultDto.setMessage("InterruptedException - Error de procesamiento dentro del hilo");
                    resultDto.setTotalRecords(0);
                }
            }
        })).start();
        //t.setUncaughtExceptionHandler(h);
        //t.start();
    } catch (Exception e) {
        System.out.println("Error: " + e.getMessage());

        resultDto.setProcesando(false);
        resultDto.setIndex(0);
        resultDto.setMessage("Exception - Error de procesamiento dentro del hilo");
        resultDto.setTotalRecords(0);
    }

}

From source file:org.deviceconnect.android.deviceplugin.host.profile.HostCanvasProfile.java

/**
 * Send Image.// w w w  .  j  a v  a  2 s  .c  om
 * @param data binary
 * @param response response message
 * @param enumMode image mode
 * @param x position
 * @param y position
 */
private void sendImage(byte[] data, Intent response, CanvasDrawImageObject.Mode enumMode, double x, double y) {
    try {
        drawImage(response, writeForImage(data), enumMode, x, y);
    } catch (OutOfMemoryError e) {
        MessageUtils.setIllegalDeviceStateError(response, e.getMessage());
    } catch (IOException e) {
        MessageUtils.setIllegalDeviceStateError(response, e.getMessage());
    }
    sendResponse(response);
}

From source file:moefou4j.http.HttpResponse.java

/**
 * Returns the response body as string.<br>
 * Disconnects the internal HttpURLConnection silently.
 * //ww w  .j a v  a  2 s .  c  o  m
 * @return response body
 * @throws MoefouException
 */
public String asString() throws MoefouException {
    if (null == responseAsString) {
        BufferedReader br = null;
        InputStream stream = null;
        try {
            stream = asStream();
            if (null == stream)
                return null;
            br = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
            final StringBuffer buf = new StringBuffer();
            String line;
            while ((line = br.readLine()) != null) {
                buf.append(line).append("\n");
            }
            responseAsString = buf.toString();
            logger.debug(responseAsString);
            stream.close();
            streamConsumed = true;
        } catch (final IOException ioe) {
            throw new MoefouException(ioe.getMessage(), ioe);
        } catch (final OutOfMemoryError e) {
            throw new MoefouException(e.getMessage(), e);
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                } catch (final IOException ignore) {
                }
            }
            if (br != null) {
                try {
                    br.close();
                } catch (final IOException ignore) {
                }
            }
            disconnectForcibly();
        }
    }
    return responseAsString;
}

From source file:twitter4j.http.HttpResponse.java

/**
 * Returns the response body as string.<br>
 * Disconnects the internal HttpURLConnection silently.
 * //from ww  w .  j  av  a  2s . co  m
 * @return response body
 * @throws TwitterException
 */
public String asString() throws TwitterException {
    if (null == responseAsString) {
        BufferedReader br = null;
        InputStream stream = null;
        try {
            stream = asStream();
            if (null == stream)
                return null;
            br = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
            final StringBuilder buf = new StringBuilder();
            String line;
            while ((line = br.readLine()) != null) {
                buf.append(line).append("\n");
            }
            responseAsString = buf.toString();
            logger.debug(responseAsString);
            stream.close();
            streamConsumed = true;
        } catch (final OutOfMemoryError oome) {
            throw new TwitterException(oome.getMessage(), oome);
        } catch (final IOException ioe) {
            throw new TwitterException(ioe.getMessage(), ioe);
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                } catch (final IOException ignore) {
                }
            }
            if (br != null) {
                try {
                    br.close();
                } catch (final IOException ignore) {
                }
            }
            disconnectForcibly();
        }
    }
    return responseAsString;
}

From source file:org.springframework.batch.repeat.support.TaskExecutorRepeatTemplateBulkAsynchronousTests.java

@Test
public void testErrorThrownByCallback() throws Exception {

    callback = new RepeatCallback() {

        private volatile AtomicInteger count = new AtomicInteger(0);

        @Override//from  w  ww.  j  av a 2s .  c  o  m
        public RepeatStatus doInIteration(RepeatContext context) throws Exception {
            int position = count.incrementAndGet();

            if (position == 4) {
                throw new OutOfMemoryError("Planned");
            } else {
                return RepeatStatus.CONTINUABLE;
            }
        }
    };

    template.setCompletionPolicy(new SimpleCompletionPolicy(10));

    try {
        template.iterate(callback);
        fail("Expected planned exception");
    } catch (OutOfMemoryError oome) {
        assertEquals("Planned", oome.getMessage());
    } catch (Exception e) {
        e.printStackTrace();
        fail("Wrong exception was thrown: " + e);
    }
}

From source file:edu.harvard.i2b2.analysis.dataModel.TimelineFactory.java

public static String createlld(int minPatientNum, int maxPatientNum, boolean bDisplayAll, boolean writeFile,
        boolean displayDemographics) {

    ArrayList conceptOrder = new ArrayList();
    int maxLineCount = 0; // zero turns off check for maximum count of lines
    StringBuilder resultFile = new StringBuilder();
    ArrayList<PatientDemographics> demographicsArray = new ArrayList<PatientDemographics>();

    try {//from  w w  w .j a  v a 2 s. c  o  m
        // get the root
        Element root = null;// doc.getRootElement();
        // get the children from the i2b2 document
        java.util.List allChildren = root.getChildren();
        int iNumberOfChildren = allChildren.size();
        // set up the variables for the loop
        String sPatient_num = null;
        String sConcept_cd = null;
        String sOldPatient_num = "start";
        String sOldConcept_cd = null;
        String sStart_date = null;
        String sOldStart_date = null;
        String sEnd_date = null;
        String sInout_cd = null;
        String sDeath_date = null;
        String sColor = null;
        String sHeight = null;
        String sValue = null;
        String sTablename = null;
        int patientNum = 0;
        Date oDate;

        resultFile.append(GetTimelineHeader());
        boolean bOverMax = false;
        int conceptCount = 0;
        int patientCount = minPatientNum;
        StringBuilder patientRecord = new StringBuilder();

        String currentPatientNum = null;
        int indexPos = 0;

        for (int p = 0; p < demographicsArray.size(); p++) {
            PatientDemographics record = demographicsArray.get(p);
            currentPatientNum = record.patientNumber();

            if (displayDemographics) {
                patientRecord.append(getTimelinePatientString(currentPatientNum, record));
            } else {
                patientRecord.append(getTimelinePatientString(currentPatientNum));
            }

            resultFile.append(patientRecord.toString());
            patientRecord = new StringBuilder();
            patientCount++;

            conceptCount = 0;
            sOldConcept_cd = null;
            sOldStart_date = null;
            sOldPatient_num = "";

            if ((indexPos == iNumberOfChildren) && bDisplayAll) {
                conceptCount = 0;
                while ((conceptOrder != null) && (conceptCount < conceptOrder.size())) {
                    patientRecord.append(getTimelineConceptString((String) conceptOrder.get(conceptCount), 1));
                    patientRecord.append(getTimelineEmptyDateString());
                    conceptCount++;
                }

                resultFile.append(patientRecord.toString());
                patientRecord = new StringBuilder();
            }

            for (int i = indexPos; i < iNumberOfChildren; i++) {
                if ((maxLineCount > 0) && (i > maxLineCount)) {
                    bOverMax = true;
                    break;
                }

                Element oChild = (Element) allChildren.get(i);
                sPatient_num = "";//oChild.getChild(ss_patient_num).getText(
                // );

                if (!sPatient_num.equals(currentPatientNum) && (sOldPatient_num.equals("start")) /*
                                                                                                 * &&
                                                                                                 * !sOldPatient_num
                                                                                                 * .equals(
                                                                                                 * sPatient_num
                                                                                                 * )
                                                                                                 */) {
                    if (bDisplayAll) {
                        try {
                            patientNum = Integer.parseInt(sPatient_num);
                            conceptCount = 0;
                            while ((conceptOrder != null) && (conceptCount < conceptOrder.size())) {
                                patientRecord.append(
                                        getTimelineConceptString((String) conceptOrder.get(conceptCount), 1));
                                patientRecord.append(getTimelineEmptyDateString());
                                conceptCount++;
                            }

                            resultFile.append(patientRecord.toString());
                            patientRecord = new StringBuilder();
                            // patientCount++;
                        } catch (java.lang.OutOfMemoryError e) {
                            log.error("In resultset builder 5: " + e.getMessage());
                            // closeConnection(oConnection);
                            return "memory error";
                        } catch (Exception e) {
                            log.error(e.getMessage());
                            // closeConnection(oConnection);
                            return "error";
                        }

                        conceptCount = 0;
                        sOldConcept_cd = null;
                        sOldStart_date = null;
                    }
                    break;
                } else if (!sPatient_num.equals(currentPatientNum) && !(sOldPatient_num.equals("start")) /*
                                                                                                         * &&!
                                                                                                         * sOldPatient_num
                                                                                                         * .equals(
                                                                                                         * sPatient_num
                                                                                                         * )
                                                                                                         */) {
                    if ((bDisplayAll) && (conceptCount < (conceptOrder.size()))) {
                        while ((conceptOrder != null) && (conceptCount < conceptOrder.size())) {
                            patientRecord.append(
                                    getTimelineConceptString((String) conceptOrder.get(conceptCount), 1));
                            patientRecord.append(getTimelineEmptyDateString());
                            conceptCount++;
                        }
                    }

                    resultFile.append(patientRecord.toString());
                    patientRecord = new StringBuilder();
                    patientCount++;

                    conceptCount = 0;
                    sOldConcept_cd = null;
                    sOldStart_date = null;
                    break;
                } else if (sPatient_num.equals(currentPatientNum)) {
                    indexPos = i + 1;
                    sOldPatient_num = sPatient_num;
                    // if (bUseConcept) {
                    // sConcept_cd =
                    // oChild.getChild(ss_concept_cd).getText();
                    // }
                    // else {
                    // sConcept_cd =
                    // oChild.getAttributeValue(ss_q_name_char);
                    // }

                    if (!sConcept_cd.equals(sOldConcept_cd)) {
                        // conceptCount++;
                        if (bDisplayAll) {
                            for (int j = conceptCount; j < conceptOrder.size(); j++) {
                                if (sConcept_cd.equals(conceptOrder.get(j))) {
                                    break;
                                } else {
                                    patientRecord
                                            .append(getTimelineConceptString((String) conceptOrder.get(j), 1));
                                    patientRecord.append(getTimelineEmptyDateString());
                                    conceptCount++;
                                }
                            }
                        }

                        // int iNumConceptObservations =
                        // getNumConceptObservationsRollingupStartDate
                        // (allChildren,i);

                        // patientRecord.append(getTimelineConceptString(
                        // sConcept_cd,iNumConceptObservations));
                        conceptCount++;
                        sOldStart_date = null;
                    }

                    sOldConcept_cd = sConcept_cd;
                    // sStart_date =
                    // oChild.getChild(ss_start_date).getText();
                    // if (!sStart_date.equals(sOldStart_date)) {
                    // if (!sStart_date.equals(null)) {
                    if ((!sStart_date.equals(null))
                            && ((sOldStart_date == null) || (!sStart_date.equals(sOldStart_date)))) {
                        // sEnd_date =
                        // oChild.getChild(ss_end_date).getText();
                        if ((sEnd_date == null) || (sEnd_date.trim().length() == 0))
                            sEnd_date = sStart_date;
                        // sInout_cd =
                        // oChild.getChild(ss_inout_cd).getText();
                        sInout_cd = "";
                        // sColor = oChild.getChild(ss_color_cd).getText();
                        // sHeight =
                        // oChild.getChild(ss_height_cd).getText();
                        // sValue = oChild.getChild(ss_value_cd).getText();
                        // sTablename =
                        // oChild.getChild(ss_table_name).getText();
                        String prefix = "C";
                        if (sTablename.equalsIgnoreCase("visit_dimension")) {
                            prefix = "E";
                        } else if (sTablename.equalsIgnoreCase("provider_dimension")) {
                            prefix = "P";
                        }

                        // if ((sValue==null)||(sValue.length()==0)) {
                        // sValue = prefix+" = ::"+sConcept_cd+"::"+
                        // "$$"+oChild.getChild(ss_patient_num).getText()+
                        // "$$"+oChild.getChild(ss_concept_cd).getText() +
                        // "$$"+ChangeRsDateFull(sStart_date) ;//+"::";
                    } else {
                        // sValue = prefix+" Value = " +
                        // "::"+sConcept_cd+": "+sValue+"::"+
                        // "$$"+oChild.getChild(ss_patient_num).getText()+
                        // "$$"+oChild.getChild(ss_concept_cd).getText() +
                        // "$$"+ChangeRsDateFull(sStart_date) ;//+"::";
                    }

                    // log.debug("   "+ ChangeRsDate(sStart_date) + " -> " +
                    // ChangeRsDate(sEnd_date));
                    // System.out.print(getTimelineDateString(ChangeRsDate(
                    // sStart_date),ChangeRsDate(sEnd_date)));
                    // System.out.print(getTimelineDateString(ChangeRsDate(
                    // sStart_date),ChangeRsDate(sEnd_date),sConcept_cd));
                    if (sInout_cd.equalsIgnoreCase("I")) {
                        if (sColor != null)
                            patientRecord.append(getTimelineDateStringSpecial(ChangeRsDate(sStart_date),
                                    ChangeRsDate(sEnd_date), sColor));
                        else
                            patientRecord.append(getTimelineDateStringSpecial(ChangeRsDate(sStart_date),
                                    ChangeRsDate(sEnd_date)));
                    } else if (sInout_cd.equalsIgnoreCase("E")) {
                        if (sColor != null)
                            patientRecord.append(getTimelineDateStringEncounter(ChangeRsDate(sStart_date),
                                    ChangeRsDate(sEnd_date), sColor));
                        else
                            patientRecord.append(getTimelineDateStringEncounter(ChangeRsDate(sStart_date),
                                    ChangeRsDate(sEnd_date)));
                    } else if (sInout_cd.equalsIgnoreCase("D")) {
                        if (sStart_date.length() == 0) {
                            if (sColor != null)
                                patientRecord.append(getTimelineDateStringEncounter("today", "today", sColor));
                            else
                                patientRecord.append(getTimelineDateStringEncounter("today", "today"));
                        } else {
                            if (sColor != null)
                                patientRecord.append(
                                        getTimelineDateStringDeath(ChangeRsDate(sStart_date), "today", sColor));
                            else
                                patientRecord.append(
                                        getTimelineDateStringDeath(ChangeRsDate(sStart_date), "today", sColor));
                        }
                    } else {
                        if (sConcept_cd.equals("Death")) {
                            if (sStart_date.length() == 0) {
                                sStart_date = "today";
                                sColor = "lightbrown";
                            }
                            sEnd_date = "today";
                        }
                        if (sColor != null) {
                            if (sConcept_cd.equalsIgnoreCase("EGFR"))
                                patientRecord.append(getTimelineDateString(ChangeRsDate(sStart_date),
                                        ChangeRsDate(sEnd_date), sColor,
                                        "http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?db=gene&cmd=Retrieve&dopt=Graphics&list_uids=1956"));
                            else
                                patientRecord.append(getTimelineDateStringHeight(ChangeRsDate(sStart_date),
                                        ChangeRsDate(sEnd_date), sColor, sHeight, sValue));
                        } else
                            patientRecord.append(getTimelineDateStringHeight(ChangeRsDate(sStart_date),
                                    ChangeRsDate(sEnd_date), sHeight));
                    }
                }
                sOldStart_date = sStart_date;

                if (!bOverMax) {
                    if (bDisplayAll && (indexPos == iNumberOfChildren)) {
                        while ((conceptOrder != null) && (conceptCount < conceptOrder.size())) {
                            patientRecord.append(
                                    getTimelineConceptString((String) conceptOrder.get(conceptCount), 1));
                            patientRecord.append(getTimelineEmptyDateString());
                            conceptCount++;
                        }
                    }
                    resultFile.append(patientRecord.toString());
                    patientRecord = new StringBuilder();
                    patientCount++;
                }
            }
        }

        if ((!bOverMax) && bDisplayAll) {
            while ((conceptOrder != null) && (conceptCount < conceptOrder.size())) {
                patientRecord.append(getTimelineConceptString((String) conceptOrder.get(conceptCount), 1));
                patientRecord.append(getTimelineEmptyDateString());
                conceptCount++;
            }
            resultFile.append(patientRecord.toString());
        }

        resultFile.append(GetTimelineFooter());
        log.debug(" Total Count " + iNumberOfChildren);

        if (writeFile) {
            String i2b2File = System.getProperty("user.dir") + '/' + "i2b2xml.lld";
            File oDelete = new File(i2b2File);
            if (oDelete != null)
                oDelete.delete();
            RandomAccessFile f = new RandomAccessFile(i2b2File, "rw");
            Lib.append(f, resultFile.toString());
            f.close();
        }

        if (bOverMax) {
            log.debug("reached maximum at " + new Date());
            return "overmaximum";
        }
    } catch (java.lang.OutOfMemoryError e) {
        log.error("In resultset builder 6: " + e.getMessage());
        // closeConnection(oConnection);
        return "memory error";
    } catch (Exception e) {
        log.error(e.getMessage());
        // closeConnection(oConnection);
        return "error";
    }

    log.debug("done at " + new Date());
    return resultFile.toString();

}

From source file:uk.bl.wa.tika.TikaDeepIdentifier.java

/**
 * // w w w .j a v  a  2s  . com
 * @param payload
 * @param metadata 
 * @return
 */
public String identify(InputStream payload, Metadata metadata) {
    // Fallback
    String tikaType = MediaType.OCTET_STREAM.toString();
    // Set up metadata object:
    Metadata md = metadata;
    TikaInputStream tis = null;
    try {
        tis = TikaInputStream.get(payload);
        // Type according to Tiki:
        tikaType = pika.getDetector().detect(tis, md).toString();
    } catch (Throwable e) {
        log.error("Tika.detect failed:" + e.getMessage());
        //e.printStackTrace();
        return MediaType.OCTET_STREAM.toString();
    } finally {
        if (tis != null) {
            try {
                tis.close();
            } catch (IOException e) {
                log.warn("Exception closing TikaInputStream. This leaves tmp-files: " + e.getMessage());
            }
        }
    }

    // Now perform full parse, to find a more detailed tikaType
    try {
        // Default to detected MIME Type:
        md.set(Metadata.CONTENT_TYPE, tikaType.toString());

        // Ensure parsing is NOT recursive:
        pika.setRecursive(ctx, false);

        // Now perform the parsing:
        //parser.parse( new ByteArrayInputStream( payload ), ch, md, ctx );
        // One could forcibly limit the size if OOM is still causing problems, like this:
        //parser.parse( new ByteArrayInputStream( value.getPayload(), 0, BUF_8KB ), ch, md, ctx );

        // Every resource gets it's own write-out buffer:
        ch = new WriteOutContentHandler(MAX_BUF);

        // Run the parser in a separate thread:
        InputStream tikainput = TikaInputStream.get(payload);
        ParseRunner runner = new ParseRunner(pika, tikainput, ch, md, ctx);
        Thread parseThread = new Thread(runner, Long.toString(System.currentTimeMillis()));
        parseThread.setDaemon(true); // Daemon to ensure proper shutdown when overall processing has finished
        try {
            // TODO: This should use TimeLimiter.run(parser, 30000L, false); but that is in the warc-indexer module
            parseThread.start();
            parseThread.join(this.parseTimeout);
            parseThread.interrupt();
        } catch (OutOfMemoryError o) {
            log.error("TikaExtractor.parse(): " + tikaType + " : " + o.getMessage());
        } catch (RuntimeException r) {
            log.error("TikaExtractor.parse(): " + tikaType + " : " + r.getMessage());
        } finally {
            if (tikainput != null) {
                try {
                    tikainput.close();
                } catch (IOException e) {
                    log.warn("Exception closing TikaInputStream. This leaves tmp-files: " + e.getMessage());
                }
            }
        }

        // Use the extended MIME type generated by the PreservationParser:
        String extMimeType = md.get(PreservationParser.EXT_MIME_TYPE);
        if (runner.complete && extMimeType != null)
            tikaType = extMimeType;

    } catch (Throwable e) {
        log.debug("Tika Exception: " + e.getMessage());
        //e.printStackTrace();
    }
    // Return whichever value works:
    return tikaType;
}

From source file:uk.bl.wa.tika.TikaDeepIdentifier.java

/**
 * // ww w .  jav a  2s .c om
 * @param payload
 * @return
 */
public String identify(byte[] payload) {
    // Fallback
    String tikaType = MediaType.OCTET_STREAM.toString();
    // Set up metadata object:
    Metadata md = new Metadata();
    TikaInputStream tis = null;
    try {
        tis = TikaInputStream.get(payload, md);
        // Type according to Tiki:
        tikaType = pika.getDetector().detect(tis, md).toString();
    } catch (Throwable e) {
        log.error("Tika.detect failed:" + e.getMessage());
        //e.printStackTrace();
        return MediaType.OCTET_STREAM.toString();
    } finally {
        if (tis != null) {
            try {
                tis.close();
            } catch (IOException e) {
                log.warn("Exception closing TikaInputStream. This leaves tmp-files: " + e.getMessage());
            }
        }
    }

    // Now perform full parse, to find a more detailed tikaType
    try {
        // Default to detected MIME Type:
        md.set(Metadata.CONTENT_TYPE, tikaType.toString());

        // Ensure parsing is NOT recursive:
        pika.setRecursive(ctx, false);

        // Now perform the parsing:
        //parser.parse( new ByteArrayInputStream( payload ), ch, md, ctx );
        // One could forcibly limit the size if OOM is still causing problems, like this:
        //parser.parse( new ByteArrayInputStream( value.getPayload(), 0, BUF_8KB ), ch, md, ctx );

        // Every resource gets it's own write-out buffer:
        ch = new WriteOutContentHandler(MAX_BUF);

        // Run the parser in a separate thread:
        InputStream tikainput = TikaInputStream.get(payload, md);
        ParseRunner runner = new ParseRunner(pika, tikainput, ch, md, ctx);
        Thread parseThread = new Thread(runner, Long.toString(System.currentTimeMillis()));
        parseThread.setDaemon(true); // Daemon to ensure proper shutdown when overall processing has finished

        try {
            // TODO: This should use TimeLimiter.run(parser, 30000L, false); but that is in the warc-indexer module
            parseThread.start();
            parseThread.join(this.parseTimeout);
            parseThread.interrupt();
        } catch (OutOfMemoryError o) {
            log.error("TikaExtractor.parse(): " + tikaType + " : " + o.getMessage());
        } catch (RuntimeException r) {
            log.error("TikaExtractor.parse(): " + tikaType + " : " + r.getMessage());
        } finally {
            if (tikainput != null) {
                try {
                    tikainput.close();
                } catch (IOException e) {
                    log.warn("Exception closing TikaInputStream. This leaves tmp-files: " + e.getMessage());
                }
            }
        }

        // Use the extended MIME type generated by the PreservationParser:
        String extMimeType = md.get(PreservationParser.EXT_MIME_TYPE);
        if (runner.complete && extMimeType != null)
            tikaType = extMimeType;

    } catch (Throwable e) {
        log.debug("Tika Exception: " + e.getMessage());
        //e.printStackTrace();
    }
    // Return whichever value works:
    return tikaType;
}