Example usage for java.lang OutOfMemoryError toString

List of usage examples for java.lang OutOfMemoryError toString

Introduction

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

Prototype

public String toString() 

Source Link

Document

Returns a short description of this throwable.

Usage

From source file:com.alibaba.akita.io.HttpInvoker.java

/**
 * Vversion 2 remoteimageview download impl, use byte[] to decode.
 * Note: Recommanded to use this method instead of version 1.
 * NUM_RETRIES retry.//ww  w  .j  a v  a2s.c o  m
 * @param imgUrl
 * @param httpReferer http Referer
 * @return
 * @throws AkServerStatusException
 * @throws AkInvokeException
 */
public static Bitmap getBitmapFromUrl(String imgUrl, String httpReferer, ProgressBar progressBar)
        throws AkServerStatusException, AkInvokeException {
    imgUrl = imgUrl.trim();
    Log.v(TAG, "getBitmapFromUrl:" + imgUrl);

    int timesTried = 1;

    while (timesTried <= NUM_RETRIES) {
        timesTried++;
        try {
            if (progressBar != null) {
                progressBar.setProgress(0);
            }
            HttpGet request = new HttpGet(imgUrl);
            if (httpReferer != null)
                request.addHeader("Referer", httpReferer);
            HttpResponse response = client.execute(request);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_CREATED
                    || statusCode == HttpStatus.SC_ACCEPTED) {
                HttpEntity resEntity = response.getEntity();
                InputStream inputStream = resEntity.getContent();

                byte[] imgBytes = retrieveImageData(inputStream, (int) (resEntity.getContentLength()),
                        progressBar);
                if (imgBytes == null) {
                    SystemClock.sleep(DEFAULT_RETRY_SLEEP_TIME);
                    continue;
                }

                Bitmap bm = null;
                try {
                    bm = ImageUtil.decodeSampledBitmapFromByteArray(imgBytes, 0, imgBytes.length, 682, 682);
                } catch (OutOfMemoryError ooe) {
                    Log.e(TAG, ooe.toString(), ooe);
                    return null; // if oom, no need to retry.
                }
                if (bm == null) {
                    SystemClock.sleep(DEFAULT_RETRY_SLEEP_TIME);
                    continue;
                }
                return bm;
            } else {
                HttpEntity resEntity = response.getEntity();
                throw new AkServerStatusException(response.getStatusLine().getStatusCode(),
                        EntityUtils.toString(resEntity, CHARSET));
            }
        } catch (ClientProtocolException cpe) {
            Log.e(TAG, cpe.toString(), cpe);
            throw new AkInvokeException(AkInvokeException.CODE_HTTP_PROTOCOL_ERROR, cpe.toString(), cpe);
        } catch (IOException ioe) {
            Log.e(TAG, ioe.toString(), ioe);
            throw new AkInvokeException(AkInvokeException.CODE_CONNECTION_ERROR, ioe.toString(), ioe);
        } catch (IllegalStateException ise) {
            Log.e(TAG, ise.toString(), ise);
            throw new AkInvokeException(AkInvokeException.CODE_TARGET_HOST_OR_URL_ERROR, ise.toString(), ise);
        } catch (IllegalArgumentException iae) {
            throw new AkInvokeException(AkInvokeException.CODE_TARGET_HOST_OR_URL_ERROR, iae.toString(), iae);
        } catch (Exception e) {
            throw new AkInvokeException(AkInvokeException.CODE_UNKOWN_ERROR, e.toString(), e);
        }

    }

    return null;
}

From source file:com.android.phone.common.mail.internet.MimeUtility.java

/**
 * Reads the Part's body and returns a String based on any charset conversion that needed
 * to be done.//from   w ww  .jav  a  2 s  .c  o m
 * @param part The part containing a body
 * @return a String containing the converted text in the body, or null if there was no text
 * or an error during conversion.
 */
