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            " larger than max allowed " + toHumanReadableSizeString(maxSize));
056    }
057
058    private static String toHumanReadableSizeString(final int size) {
059        return toHumanReadableSizeString(BigInteger.valueOf(size));
060    }
061
062    private static String toHumanReadableSizeString(final long size) {
063        return toHumanReadableSizeString(BigInteger.valueOf(size));
064    }
065
066    private static String toHumanReadableSizeString(final BigInteger size) {
067        String displaySize;
068
069        final BigInteger ONE_KB_BI = BigInteger.valueOf(1024);
070        final BigInteger ONE_MB_BI = ONE_KB_BI.multiply(ONE_KB_BI);
071        final BigInteger ONE_GB_BI = ONE_KB_BI.multiply(ONE_MB_BI);
072
073        if (size.divide(ONE_GB_BI).compareTo(BigInteger.ZERO) > 0) {
074            displaySize = String.valueOf(size.divide(ONE_GB_BI)) + " GB";
075        } else if (size.divide(ONE_MB_BI).compareTo(BigInteger.ZERO) > 0) {
076            displaySize = String.valueOf(size.divide(ONE_MB_BI)) + " MB";
077        } else if (size.divide(ONE_KB_BI).compareTo(BigInteger.ZERO) > 0) {
078            displaySize = String.valueOf(size.divide(ONE_KB_BI)) + " KB";
079        } else {
080            displaySize = String.valueOf(size) + " bytes";
081        }
082
083        return displaySize;
084    }
085}