org.apache.wicket.util.lang.Bytes类的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(8.9k)|赞(0)|评价(0)|浏览(84)

本文整理了Java中org.apache.wicket.util.lang.Bytes类的一些代码示例,展示了Bytes类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Bytes类的具体详情如下:
包路径:org.apache.wicket.util.lang.Bytes
类名称:Bytes

Bytes介绍

[英]Represents an immutable byte count. These static factory methods allow easy construction of value objects using either long values like bytes(2034) or megabytes(3):

  • Bytes.bytes(long)
  • Bytes.kilobytes(long)
  • Bytes.megabytes(long)
  • Bytes.gigabytes(long)
  • Bytes.terabytes(long)

or double precision floating point values like megabytes(3.2):

  • Bytes.bytes(double)
  • Bytes.kilobytes(double)
  • Bytes.megabytes(double)
  • Bytes.gigabytes(double)
  • Bytes.terabytes(double)

In the case of bytes(double), the value will be rounded off to the nearest integer byte count using Math.round().

The precise number of bytes in a Bytes object can be retrieved by calling bytes(). Approximate values for different units can be retrieved as double precision values using these methods:

  • kilobytes()
  • megabytes()
  • gigabytes()
  • terabytes()

Also, value objects can be constructed from strings, optionally using a Locale with valueOf(String) and valueOf(String,Locale). The string may contain a decimal or floating point number followed by optional whitespace followed by a unit (nothing for bytes, K for kilobyte, M for megabytes, G for gigabytes or T for terabytes) optionally followed by a B (for bytes). Any of these letters can be any case. So, examples of permissible string values are:

  • 37 (37 bytes)
  • 2.3K (2.3 kilobytes)
  • 2.5 kb (2.5 kilobytes)
  • 4k (4 kilobytes)
  • 35.2GB (35.2 gigabytes)
  • 1024M (1024 megabytes)

Note that if the Locale was not US, the values might substitute "," for "." as that is the custom in Euroland.

The toString() and toString(Locale) methods are smart enough to convert a given value object to the most appropriate units for the given value.
[中]表示不可变的字节计数。这些静态工厂方法允许使用长值(如字节(2034)或兆字节(3))轻松构建值对象:
*字节。字节(长)
*字节。千字节(长)
*字节。兆字节(长)
*字节。千兆字节(长)
*字节。TB(长)
或双精度浮点值,如兆字节(3.2):
*字节。字节(双字节)
*字节。千字节(双字节)
*字节。兆字节(双字节)
*字节。千兆字节(双字节)
*字节。TB(双字节)
如果是字节(双精度),则使用Math将该值四舍五入到最接近的整数字节计数。圆形()。
可以通过调用bytes()来检索bytes对象中的精确字节数。使用以下方法,可以将不同单位的近似值检索为双精度值:
*千字节()
*兆字节()
*千兆字节()
*TB()
此外,还可以从字符串构造值对象,可以选择使用valueOf(String)和valueOf(String,Locale)的区域设置。该字符串可能包含一个十进制或浮点数,后跟可选的空格,后跟一个单位(无表示字节,K表示千字节,M表示兆字节,G表示千兆字节或T表示兆字节),可选地后跟一个B(表示字节)。这些字母中的任何一个都可以是任意大小写。因此,允许的字符串值示例如下:
*37(37字节)
*2.3K(2.3KB)
*2.5 kb(2.5 kb)
*4k(4KB)
*35.2GB(35.2GB)
*1024M(1024兆字节)
请注意,如果区域设置不是US,则这些值可能会替换为“,”for“这是欧洲大陆的习俗。
toString()和toString(Locale)方法足够聪明,可以将给定值对象转换为给定值的最合适单位。

代码示例

代码示例来源:origin: org.ops4j.pax.wicket/pax-wicket-service

/**
 * @return human readable string of total number of bytes
 */
public String getTotalBytesString()
{
  return Bytes.bytes(totalBytes).toString();
}

代码示例来源:origin: com.giffing.wicket.spring.boot.starter/wicket-spring-boot-context