public static String getTextFromPart(Part part) {
    try {
        if (part != null && part.getBody() != null) {
            InputStream in = part.getBody().getInputStream();
            String mimeType = part.getMimeType();
            if (mimeType != null && MimeUtility.mimeTypeMatches(mimeType, "text/*")) {
                /*
                 * Now we read the part into a buffer for further processing. Because
                 * the stream is now wrapped we'll remove any transfer encoding at this point.
                 */
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                IOUtils.copy(in, out);
                in.close();
                in = null; // we want all of our memory back, and close might not release

                /*
                 * We've got a text part, so let's see if it needs to be processed further.
                 */
                String charset = getHeaderParameter(part.getContentType(), "charset");
                if (charset != null) {
                    /*
                     * See if there is conversion from the MIME charset to the Java one.
                     */
                    charset = CharsetUtil.toJavaCharset(charset);
                }
                /*
                 * No encoding, so use us-ascii, which is the standard.
                 */
                if (charset == null) {
                    charset = "ASCII";
                }
                /*
                 * Convert and return as new String
                 */
                String result = out.toString(charset);
                out.close();
                return result;
            }
        }

    } catch (OutOfMemoryError oom) {
        /*
         * If we are not able to process the body there's nothing we can do about it. Return
         * null and let the upper layers handle the missing content.
         */
        Log.e(LOG_TAG, "Unable to getTextFromPart " + oom.toString());
    } catch (Exception e) {
        /*
         * If we are not able to process the body there's nothing we can do about it. Return
         * null and let the upper layers handle the missing content.
         */
        Log.e(LOG_TAG, "Unable to getTextFromPart " + e.toString());
    }
    return null;
}

From source file:com.android.email.mail.internet.MimeUtility.java

/**
 * Reads the Part's body and returns a String based on any charset conversion that needed
 * to be done.//www. j  a  v  a 2 s .com
 * @param part The part containing a body
 * @return a String containing the converted text in the body, or null if there was no text
 * or an error during conversion.
 */
public static String getTextFromPart(Part part) {
    try {
        if (part != null && part.getBody() != null) {
            InputStream in = part.getBody().getInputStream();
            String mimeType = part.getMimeType();
            if (mimeType != null && MimeUtility.mimeTypeMatches(mimeType, "text/*")) {
                /*
                 * Now we read the part into a buffer for further processing. Because
                 * the stream is now wrapped we'll remove any transfer encoding at this point.
                 */
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                IOUtils.copy(in, out);
                in.close();
                in = null; // we want all of our memory back, and close might not release

                /*
                 * We've got a text part, so let's see if it needs to be processed further.
                 */
                String charset = getHeaderParameter(part.getContentType(), "charset");
                if (charset != null) {
                    /*
                     * See if there is conversion from the MIME charset to the Java one.
                     */
                    charset = CharsetUtil.toJavaCharset(charset);
                }
                /*
                 * No encoding, so use us-ascii, which is the standard.
                 */
                if (charset == null) {
                    charset = "ASCII";
                }
                /*
                 * Convert and return as new String
                 */
                String result = out.toString(charset);
                out.close();
                return result;
            }
        }

    } catch (OutOfMemoryError oom) {
        /*
         * If we are not able to process the body there's nothing we can do about it. Return
         * null and let the upper layers handle the missing content.
         */
        Log.e(Email.LOG_TAG, "Unable to getTextFromPart " + oom.toString());
    } catch (Exception e) {
        /*
         * If we are not able to process the body there's nothing we can do about it. Return
         * null and let the upper layers handle the missing content.
         */
        Log.e(Email.LOG_TAG, "Unable to getTextFromPart " + e.toString());
    }
    return null;
}

From source file:com.android.emailcommon.internet.MimeUtility.java

/**
 * Reads the Part's body and returns a String based on any charset conversion that needed
 * to be done.//from   w  w w . j  a  v a 2s  . co  m
 * @param part The part containing a body
 * @return a String containing the converted text in the body, or null if there was no text
 * or an error during conversion.
 */
public static String getTextFromPart(Part part) {
    try {
        if (part != null && part.getBody() != null) {
            InputStream in = part.getBody().getInputStream();
            String mimeType = part.getMimeType();
            if (mimeType != null && MimeUtility.mimeTypeMatches(mimeType, "text/*")) {
                /*
                 * Now we read the part into a buffer for further processing. Because
                 * the stream is now wrapped we'll remove any transfer encoding at this point.
                 */
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                IOUtils.copy(in, out);
                in.close();
                in = null; // we want all of our memory back, and close might not release

                /*
                 * We've got a text part, so let's see if it needs to be processed further.
                 */
                String charset = getHeaderParameter(part.getContentType(), "charset");
                if (charset != null) {
                    /*
                     * See if there is conversion from the MIME charset to the Java one.
                     */
                    charset = CharsetUtil.toJavaCharset(charset);
                }
                /*
                 * No encoding, so use us-ascii, which is the standard.
                 */
                if (charset == null) {
                    charset = "ASCII";
                }
                /*
                 * Convert and return as new String
                 */
                String result = out.toString(charset);
                out.close();
                return result;
            }
        }

    } catch (OutOfMemoryError oom) {
        /*
         * If we are not able to process the body there's nothing we can do about it. Return
         * null and let the upper layers handle the missing content.
         */
        Log.e(Logging.LOG_TAG, "Unable to getTextFromPart " + oom.toString());
    } catch (Exception e) {
        /*
         * If we are not able to process the body there's nothing we can do about it. Return
         * null and let the upper layers handle the missing content.
         */
        Log.e(Logging.LOG_TAG, "Unable to getTextFromPart " + e.toString());
    }
    return null;
}

