bundlebinding.java
来自「jsr170接口的java实现。是个apache的开源项目。」· Java 代码 · 共 660 行 · 第 1/2 页
JAVA
660 行
} catch (IOException e) { if (errorHandling.ignoreMissingBlobs()) { log.warn("Ignoring error while reading blob-resource: " + e); val = InternalValue.create(new byte[0]); } else { throw e; } } catch (Exception e) { throw new IOException("Unable to create property value: " + e.toString()); } } else { // short values into memory byte[] data = new byte[size]; in.readFully(data); val = InternalValue.create(data); } break; case PropertyType.DOUBLE: val = InternalValue.create(in.readDouble()); break; case PropertyType.LONG: val = InternalValue.create(in.readLong()); break; case PropertyType.BOOLEAN: val = InternalValue.create(in.readBoolean()); break; case PropertyType.NAME: val = InternalValue.create(readQName(in)); break; case PropertyType.REFERENCE: val = InternalValue.create(readUUID(in)); break; default: // because writeUTF(String) has a size limit of 64k, // Strings are serialized as <length><byte[]> int len = in.readInt(); byte[] bytes = new byte[len]; int pos = 0; while (pos<len) { pos+= in.read(bytes, pos, len-pos); } val = InternalValue.valueOf(new String(bytes, "UTF-8"), type); } values[i] = val; } entry.setValues(values); entry.setBlobIds(blobIds); return entry; } /** * Checks a <code>PropertyState</code> from the data input stream. * * @param in the input stream * @return <code>true</code> if the data is valid; * <code>false</code> otherwise. */ public boolean checkPropertyState(DataInputStream in) { int type; try { type = in.readInt(); short modCount = (short) ((type >> 16) | 0xffff); type &= 0xffff; log.info(" PropertyType: " + PropertyType.nameFromValue(type)); log.info(" ModCount: " + modCount); } catch (IOException e) { log.error("Error while reading property type: " + e); return false; } try { boolean isMV = in.readBoolean(); log.info(" MultiValued: " + isMV); } catch (IOException e) { log.error("Error while reading multivalued: " + e); return false; } try { String defintionId = in.readUTF(); log.info(" DefinitionId: " + defintionId); } catch (IOException e) { log.error("Error while reading definition id: " + e); return false; } int count; try { count = in.readInt(); log.info(" num values: " + count); } catch (IOException e) { log.error("Error while reading number of values: " + e); return false; } for (int i = 0; i < count; i++) { switch (type) { case PropertyType.BINARY: int size; try { size = in.readInt(); log.info(" binary size: " + size); } catch (IOException e) { log.error("Error while reading size of binary: " + e); return false; } if (size == -1) { try { String s = in.readUTF(); log.info(" blobid: " + s); } catch (IOException e) { log.error("Error while reading blob id: " + e); return false; } } else { // short values into memory byte[] data = new byte[size]; try { in.readFully(data); log.info(" binary: " + data.length + " bytes"); } catch (IOException e) { log.error("Error while reading inlined binary: " + e); return false; } } break; case PropertyType.DOUBLE: try { double d = in.readDouble(); log.info(" double: " + d); } catch (IOException e) { log.error("Error while reading double value: " + e); return false; } break; case PropertyType.LONG: try { double l = in.readLong(); log.info(" long: " + l); } catch (IOException e) { log.error("Error while reading long value: " + e); return false; } break; case PropertyType.BOOLEAN: try { boolean b = in.readBoolean(); log.info(" boolean: " + b); } catch (IOException e) { log.error("Error while reading boolean value: " + e); return false; } break; case PropertyType.NAME: try { QName name = readQName(in); log.info(" name: " + name); } catch (IOException e) { log.error("Error while reading name value: " + e); return false; } break; case PropertyType.REFERENCE: try { UUID uuid = readUUID(in); log.info(" reference: " + uuid); } catch (IOException e) { log.error("Error while reading reference value: " + e); return false; } break; default: // because writeUTF(String) has a size limit of 64k, // Strings are serialized as <length><byte[]> int len; try { len = in.readInt(); log.info(" size of string value: " + len); } catch (IOException e) { log.error("Error while reading size of string value: " + e); return false; } try { byte[] bytes = new byte[len]; int pos = 0; while (pos<len) { pos+= in.read(bytes, pos, len-pos); } log.info(" string: " + new String(bytes, "UTF-8")); } catch (IOException e) { log.error("Error while reading string value: " + e); return false; } } } return true; } /** * Serializes a <code>PropertyState</code> to the data output stream * * @param out the output stream * @param state the property entry to store * @throws IOException if an I/O error occurs. */ public void writeState(DataOutputStream out, NodePropBundle.PropertyEntry state) throws IOException { // type & mod count out.writeInt(state.getType() | (state.getModCount() << 16)); // multiValued out.writeBoolean(state.isMultiValued()); // definitionId out.writeUTF(state.getPropDefId().toString()); // values InternalValue[] values = state.getValues(); out.writeInt(values.length); // count for (int i = 0; i < values.length; i++) { InternalValue val = values[i]; switch (state.getType()) { case PropertyType.BINARY: // special handling required for binary value: // spool binary value to file in blob store BLOBFileValue blobVal = (BLOBFileValue) val.internalValue(); long size = blobVal.getLength(); if (size < 0) { log.warn("Blob has negative size. Potential loss of data. " + "id={} idx={}", state.getId(), String.valueOf(i)); out.writeInt(0); values[i] = InternalValue.create(new byte[0]); blobVal.discard(); } else if (size > minBlobSize) { out.writeInt(-1); String blobId = state.getBlobId(i); if (blobId == null) { try { InputStream in = blobVal.getStream(); try { blobId = blobStore.createId(state.getId(), i); blobStore.put(blobId, in, size); state.setBlobId(blobId, i); } finally { try { in.close(); } catch (IOException e) { // ignore } } } catch (Exception e) { String msg = "Error while storing blob. id=" + state.getId() + " idx=" + i + " size=" + size; log.error(msg, e); throw new IOException(msg); } try { // replace value instance with value // backed by resource in blob store and delete temp file if (blobStore instanceof ResourceBasedBLOBStore) { values[i] = InternalValue.create(((ResourceBasedBLOBStore) blobStore).getResource(blobId)); } else { values[i] = InternalValue.create(blobStore.get(blobId), false); } } catch (Exception e) { log.error("Error while reloading blob. truncating. id=" + state.getId() + " idx=" + i + " size=" + size, e); values[i] = InternalValue.create(new byte[0]); } blobVal.discard(); } // store id of blob as property value out.writeUTF(blobId); // value } else { // delete evt. blob out.writeInt((int) size); byte[] data = new byte[(int) size]; try { InputStream in = blobVal.getStream(); try { int pos = 0; while (pos < size) { int n = in.read(data, pos, (int) size-pos); if (n < 0) { throw new EOFException(); } pos += n; } } finally { try { in.close(); } catch (IOException e) { // ignore } } } catch (Exception e) { String msg = "Error while storing blob. id=" + state.getId() + " idx=" + i + " size=" + size; log.error(msg, e); throw new IOException(msg); } out.write(data, 0, data.length); // replace value instance with value // backed by resource in blob store and delete temp file values[i] = InternalValue.create(data); blobVal.discard(); } break; case PropertyType.DOUBLE: out.writeDouble(((Double) val.internalValue()).doubleValue()); break; case PropertyType.LONG: out.writeLong(((Long) val.internalValue()).longValue()); break; case PropertyType.BOOLEAN: out.writeBoolean(((Boolean) val.internalValue()).booleanValue()); break; case PropertyType.NAME: writeQName(out, (QName) val.internalValue()); break; case PropertyType.REFERENCE: writeUUID(out, (UUID) val.internalValue()); break; default: // because writeUTF(String) has a size limit of 64k, // we're using write(byte[]) instead byte[] bytes = val.toString().getBytes("UTF-8"); out.writeInt(bytes.length); // lenght of byte[] out.write(bytes); // byte[] } } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?