@Override public int read(byte[] b, int off, int len) throws IOException { if (len == 0) { return 0; } int c = read(); if (c == -1) { return -1; } b[off] = (byte) c; int i = 1; // Don't try to fill up the buffer if the reader is waiting. for (; (i < len) && reader.ready(); i++) { c = read(); if (c == -1) { return i; } b[off + i] = (byte) c; } return i; }
/** Writes an object safely to the object output. * Can be read by {@link NbObjectInputStream#readSafely}. * @param oo object output to write to * @param obj the object to write * @exception SafeException if the object simply fails to be serialized * @exception IOException if something more serious fails */ public static void writeSafely(ObjectOutput oo, Object obj) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(200); try { NbObjectOutputStream oos = new NbObjectOutputStream(bos); oos.writeObject(obj); oos.flush(); bos.close(); } catch (Exception exc) { // exception during safe of the object // encapsulate all exceptions into safe exception oo.writeInt(0); throw new SafeException(exc); } oo.writeInt(bos.size()); oo.write(bos.toByteArray()); }
/** Reads an object from the given object input. * The object had to be saved by the {@link NbObjectOutputStream#writeSafely} method. * * @param oi object input * @return the read object * @exception IOException if IO error occured * @exception SafeException if the operation failed but the stream is ok * for further reading */ public static Object readSafely(ObjectInput oi) throws IOException { int size = oi.readInt(); byte[] byteArray = new byte[size]; oi.readFully(byteArray, 0, size); try { ByteArrayInputStream bis = new ByteArrayInputStream(byteArray); NbObjectInputStream ois = new NbObjectInputStream(bis); Object obj = ois.readObject(); bis.close(); return obj; } catch (Exception exc) { // encapsulate all exceptions into safe exception throw new SafeException(exc); } catch (LinkageError le) { throw new SafeException(new InvocationTargetException(le)); } }
org.openide.util.io.NbMarshalledObject m = new org.openide.util.io.NbMarshalledObject(this); return (ServiceType) m.get(); } catch (IOException ex) { anEx = ex;
ObjectInputStream ois = new NbObjectInputStream(bin);
/** * added for future use - convert NbMarshalledObject to primitive data types and other supported types (if possible) */ static Object unMarshallObjectRecursively(Object mo) { Object o = mo; while (o instanceof NbMarshalledObject) { try { o = ((NbMarshalledObject) o).get(); } catch (IOException e) { ExternalUtil.exception(e); return mo; } catch (ClassNotFoundException e) { ExternalUtil.exception(e); return mo; } } return (o == null) ? mo : o; }
/** Reads external. */ public void readExternal(ObjectInput oi) throws IOException, ClassNotFoundException { for (;;) { String file = (String) oi.readObject(); if (file == null) { break; } for (;;) { String attr = (String) oi.readObject(); if (attr == null) { break; } Object o = oi.readObject(); // backward compatibility if (o instanceof java.rmi.MarshalledObject) { o = ((java.rmi.MarshalledObject) o).get(); o = new NbMarshalledObject(o); } // end of backward compatibility if (o instanceof NbMarshalledObject) { setAttr(file, attr, o); } } } } }
/** * Creates a new <code>NbMarshalledObject</code> that contains the * serialized representation of the provided object. * * @param obj the object to be serialized (must be serializable) * @exception IOException the object is not serializable */ public NbMarshalledObject(Object obj) throws IOException { if (obj == null) { hash = 17; return; } ByteArrayOutputStream bout = new ByteArrayOutputStream(); ObjectOutputStream out = new NbObjectOutputStream(bout); out.writeObject(obj); out.flush(); objBytes = bout.toByteArray(); int h = 0; for (int i = 0; i < objBytes.length; i++) { h = (37 * h) + objBytes[i]; } hash = h; }
protected Class resolveClass(ObjectStreamClass v) throws IOException, ClassNotFoundException { ClassLoader cl = getNBClassLoader(); try { return Class.forName(v.getName(), false, cl); } catch (ClassNotFoundException cnfe) { String msg = "Offending classloader: " + cl; // NOI18N Exceptions.attachMessage(cnfe, msg); throw cnfe; } }
/** Create a new object input. * @param is underlying input stream * @throws IOException for the usual reasons */ public NbObjectInputStream(InputStream is) throws IOException { super(is); try { enableResolveObject(true); } catch (SecurityException ex) { throw new IOException(ex.toString()); } }
/** Create a new object output. * @param os the underlying output stream * @throws IOException for the usual reasons */ public NbObjectOutputStream(OutputStream os) throws IOException { super(os); try { enableReplaceObject(true); } catch (SecurityException ex) { throw (IOException) new IOException(ex.toString()).initCause(ex); } }
if (isSerialVersionUIDDeclared(cl)) { return;
public MultiplexPrintStream(boolean err) { this(new NullOutputStream(), err); }
org.openide.util.io.NbMarshalledObject m = new org.openide.util.io.NbMarshalledObject(this); return (ServiceType) m.get(); } catch (IOException ex) { anEx = ex;
ObjectInputStream ois = new NbObjectInputStream(bis); Object ret = ois.readObject();
try { NbMarshalledObject mar = (NbMarshalledObject) obj; fs = (FileSystem) mar.get(); } catch (IOException ex) { ExternalUtil.exception(ex);
/** Saves pool to stream by saving all filesystems. * The default (system) filesystem, or any persistent filesystems, are skipped. * * @param oos object output stream * @exception IOException if an error occures * @deprecated Unused. */ @Deprecated public final synchronized void writeExternal(ObjectOutput oos) throws IOException { Iterator iter = fileSystems.iterator(); while (iter.hasNext()) { FileSystem fs = (FileSystem) iter.next(); if (!fs.isDefault() && !fs.isPersistent()) { oos.writeObject(new NbMarshalledObject(fs)); } } oos.writeObject(null); }
ClassLoader cl = getNBClassLoader(); Class clazz = Class.forName(newN, false, cl);
ObjectInputStream ois = new org.openide.util.io.NbObjectInputStream(fis); Object o = ois.readObject();
return (mo == null) ? null : mo.get(); } catch (IOException e) { ExternalUtil.log("Cannot load attribute " + attrName + " from " + fileName); // NOI18N