View Javadoc

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    final static public void leftPad(StringBuffer buf, String s, int desiredLength) {
36      int actualLen = 0;
37      if (s != null) {
38        actualLen = s.length();
39      }
40      if (actualLen < desiredLength) {
41        spacePad(buf, desiredLength - actualLen);
42      }
43      if (s != null) {
44        buf.append(s);
45      }
46    }
47  
48    final static public void rightPad(StringBuffer buf, String s, int desiredLength) {
49      int actualLen = 0;
50      if (s != null) {
51        actualLen = s.length();
52      }
53      if (s != null) {
54        buf.append(s);
55      }
56      if (actualLen < desiredLength) {
57        spacePad(buf, desiredLength - actualLen);
58      }
59    }
60    
61    /**
62     * Fast space padding method.
63     */
64    final static public void spacePad(StringBuffer sbuf, int length) {
65      while (length >= 32) {
66        sbuf.append(SPACES[5]);
67        length -= 32;
68      }
69  
70      for (int i = 4; i >= 0; i--) {
71        if ((length & (1 << i)) != 0) {
72          sbuf.append(SPACES[i]);
73        }
74      }
75    }
76  }