You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ByteFormat.java 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright 2011 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit.utils;
  17. import java.text.DecimalFormat;
  18. import java.text.FieldPosition;
  19. import java.text.Format;
  20. import java.text.ParsePosition;
  21. /**
  22. * A formatter for formatting byte sizes. For example, formatting 12345 byes
  23. * results in "12.1 K" and 1234567 results in "1.18 MB".
  24. *
  25. */
  26. public class ByteFormat extends Format {
  27. private static final long serialVersionUID = 1L;
  28. public ByteFormat() {
  29. }
  30. // Implemented from the Format class
  31. /**
  32. * Formats a long which represent a number of bytes.
  33. */
  34. public String format(long bytes) {
  35. return format(Long.valueOf(bytes));
  36. }
  37. /**
  38. * Formats a long which represent a number of kilobytes.
  39. */
  40. public String formatKB(long kilobytes) {
  41. return format(Long.valueOf(kilobytes * 1024));
  42. }
  43. /**
  44. * Format the given object (must be a Long).
  45. *
  46. * @param obj
  47. * assumed to be the number of bytes as a Long.
  48. * @param buf
  49. * the StringBuffer to append to.
  50. * @param pos
  51. * @return A formatted string representing the given bytes in more
  52. * human-readable form.
  53. */
  54. public StringBuffer format(Object obj, StringBuffer buf, FieldPosition pos) {
  55. if (obj instanceof Long) {
  56. long numBytes = ((Long) obj).longValue();
  57. if (numBytes < 1024) {
  58. DecimalFormat formatter = new DecimalFormat("#,##0");
  59. buf.append(formatter.format((double) numBytes)).append(" b");
  60. } else if (numBytes < 1024 * 1024) {
  61. DecimalFormat formatter = new DecimalFormat("#,##0.0");
  62. buf.append(formatter.format((double) numBytes / 1024.0)).append(" KB");
  63. } else if (numBytes < 1024 * 1024 * 1024) {
  64. DecimalFormat formatter = new DecimalFormat("#,##0.0");
  65. buf.append(formatter.format((double) numBytes / (1024.0 * 1024.0))).append(" MB");
  66. } else {
  67. DecimalFormat formatter = new DecimalFormat("#,##0.0");
  68. buf.append(formatter.format((double) numBytes / (1024.0 * 1024.0 * 1024.0))).append(" GB");
  69. }
  70. }
  71. return buf;
  72. }
  73. /**
  74. * In this implementation, returns null always.
  75. *
  76. * @param source
  77. * @param pos
  78. * @return returns null in this implementation.
  79. */
  80. public Object parseObject(String source, ParsePosition pos) {
  81. return null;
  82. }
  83. }