public static Bytes parse(Long size, SessionUnit sessionUnit){
  switch(sessionUnit){
  case BYTES:
    return Bytes.bytes(size);
  case KILOBYTES:
    return Bytes.kilobytes(size);
  case MEGABYTES:
    return Bytes.megabytes(size);
  case TERABYTES:
    return Bytes.terabytes(size);
  }
  throw new WicketSpringBootException("Could not parse size with session unit " + size + " " + sessionUnit);
}

代码示例来源:origin: org.apache.wicket/wicket-util

/**
 * Converts a string to a number of bytes. Strings consist of a floating point value followed by
 * K, M, G or T for kilobytes, megabytes, gigabytes or terabytes, respectively. The
 * abbreviations KB, MB, GB and TB are also accepted. Matching is case insensitive.
 * 
 * @param string
 *            The string to convert
 * @return The Bytes value for the string
 * @throws StringValueConversionException
 */
public static Bytes valueOf(final String string) throws StringValueConversionException
{
  return valueOf(string, Locale.getDefault(Locale.Category.FORMAT));
}

代码示例来源:origin: org.ops4j.pax.wicket/pax-wicket-service

/**
 * Creates a new {@link DiskPageStore} instance.
 */
public DiskPageStore()
{
  this((int)Bytes.megabytes(10).bytes(), (int)Bytes.megabytes(100).bytes(), 50);
}

代码示例来源:origin: org.apache.wicket/wicket-core

@Override
public Bytes length()
{
  return Bytes.bytes(length);
}

代码示例来源:origin: org.apache.wicket/wicket-util

/**
 * Gets the byte count in gigabytes.
 * 
 * @return The value in gigabytes
 */
public final double gigabytes()
{
  return megabytes() / 1024.0;
}

代码示例来源:origin: org.onehippo.cms7/hippo-cms-api

private void validateMaxFileSize(final FileUpload upload) {
  Bytes fileSize = Bytes.bytes(upload.getSize());
  final Bytes maxFileSize = Bytes.valueOf(values.getString(MAX_FILE_SIZE, getDefaultMaxFileSize()));
  if (maxFileSize.compareTo(fileSize) == -1) {
    addViolation("file.validation.size",
        upload.getClientFileName(), fileSize.toString(), maxFileSize.toString());
    if (log.isDebugEnabled()) {
      log.debug("File '{}' has size {} which is too big. The maximum size allowed is {}",
           upload.getClientFileName(), fileSize.toString(), maxFileSize.toString());
    }
  }
}

代码示例来源:origin: org.apache.wicket/wicket-util

/**
 * Converts this byte count to a string using the given locale.
 * 
 * @param locale
 *            Locale to use for conversion
 * @return The string for this byte count
 */
public String toString(final Locale locale)
{
  if (terabytes() >= 1.0)
  {
    return unitString(terabytes(), "TB", locale);
  }
  if (gigabytes() >= 1.0)
  {
    return unitString(gigabytes(), "GB", locale);
  }
  if (megabytes() >= 1.0)
  {
    return unitString(megabytes(), "MB", locale);
  }
  if (kilobytes() >= 1.0)
  {
    return unitString(kilobytes(), "KB", locale);
  }
  return Long.toString(value) + " bytes";
}

代码示例来源:origin: org.onehippo.cms7/hippo-cms-api

@Override
  public String diff(final String originalValue, final String currentValue) {
    Bytes maxDiffSize = Bytes.valueOf(params.getString(MAX_DIFF_SIZE, DEFAULT_MAX_DIFF_SIZE));
    final int maxLenSize = Math.max(originalValue.length(), currentValue.length());

    try {
      final ByteArrayOutputStream baos = new ByteArrayOutputStream();
      if (maxLenSize <= maxDiffSize.bytes()){
        DiffHelper.diffHtml(originalValue, currentValue, new StreamResult(baos),
            Session.get().getLocale());
        return baos.toString("UTF-8");
      } else {
        log.warn("Unable to diff a large content of size {} KB, which is exceeds the limitation {} KB ",
            Bytes.bytes(maxLenSize).kilobytes(), maxDiffSize.kilobytes());
      }
    } catch (TransformerConfigurationException e) {
      log.error(e.getMessage(), e);
    } catch (SAXException e) {
      log.info(e.getMessage(), e);
    } catch (IOException e) {
      log.error(e.getMessage());
    }
    return null;
  }
}

