blobfilevalue.java

来自「jsr170接口的java实现。是个apache的开源项目。」· Java 代码 · 共 549 行 · 第 1/2 页

JAVA
549
字号
     * calling this method will have no effect.     *     * @see #delete()     * @see #delete(boolean)     */    public void discard() {        if (!temp) {            // do nothing if this instance is not backed by temporarily            // allocated resource/buffer            return;        }        if (file != null) {            // this instance is backed by a temp file            file.delete();        } else if (buffer != null) {            // this instance is backed by an in-memory buffer            buffer = EMPTY_BYTE_ARRAY;        }    }    /**     * Deletes the persistent resource backing this <code>BLOBFileValue</code>.     * Same as <code>{@link #delete(false)}</code>.     * <p/>     * If this <code>BLOBFileValue</code> is <i>not</i> backed by a persistent     * resource calling this method will have no effect.     *     * @see #discard()     */    public void delete() {        if (!temp) {            delete(false);        }    }    /**     * Deletes the persistent resource backing this <code>BLOBFileValue</code>.     *     * @param pruneEmptyParentDirs if <code>true</code>, empty parent directories     *                             will automatically be deleted     */    public void delete(boolean pruneEmptyParentDirs) {        if (file != null) {            // this instance is backed by a 'real' file            file.delete();            if (pruneEmptyParentDirs) {                // prune empty parent directories                File parent = file.getParentFile();                while (parent != null && parent.delete()) {                    parent = parent.getParentFile();                }            }        } else if (fsResource != null) {            // this instance is backed by a resource in the virtual file system            try {                fsResource.delete(pruneEmptyParentDirs);            } catch (FileSystemException fse) {                // ignore                log.warn("Error while deleting BLOBFileValue: " + fse.getMessage());            }        } else {            // this instance is backed by an in-memory buffer            buffer = EMPTY_BYTE_ARRAY;        }    }    /**     * Spools the contents of this <code>BLOBFileValue</code> to the given     * output stream.     *     * @param out output stream     * @throws RepositoryException if the input stream for this     *                             <code>BLOBFileValue</code> could not be obtained     * @throws IOException         if an error occurs while while spooling     */    public void spool(OutputStream out) throws RepositoryException, IOException {        InputStream in;        if (file != null) {            // this instance is backed by a 'real' file            try {                in = new FileInputStream(file);            } catch (FileNotFoundException fnfe) {                throw new RepositoryException("file backing binary value not found",                        fnfe);            }        } else if (fsResource != null) {            // this instance is backed by a resource in the virtual file system            try {                in = fsResource.getInputStream();            } catch (FileSystemException fse) {                throw new RepositoryException(fsResource.getPath()                        + ": the specified resource does not exist", fse);            }        } else {            // this instance is backed by an in-memory buffer            in = new ByteArrayInputStream(buffer);        }        try {            byte[] buffer = new byte[0x2000];            int read;            while ((read = in.read(buffer)) > 0) {                out.write(buffer, 0, read);            }        } finally {            try {                in.close();            } catch (IOException ignore) {            }        }    }    //-------------------------------------------< java.lang.Object overrides >    /**     * Returns a string representation of this <code>BLOBFileValue</code>     * instance. The string representation of a resource backed value is     * the path of the underlying resource. If this instance is backed by an     * in-memory buffer the generic object string representation of the byte     * array will be used instead.     *     * @return A string representation of this <code>BLOBFileValue</code> instance.     */    public String toString() {        if (file != null) {            // this instance is backed by a 'real' file            return file.toString();        } else if (fsResource != null) {            // this instance is backed by a resource in the virtual file system            return fsResource.toString();        } else {            // this instance is backed by an in-memory buffer            return buffer.toString();        }    }    /**     * {@inheritDoc}     */    public boolean equals(Object obj) {        if (this == obj) {            return true;        }        if (obj instanceof BLOBFileValue) {            BLOBFileValue other = (BLOBFileValue) obj;            return ((file == null ? other.file == null : file.equals(other.file))                    && (fsResource == null ? other.fsResource == null : fsResource.equals(other.fsResource))                    && Arrays.equals(buffer, other.buffer));        }        return false;    }    /**     * Returns zero to satisfy the Object equals/hashCode contract.     * This class is mutable and not meant to be used as a hash key.     *     * @return always zero     * @see Object#hashCode()     */    public int hashCode() {        return 0;    }    //----------------------------------------------------------------< Value >    /**     * {@inheritDoc}     */    public int getType() {        return TYPE;    }    /**     * {@inheritDoc}     */    public String getString()            throws ValueFormatException, IllegalStateException,            RepositoryException {        if (text == null) {            ByteArrayOutputStream out = new ByteArrayOutputStream();            try {                spool(out);                byte[] data = out.toByteArray();                text = new String(data, DEFAULT_ENCODING);            } catch (UnsupportedEncodingException e) {                throw new RepositoryException(DEFAULT_ENCODING                        + " not supported on this platform", e);            } catch (IOException e) {                throw new ValueFormatException("conversion from stream to string failed", e);            } finally {                try {                    out.close();                } catch (IOException e) {                    // ignore                }            }        }        return text;    }    /**     * {@inheritDoc}     */    public InputStream getStream()            throws IllegalStateException, RepositoryException {        // always return a 'fresh' stream        if (file != null) {            // this instance is backed by a 'real' file            try {                return new FileInputStream(file);            } catch (FileNotFoundException fnfe) {                throw new RepositoryException("file backing binary value not found",                        fnfe);            }        } else if (fsResource != null) {            // this instance is backed by a resource in the virtual file system            try {                return fsResource.getInputStream();            } catch (FileSystemException fse) {                throw new RepositoryException(fsResource.getPath()                        + ": the specified resource does not exist", fse);            }        } else {            return new ByteArrayInputStream(buffer);        }    }    /**     * {@inheritDoc}     */    public double getDouble()            throws ValueFormatException, IllegalStateException,            RepositoryException {        try {            return Double.parseDouble(getString());        } catch (NumberFormatException e) {            throw new ValueFormatException("conversion to double failed", e);        }    }    /**     * {@inheritDoc}     */    public Calendar getDate()            throws ValueFormatException, IllegalStateException,            RepositoryException {        Calendar cal = ISO8601.parse(getString());        if (cal != null) {            return cal;        } else {            throw new ValueFormatException("not a valid date format");        }    }    /**     * {@inheritDoc}     */    public long getLong()            throws ValueFormatException, IllegalStateException,            RepositoryException {        try {            return Long.parseLong(getString());        } catch (NumberFormatException e) {            throw new ValueFormatException("conversion to long failed", e);        }    }    /**     * {@inheritDoc}     */    public boolean getBoolean()            throws ValueFormatException, IllegalStateException,            RepositoryException {        return Boolean.valueOf(getString()).booleanValue();    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?