001// -------------------------------------------------------------------------------- 002// Copyright 2002-2024 Echo Three, LLC 003// 004// Licensed under the Apache License, Version 2.0 (the "License"); 005// you may not use this file except in compliance with the License. 006// You may obtain a copy of the License at 007// 008// http://www.apache.org/licenses/LICENSE-2.0 009// 010// Unless required by applicable law or agreed to in writing, software 011// distributed under the License is distributed on an "AS IS" BASIS, 012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013// See the License for the specific language governing permissions and 014// limitations under the License. 015// -------------------------------------------------------------------------------- 016 017package com.echothree.util.common.string; 018 019public class Inet4AddressUtils { 020 021 private Inet4AddressUtils() { 022 super(); 023 } 024 025 private static class Inet4AddressUtilsHolder { 026 static Inet4AddressUtils instance = new Inet4AddressUtils(); 027 } 028 029 public static Inet4AddressUtils getInstance() { 030 return Inet4AddressUtilsHolder.instance; 031 } 032 033 // http://darksleep.com/player/JavaAndUnsignedTypes.html 034 public String formatInet4Address(Integer inet4Address) { 035 long rawInet4Address = inet4Address & 0xFFFFFFFFL; 036 int inet4Address0 = (int)(rawInet4Address & 0xFF); 037 int inet4Address1 = (int)((rawInet4Address & 0xFF00) >> 8); 038 int inet4Address2 = (int)((rawInet4Address & 0xFF0000) >> 16); 039 int inet4Address3 = (int)(rawInet4Address >> 24); 040 StringBuilder sb = new StringBuilder().append(inet4Address3).append('.').append(inet4Address2).append('.'). 041 append(inet4Address1).append('.').append(inet4Address0); 042 043 return sb.toString(); 044 } 045 046}