代码示例来源:origin: micromata/projectforge

this.setOutputMarkupId(true);
Bytes maxSize = Bytes.valueOf(configurationService.getMaxFileSizeDatev());
this.setMaxSize(maxSize);
String hint = I18nHelper.getLocalizedMessage("finance.datev.upload.hint", NumberHelper.formatBytes(maxSize.bytes()));
final FieldsetPanel fs = gridBuilder.newFieldset(getString("file"), hint);
fileUploadField = new FileUploadField(FileUploadPanel.WICKET_ID);

代码示例来源:origin: apache/wicket

@Override
public String getObject()
{
  Bytes sessionSizeInBytes = size.getObject();
  String sessionSizeAsString = sessionSizeInBytes != null
    ? sessionSizeInBytes.toString() : "unknown";
  return "Session: " + sessionSizeAsString;
}

代码示例来源:origin: org.apache.wicket/wicket-util

/**
 * Instantiate immutable Bytes value object..
 * 
 * @param megabytes
 *            Value to convert
 * @return Input as Bytes
 */
public static Bytes megabytes(final double megabytes)
{
  return kilobytes(megabytes * 1024.0);
}

代码示例来源:origin: org.onehippo.cms7/hippo-cms-api

/**
 * Check if the defaultMaximumUploadSize stored in the IApplicationSettings is set explicitly and only
 * then used it, otherwise use DEFAULT_MAX_FILE_SIZE. This is because it is set to Bytes.MAX
 * by default which is a bit overkill (8388608T).
 *
 * @return The String value of the default maximum file size for an upload
 */
protected String getDefaultMaxFileSize() {
  IApplicationSettings settings = Application.get().getApplicationSettings();
  Bytes defaultSize = settings.getDefaultMaximumUploadSize();
  return Bytes.MAX.equals(defaultSize) ? DEFAULT_MAX_FILE_SIZE : defaultSize.toString();
}

代码示例来源:origin: org.apache.wicket/wicket-util

/**
 * Instantiate immutable Bytes value object..
 * 
 * @param kilobytes
 *            Value to convert
 * @return Input as Bytes
 */
public static Bytes kilobytes(final long kilobytes)
{
  return bytes(kilobytes * 1024);
}

代码示例来源:origin: org.apache.wicket/com.springsource.org.apache.wicket

/**
 * Creates a new {@link DiskPageStore} instance.
 */
public DiskPageStore()
{
  this((int)Bytes.megabytes(10).bytes(), (int)Bytes.megabytes(100).bytes(), 50);
}

代码示例来源:origin: org.apache.wicket/wicket-util

/**
 * Instantiate immutable Bytes value object..
 * 
 * @param gigabytes
 *            Value to convert
 * @return Input as Bytes
 */
public static Bytes gigabytes(final double gigabytes)
{
  return megabytes(gigabytes * 1024.0);
}

代码示例来源:origin: org.apache.wicket/com.springsource.org.apache.wicket

if (terabytes() >= 1.0)
  return unitString(terabytes(), "T", locale);
if (gigabytes() >= 1.0)
  return unitString(gigabytes(), "G", locale);
if (megabytes() >= 1.0)
  return unitString(megabytes(), "M", locale);
if (kilobytes() >= 1.0)
  return unitString(kilobytes(), "K", locale);

代码示例来源:origin: org.apache.wicket/com.springsource.org.apache.wicket

/**
 * Converts this byte count to a string using the default locale.
 * 
 * @return The string for this byte count
 */
public String toString()
{
  return toString(Locale.getDefault());
}

代码示例来源:origin: org.apache.wicket/wicket-util

/**
 * Gets the byte count in megabytes.
 * 
 * @return The value in megabytes
 */
public final double megabytes()
{
  return kilobytes() / 1024.0;
}

代码示例来源:origin: org.ops4j.pax.wicket/pax-wicket-service

/**
 * @return human readable string of bytes uploaded so far
 */
public String getBytesUploadedString()
{
  return Bytes.bytes(bytesUploaded).toString();
}

相关文章