001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.util;
018
019import java.io.IOException;
020import java.math.BigInteger;
021
022import org.apache.activemq.MaxFrameSizeExceededException;
023
024public final class IOExceptionSupport {
025
026    private IOExceptionSupport() {
027    }
028
029    public static IOException create(String msg, Throwable cause) {
030        IOException exception = new IOException(msg);
031        exception.initCause(cause);
032        return exception;
033    }
034
035    public static IOException create(String msg, Exception cause) {
036        IOException exception = new IOException(msg);
037        exception.initCause(cause);
038        return exception;
039    }
040
041    public static IOException create(Throwable cause) {
042        IOException exception = new IOException(cause.getMessage());
043        exception.initCause(cause);
044        return exception;
045    }
046
047    public static IOException create(Exception cause) {
048        IOException exception = new IOException(cause.getMessage());
049        exception.initCause(cause);
050        return exception;
051    }
052
053    public static IOException createFrameSizeException(int size, long maxSize) {
054        return new MaxFrameSizeExceededException("Frame size of " + toHumanReadableSizeString(size) +
055            " is larger than max allowed " + toHumanReadableSizeString(maxSize));
056    }
057
058    public static IOException createFrameSizeBufferException(int bufferSize, long frameSize) {
059        return new IOException("Estimated allocated buffer size of "
060          + toHumanReadableSizeString(bufferSize) + " is larger than frame size of "
061          + toHumanReadableSizeString(frameSize));
062    }
063
064    private static String toHumanReadableSizeString(final int size) {
065        return toHumanReadableSizeString(BigInteger.valueOf(size));
066    }
067
068    private static String toHumanReadableSizeString(final long size) {
069        return toHumanReadableSizeString(BigInteger.valueOf(size));
070    }
071
072    private static String toHumanReadableSizeString(final BigInteger size) {
073        String displaySize;
074
075        final BigInteger ONE_KB_BI = BigInteger.valueOf(1024);
076        final BigInteger ONE_MB_BI = ONE_KB_BI.multiply(ONE_KB_BI);
077        final BigInteger ONE_GB_BI = ONE_KB_BI.multiply(ONE_MB_BI);
078
079        if (size.divide(ONE_GB_BI).compareTo(BigInteger.ZERO) > 0) {
080            displaySize = String.valueOf(size.divide(ONE_GB_BI)) + " GB";
081        } else if (size.divide(ONE_MB_BI).compareTo(BigInteger.ZERO) > 0) {
082            displaySize = String.valueOf(size.divide(ONE_MB_BI)) + " MB";
083        } else if (size.divide(ONE_KB_BI).compareTo(BigInteger.ZERO) > 0) {
084            displaySize = String.valueOf(size.divide(ONE_KB_BI)) + " KB";
085        } else {
086            displaySize = String.valueOf(size) + " bytes";
087        }
088
089        return displaySize;
090    }
091}