1 /** 2 * Copyright (c) 2004-2011 QOS.ch 3 * All rights reserved. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining 6 * a copy of this software and associated documentation files (the 7 * "Software"), to deal in the Software without restriction, including 8 * without limitation the rights to use, copy, modify, merge, publish, 9 * distribute, sublicense, and/or sell copies of the Software, and to 10 * permit persons to whom the Software is furnished to do so, subject to 11 * the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be 14 * included in all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 * 24 */ 25 package org.slf4j.profiler; 26 27 public class SpacePadder { 28 public static final String LINE_SEP = System.getProperty("line.separator"); 29 30 final static String[] SPACES = { " ", " ", " ", " ", // 1,2,4,8 31 // spaces 32 " ", // 16 spaces 33 " " }; // 32 spaces 34 35 @Deprecated leftPad(StringBuffer buf, String s, int desiredLength)36 final static public void leftPad(StringBuffer buf, String s, int desiredLength) { 37 int actualLen = 0; 38 if (s != null) { 39 actualLen = s.length(); 40 } 41 if (actualLen < desiredLength) { 42 spacePad(buf, desiredLength - actualLen); 43 } 44 if (s != null) { 45 buf.append(s); 46 } 47 } 48 leftPad(StringBuilder buf, String s, int desiredLength)49 final static public void leftPad(StringBuilder buf, String s, int desiredLength) { 50 int actualLen = 0; 51 if (s != null) { 52 actualLen = s.length(); 53 } 54 if (actualLen < desiredLength) { 55 spacePad(buf, desiredLength - actualLen); 56 } 57 if (s != null) { 58 buf.append(s); 59 } 60 } 61 62 @Deprecated rightPad(StringBuffer buf, String s, int desiredLength)63 final static public void rightPad(StringBuffer buf, String s, int desiredLength) { 64 int actualLen = 0; 65 if (s != null) { 66 actualLen = s.length(); 67 } 68 if (s != null) { 69 buf.append(s); 70 } 71 if (actualLen < desiredLength) { 72 spacePad(buf, desiredLength - actualLen); 73 } 74 } 75 rightPad(StringBuilder buf, String s, int desiredLength)76 final static public void rightPad(StringBuilder buf, String s, int desiredLength) { 77 int actualLen = 0; 78 if (s != null) { 79 actualLen = s.length(); 80 } 81 if (s != null) { 82 buf.append(s); 83 } 84 if (actualLen < desiredLength) { 85 spacePad(buf, desiredLength - actualLen); 86 } 87 } 88 89 /** 90 * Fast space padding method. 91 * 92 * @param sbuf the buffer to pad 93 * @param length the target size of the buffer after padding 94 */ 95 @Deprecated spacePad(StringBuffer sbuf, int length)96 final static public void spacePad(StringBuffer sbuf, int length) { 97 while (length >= 32) { 98 sbuf.append(SPACES[5]); 99 length -= 32; 100 } 101 102 for (int i = 4; i >= 0; i--) { 103 if ((length & (1 << i)) != 0) { 104 sbuf.append(SPACES[i]); 105 } 106 } 107 } 108 109 /** 110 * Fast space padding method. 111 * 112 * @param sbuf the buffer to pad 113 * @param length the target size of the buffer after padding 114 */ spacePad(StringBuilder sbuf, int length)115 final static public void spacePad(StringBuilder sbuf, int length) { 116 while (length >= 32) { 117 sbuf.append(SPACES[5]); 118 length -= 32; 119 } 120 121 for (int i = 4; i >= 0; i--) { 122 if ((length & (1 << i)) != 0) { 123 sbuf.append(SPACES[i]); 124 } 125 } 126 } 127 } 128