View Javadoc

1   /**
2    * Logback: the generic, reliable, fast and flexible logging framework.
3    * 
4    * Copyright (C) 1999-2008, QOS.ch
5    * 
6    * This library is free software, you can redistribute it and/or modify it under
7    * the terms of the GNU Lesser General Public License as published by the Free
8    * Software Foundation.
9    */
10  package ch.qos.logback.core.pattern;
11  
12  public class SpacePadder {
13  
14    final static String[] SPACES = { " ", "  ", "    ", "        ", // 1,2,4,8
15        // spaces
16        "                ", // 16 spaces
17        "                                " }; // 32 spaces
18  
19    final static public void leftPad(StringBuffer buf, String s, int desiredLength) {
20      int actualLen = 0;
21      if (s != null) {
22        actualLen = s.length();
23      }
24      if (actualLen < desiredLength) {
25        spacePad(buf, desiredLength - actualLen);
26      }
27      if (s != null) {
28        buf.append(s);
29      }
30    }
31  
32    final static public void rightPad(StringBuffer buf, String s, int desiredLength) {
33      int actualLen = 0;
34      if (s != null) {
35        actualLen = s.length();
36      }
37      if (s != null) {
38        buf.append(s);
39      }
40      if (actualLen < desiredLength) {
41        spacePad(buf, desiredLength - actualLen);
42      }
43    }
44    
45    /**
46     * Fast space padding method.
47     */
48    final static public void spacePad(StringBuffer sbuf, int length) {
49      while (length >= 32) {
50        sbuf.append(SPACES[5]);
51        length -= 32;
52      }
53  
54      for (int i = 4; i >= 0; i--) {
55        if ((length & (1 << i)) != 0) {
56          sbuf.append(SPACES[i]);
57        }
58      }
59    }
60  }