From source file:net.yacy.cora.protocol.http.HTTPClient.java

/**
 * Return entity content loaded as a byte array
 * @param entity HTTP entity// w  ww  .jav a2 s . c om
 * @param maxBytes maximum bytes to read. -1 means no maximum limit.
 * @return content bytes or null when entity content is null.
 * @throws IOException when a read error occured or content length is over maxBytes
 */
public static byte[] getByteArray(final HttpEntity entity, int maxBytes) throws IOException {
    final InputStream instream = entity.getContent();
    if (instream == null) {
        return null;
    }
    try {
        long contentLength = entity.getContentLength();
        /*
         * When no maxBytes is specified, the default limit is
         * Integer.MAX_VALUE as a byte array size can not be over
         */
        if (maxBytes < 0) {
            maxBytes = Integer.MAX_VALUE;
        }
        /*
         * Content length may already be known now : check it before
         * downloading
         */
        if (contentLength > maxBytes) {
            throw new IOException(
                    "Content to download exceed maximum value of " + Formatter.bytesToString(maxBytes));
        }
        int initialSize = Math.min(maxBytes, (int) contentLength);
        /* ContentLenght may be negative because unknown for now */
        if (initialSize < 0) {
            initialSize = 4096;
        }
        final ByteArrayBuffer buffer = new ByteArrayBuffer(initialSize);
        byte[] tmp = new byte[4096];
        int l = 0;
        /* Sum is a long to enable check against Integer.MAX_VALUE */
        long sum = 0;
        while ((l = instream.read(tmp)) != -1) {
            sum += l;
            /*
             * Check total length while downloading as content length might
             * not be known at beginning
             */
            if (sum > maxBytes) {
                throw new IOException(
                        "Download exceeded maximum value of " + Formatter.bytesToString(maxBytes));
            }
            buffer.append(tmp, 0, l);
        }
        return buffer.toByteArray();
    } catch (final OutOfMemoryError e) {
        throw new IOException(e.toString());
    } finally {
        instream.close();
    }
}

From source file:edu.valelab.GaussianFit.ZCalibrator.java

/**
 * Use the fitfunction to estimate the z position given width in x and y
 * /*from   w w  w  .jav  a  2s.c  o  m*/
 * minimize the distance D in sqrt wx and sqrt wy space
 * D = sqrt (  square (sqrt wx - sqrt wx, calib) + sqr(sqrt wy - sqrt w, calib) )
 * 
 * 
 */

public double getZ(double wx, double wy) {
    if (!hasFitFunctions())
        return 0.0;

    NelderMead nmx = new NelderMead();
    SimpleScalarValueChecker convergedChecker_ = new SimpleScalarValueChecker(1e-6, -1);

    MultiVariateZFunction mz = new MultiVariateZFunction(fitFunctionWx_, fitFunctionWy_, wx, wy);

    double[] params0_ = new double[1]; // initial estimates:
    params0_[0] = 15; // TODO: Need the middle z value of the stack here!!!

    nmx.setStartConfiguration(params0_);
    nmx.setConvergenceChecker(convergedChecker_);
    nmx.setMaxIterations(maxIterations_);

    double[] paramsOut = { 0.0 };

    try {
        RealPointValuePair result = nmx.optimize(mz, GoalType.MINIMIZE, params0_);
        paramsOut = result.getPoint();
    } catch (java.lang.OutOfMemoryError e) {
        throw (e);
    } catch (Exception e) {
        ij.IJ.log(" " + e.toString());
    }

    return paramsOut[0];
}

From source file:edu.valelab.gaussianfit.fitting.ZCalibrator.java

/**
 * Use the fitfunction to estimate the z position given width in x and y
 * //from w w  w .j  a  v  a  2  s .c  o  m
 * minimize the distance D in sqrt wx and sqrt wy space
 * D = sqrt (  square (sqrt wx - sqrt wx, calib) + sqr(sqrt wy - sqrt w, calib) )
 * 
 * 
 * @param wx - width in x
 * @param wy - width in y
 * @return - calculated z position
 */

