View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.pattern;
15  
16  public class SpacePadder {
17  
18      final static String[] SPACES = { " ", "  ", "    ", "        ", // 1,2,4,8
19              // spaces
20              "                ", // 16 spaces
21              "                                " }; // 32 spaces
22  
23      final static public void leftPad(StringBuilder buf, String s, int desiredLength) {
24          int actualLen = 0;
25          if (s != null) {
26              actualLen = s.length();
27          }
28          if (actualLen < desiredLength) {
29              spacePad(buf, desiredLength - actualLen);
30          }
31          if (s != null) {
32              buf.append(s);
33          }
34      }
35  
36      final static public void rightPad(StringBuilder buf, String s, int desiredLength) {
37          int actualLen = 0;
38          if (s != null) {
39              actualLen = s.length();
40          }
41          if (s != null) {
42              buf.append(s);
43          }
44          if (actualLen < desiredLength) {
45              spacePad(buf, desiredLength - actualLen);
46          }
47      }
48  
49      /**
50       * Fast space padding method.
51       */
52      final static public void spacePad(StringBuilder sbuf, int length) {
53          while (length >= 32) {
54              sbuf.append(SPACES[5]);
55              length -= 32;
56          }
57  
58          for (int i = 4; i >= 0; i--) {
59              if ((length & (1 << i)) != 0) {
60                  sbuf.append(SPACES[i]);
61              }
62          }
63      }
64  }