List of usage examples for org.apache.wicket.util.lang Bytes bytes
bytes
From source file:com.locke.library.utilities.io.stream.Streams.java
License:Apache License
/** * Writes the input stream to the output stream. Input is done without a * Reader object, meaning that the input is copied in its raw form. * /* www.jav a 2s .com*/ * @param in * The input stream * @param out * The output stream * @return Number of bytes copied from one stream to the other * @throws IOException */ public static int copy(final InputStream in, final OutputStream out, final Bytes maximum) throws IOException, StreamTooLongException { final byte[] buffer = new byte[4096]; int bytesCopied = 0; while (true) { int byteCount = in.read(buffer, 0, buffer.length); if (byteCount <= 0) { break; } out.write(buffer, 0, byteCount); bytesCopied += byteCount; if (bytesCopied > maximum.bytes()) { throw new StreamTooLongException(); } } return bytesCopied; }
From source file:eu.esdihumboldt.hale.server.projects.war.components.UploadForm.java
License:Open Source License
/** * Convert {@link Bytes} to a string, produces a prettier output than * {@link Bytes#toString(Locale)}//from w ww .j a va 2 s. c o m * * @param bytes the bytes * @param locale the locale * * @return the converted string */ public static String bytesToString(Bytes bytes, Locale locale) { if (bytes.bytes() >= 0) { if (bytes.terabytes() >= 1.0) { return unitString(bytes.terabytes(), "TB", locale); } if (bytes.gigabytes() >= 1.0) { return unitString(bytes.gigabytes(), "GB", locale); } if (bytes.megabytes() >= 1.0) { return unitString(bytes.megabytes(), "MB", locale); } if (bytes.kilobytes() >= 1.0) { return unitString(bytes.kilobytes(), "KB", locale); } return Long.toString(bytes.bytes()) + " bytes"; } else { return "N/A"; } }
From source file:org.hippoecm.frontend.plugins.console.editor.BinaryEditor.java
License:Apache License
private static String getSizeString(final Bytes bytes) { if (bytes == null) { return "unknown size"; }//from w ww. ja v a2s .c om long length = bytes.bytes(); String sizeString; if (length / ONE_GB > 0) { sizeString = String.valueOf(length / ONE_GB) + " GB"; } else if (length / ONE_MB > 0) { sizeString = String.valueOf(length / ONE_MB) + " MB"; } else if (length / ONE_KB > 0) { sizeString = String.valueOf(length / ONE_KB) + " KB"; } else { sizeString = String.valueOf(length) + " bytes"; } return sizeString; }
From source file:org.hippoecm.frontend.plugins.standards.diff.DefaultHtmlDiffService.java
License:Apache License
@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 {/*w w w .j av a 2 s . c o m*/ 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; }
From source file:org.xaloon.wicket.component.uploadify.UploadifyBehaviorItem.java
License:Apache License
public void setSizeLimit(Bytes limit) { addOrReplace("sizeLimit", String.valueOf(limit.bytes())); }