public double getZ(double wx, double wy) {
    if (!hasFitFunctions())
        return 0.0;

    NelderMead nmx = new NelderMead();
    SimpleScalarValueChecker convergedChecker_ = new SimpleScalarValueChecker(1e-6, -1);

    MultiVariateZFunction mz = new MultiVariateZFunction(fitFunctionWx_, fitFunctionWy_, wx, wy);

    double[] params0_ = new double[1]; // initial estimates:
    params0_[0] = 15; // TODO: Need the middle z value of the stack here!!!

    nmx.setStartConfiguration(params0_);
    nmx.setConvergenceChecker(convergedChecker_);
    nmx.setMaxIterations(maxIterations_);

    double[] paramsOut = { 0.0 };

    try {
        RealPointValuePair result = nmx.optimize(mz, GoalType.MINIMIZE, params0_);
        paramsOut = result.getPoint();
    } catch (java.lang.OutOfMemoryError e) {
        throw (e);
    } catch (FunctionEvaluationException e) {
        ij.IJ.log(" " + e.toString());
    } catch (OptimizationException e) {
        ij.IJ.log(" " + e.toString());
    } catch (IllegalArgumentException e) {
        ij.IJ.log(" " + e.toString());
    }

    return paramsOut[0];
}

From source file:com.facebook.stetho.inspector.protocol.module.Network.java

private GetResponseBodyResponse readResponseBody(String requestId) throws IOException, JsonRpcException {
    GetResponseBodyResponse response = new GetResponseBodyResponse();
    ResponseBodyData bodyData;//from  w w  w  .j  a  v  a2 s.  c  o m
    try {
        bodyData = mResponseBodyFileManager.readFile(requestId);
    } catch (OutOfMemoryError e) {
        throw new JsonRpcException(
                new JsonRpcError(JsonRpcError.ErrorCode.INTERNAL_ERROR, e.toString(), null /* data */));
    }
    response.body = bodyData.data;
    response.base64Encoded = bodyData.base64Encoded;
    return response;
}

From source file:org.pentaho.di.trans.steps.fuzzymatch.FuzzyMatch.java

private void addToCache(Object[] value) throws KettleException {
    try {// w  ww.  j av a  2s .c  o m
        data.look.add(value);
    } catch (java.lang.OutOfMemoryError o) {
        // exception out of memory
        throw new KettleException(BaseMessages.getString(PKG, "FuzzyMatch.Error.JavaHeap", o.toString()));
    }
}

From source file:com.netscape.kra.StorageKeyUnit.java

/**
 * Decrypts shares with the given password.
 *///www.  j a v a2 s.  c om
public byte[] decryptShare(CryptoToken token, String encoding, String pwd) throws EBaseException {
    try {
        CMS.debug("StorageKeyUnit.decryptShare");
        byte share[] = Utils.base64decode(encoding);
        Cipher cipher = token.getCipherContext(EncryptionAlgorithm.DES3_CBC_PAD);
        SymmetricKey sk = StorageKeyUnit.buildSymmetricKey(token, pwd);

        cipher.initDecrypt(sk, IV);
        byte dec[] = cipher.doFinal(share);

        if (dec == null || !verifyShare(dec)) {
            // invalid passwod
            throw new EBaseException(CMS.getUserMessage("CMS_AUTHENTICATION_INVALID_CREDENTIAL"));
        }
        return postVerify(dec);
    } catch (OutOfMemoryError e) {
        // XXX - this happens in cipher.doFinal when
        // the given share is not valid (the password
        // given from the agent is not correct).
        // Actulla, cipher.doFinal should return
        // something better than this!
        //
        // e.printStackTrace();
        //
        throw new EBaseException(CMS.getUserMessage("CMS_KRA_INVALID_PASSWORD", e.toString()));
    } catch (TokenException e) {
        throw new EBaseException(CMS.getUserMessage("CMS_KRA_INVALID_PASSWORD", e.toString()));
    } catch (NoSuchAlgorithmException e) {
        throw new EBaseException(CMS.getUserMessage("CMS_KRA_INVALID_PASSWORD", e.toString()));
    } catch (InvalidKeyException e) {
        throw new EBaseException(CMS.getUserMessage("CMS_KRA_INVALID_PASSWORD", e.toString()));
    } catch (InvalidAlgorithmParameterException e) {
        throw new EBaseException(CMS.getUserMessage("CMS_KRA_INVALID_PASSWORD", e.toString()));
    } catch (IllegalBlockSizeException e) {
        throw new EBaseException(CMS.getUserMessage("CMS_KRA_INVALID_PASSWORD", e.toString()));
    } catch (BadPaddingException e) {
        throw new EBaseException(CMS.getUserMessage("CMS_KRA_INVALID_PASSWORD", e.toString()));
    }
}