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 022public final class IOExceptionSupport { 023 024 private IOExceptionSupport() { 025 } 026 027 public static IOException create(String msg, Throwable cause) { 028 IOException exception = new IOException(msg); 029 exception.initCause(cause); 030 return exception; 031 } 032 033 public static IOException create(String msg, Exception cause) { 034 IOException exception = new IOException(msg); 035 exception.initCause(cause); 036 return exception; 037 } 038 039 public static IOException create(Throwable cause) { 040 IOException exception = new IOException(cause.getMessage()); 041 exception.initCause(cause); 042 return exception; 043 } 044 045 public static IOException create(Exception cause) { 046 IOException exception = new IOException(cause.getMessage()); 047 exception.initCause(cause); 048 return exception; 049 } 050 051 public static IOException createFrameSizeException(int size, long maxSize) { 052 return new IOException("Frame size of " + toHumanReadableSizeString(size) + 053 " larger than max allowed " + toHumanReadableSizeString(maxSize)); 054 } 055 056 private static String toHumanReadableSizeString(final int size) { 057 return toHumanReadableSizeString(BigInteger.valueOf(size)); 058 } 059 060 private static String toHumanReadableSizeString(final long size) { 061 return toHumanReadableSizeString(BigInteger.valueOf(size)); 062 } 063 064 private static String toHumanReadableSizeString(final BigInteger size) { 065 String displaySize; 066 067 final BigInteger ONE_KB_BI = BigInteger.valueOf(1024); 068 final BigInteger ONE_MB_BI = ONE_KB_BI.multiply(ONE_KB_BI); 069 final BigInteger ONE_GB_BI = ONE_KB_BI.multiply(ONE_MB_BI); 070 071 if (size.divide(ONE_GB_BI).compareTo(BigInteger.ZERO) > 0) { 072 displaySize = String.valueOf(size.divide(ONE_GB_BI)) + " GB"; 073 } else if (size.divide(ONE_MB_BI).compareTo(BigInteger.ZERO) > 0) { 074 displaySize = String.valueOf(size.divide(ONE_MB_BI)) + " MB"; 075 } else if (size.divide(ONE_KB_BI).compareTo(BigInteger.ZERO) > 0) { 076 displaySize = String.valueOf(size.divide(ONE_KB_BI)) + " KB"; 077 } else { 078 displaySize = String.valueOf(size) + " bytes"; 079 } 080 081 return displaySize; 082 } 083}