📄 byteformat.java
字号:
/** * $RCSfile: ByteFormat.java,v $ * $Revision: 1.3 $ * $Date: 2003/01/09 06:05:32 $ * * Copyright (C) 1999-2002 Jive Software. All rights reserved. * * This software is the proprietary information of Jive Software. Use is subject to license terms. */package com.struts2.framework.util;import java.text.*;/** * A formatter for formatting byte sizes. For example, formatting 12345 byes results in * "12.1 K" and 1234567 results in "1.18 MB". * * @author Bill Lynch */public class ByteFormat extends Format {// private long numBytes = 0; public ByteFormat() {}// public ByteFormat(int numBytes) {// this.numBytes = numBytes;// }//// public long getNumBytes() {// return numBytes;// }//// public void setNumBytes(long numBytes) {// this.numBytes = numBytes;// } // Implemented from the Format class /** * Formats a long which represent a number of bytes. */ public String format(long bytes) { return format(new Long(bytes)); } /** * Formats a long which represent a number of kilobytes. */ public String formatKB(long kilobytes) { return format(new Long(kilobytes*1024)); } /** * Format the given object (must be a Long). * * @param obj assumed to be the number of bytes as a Long. * @param buf the StringBuffer to append to. * @param pos * @return A formatted string representing the given bytes in more human-readable form. */ public StringBuffer format(Object obj, StringBuffer buf, FieldPosition pos) { if (obj instanceof Long) { long numBytes = ((Long)obj).longValue(); if (numBytes < 1024*1024) { DecimalFormat formatter = new DecimalFormat("#,##0.0"); buf.append(formatter.format((double)numBytes/1024.0)).append(" K"); } else { DecimalFormat formatter = new DecimalFormat("#,##0.0"); buf.append(formatter.format((double)numBytes/(1024.0*1024.0))).append(" MB"); } } return buf; } /** * In this implementation, returns null always. * @param source * @param pos * @return returns null in this implementation. */ public Object parseObject(String source, ParsePosition pos) { return null; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -