1 /* 2 * Copyright 2012 Sebastian Annies, Hamburg 3 * 4 * Licensed under the Apache License, Version 2.0 (the License); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an AS IS BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.googlecode.mp4parser.util; 17 18 19 public class CastUtils { 20 /** 21 * Casts a long to an int. In many cases I use a long for a UInt32 but this cannot be used to allocate 22 * ByteBuffers or arrays since they restricted to <code>Integer.MAX_VALUE</code> this cast-method will throw 23 * a RuntimeException if the cast would cause a loss of information. 24 * 25 * @param l the long value 26 * @return the long value as int 27 */ l2i(long l)28 public static int l2i(long l) { 29 if (l > Integer.MAX_VALUE || l < Integer.MIN_VALUE) { 30 throw new RuntimeException("A cast to int has gone wrong. Please contact the mp4parser discussion group (" + l + ")"); 31 } 32 return (int) l; 33 } 34 } 35