private OptionalDataException createOptionalDataException() { try { OptionalDataException result = (OptionalDataException) OPT_DATA_EXCEPTION_CTOR.newInstance(new Object[] { Boolean.TRUE }); if (result == null) // XXX I18N, logging needed. throw new Error("Created null OptionalDataException"); return result; } catch (Exception ex) { // XXX I18N, logging needed. throw new Error("Couldn't create OptionalDataException", ex); } }
@Override protected boolean canHandle(String className, String message, @Nullable Throwable throwable) { Throwable cause = throwable != null ? throwable.getCause() : null; if (cause == null) { return false; } if (cause instanceof ClassCastException && checkClassCastExceptionStack(cause.getStackTrace())) { return true; } if (cause instanceof OptionalDataException && checkOptionalDataExceptionStack(cause.getStackTrace())) { return true; } return false; }
/** * Accept incoming events and processes them. */ @Override public void run() { while (isActive) { try { final byte[] buf = new byte[maxBufferSize]; final DatagramPacket packet = new DatagramPacket(buf, buf.length); server.receive(packet); final ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(packet.getData(), packet.getOffset(), packet.getLength())); final LogEvent event = (LogEvent) ois.readObject(); if (event != null) { log(event); } } catch (final OptionalDataException opt) { logger.error("OptionalDataException eof=" + opt.eof + " length=" + opt.length, opt); } catch (final ClassNotFoundException cnfe) { logger.error("Unable to locate LogEvent class", cnfe); } catch (final EOFException eofe) { logger.info("EOF encountered"); } catch (final IOException ioe) { logger.error("Exception encountered on accept. Ignoring. Stack Trace :", ioe); } } }
private static Constructor getOptDataExceptionCtor() { try { Constructor result = (Constructor) AccessController.doPrivileged( new PrivilegedExceptionAction() { public java.lang.Object run() throws NoSuchMethodException, SecurityException { Constructor boolCtor = OptionalDataException.class.getDeclaredConstructor( new Class[] { Boolean.TYPE }); boolCtor.setAccessible(true); return boolCtor; }}); if (result == null) // XXX I18N, logging needed. throw new Error("Unable to find OptionalDataException constructor"); return result; } catch (Exception ex) { // XXX I18N, logging needed. throw new ExceptionInInitializerError(ex); } }
/** * Restores this <code>DataFlavor</code> from a Serialized state. */ public synchronized void readExternal(ObjectInput is) throws IOException , ClassNotFoundException { String rcn = null; mimeType = (MimeType)is.readObject(); if (mimeType != null) { humanPresentableName = mimeType.getParameter("humanPresentableName"); mimeType.removeParameter("humanPresentableName"); rcn = mimeType.getParameter("class"); if (rcn == null) { throw new IOException("no class parameter specified in: " + mimeType); } } try { representationClass = (Class)is.readObject(); } catch (OptionalDataException ode) { if (!ode.eof || ode.length != 0) { throw ode; } // Ensure backward compatibility. // Old versions didn't write the representation class to the stream. if (rcn != null) { representationClass = DataFlavor.tryToLoadClass(rcn, getClass().getClassLoader()); } } }
/** * Returns a new OptionalDataException with {@code eof} set to {@code true} * or {@code false}. * @param bool the value of {@code eof} in the created OptionalDataException * @return a new OptionalDataException */ public final OptionalDataException newOptionalDataExceptionForSerialization(boolean bool) { try { Constructor<OptionalDataException> boolCtor = OptionalDataException.class.getDeclaredConstructor(Boolean.TYPE); boolCtor.setAccessible(true); return boolCtor.newInstance(bool); } catch (NoSuchMethodException | InstantiationException| IllegalAccessException|InvocationTargetException ex) { throw new InternalError("unable to create OptionalDataException", ex); } }
/** * Test the constructor of OptionalDataExceptions. */ @Test static void newOptionalDataException() { OptionalDataException ode = factory.newOptionalDataExceptionForSerialization(true); Assert.assertTrue(ode.eof, "eof wrong"); ode = factory.newOptionalDataExceptionForSerialization(false); Assert.assertFalse(ode.eof, "eof wrong"); }
/** * Return the accessible constructor for OptionalDataException signaling eof. * @returns the eof constructor for OptionalDataException */ public final Constructor<OptionalDataException> newOptionalDataExceptionForSerialization() { try { Constructor<OptionalDataException> boolCtor = OptionalDataException.class.getDeclaredConstructor(Boolean.TYPE); boolCtor.setAccessible(true); return boolCtor; } catch (NoSuchMethodException ex) { throw new InternalError("Constructor not found", ex); } }
/** * Returns a new OptionalDataException with {@code eof} set to {@code true} * or {@code false}. * @param bool the value of {@code eof} in the created OptionalDataException * @return a new OptionalDataException */ public final OptionalDataException newOptionalDataExceptionForSerialization(boolean bool) { Constructor<OptionalDataException> cons = delegate.newOptionalDataExceptionForSerialization(); try { return cons.newInstance(bool); } catch (InstantiationException|IllegalAccessException|InvocationTargetException ex) { throw new InternalError("unable to create OptionalDataException", ex); } }
/** * Restores this {@code DataFlavor} from a Serialized state. */ public synchronized void readExternal(ObjectInput is) throws IOException , ClassNotFoundException { String rcn = null; mimeType = (MimeType)is.readObject(); if (mimeType != null) { humanPresentableName = mimeType.getParameter("humanPresentableName"); mimeType.removeParameter("humanPresentableName"); rcn = mimeType.getParameter("class"); if (rcn == null) { throw new IOException("no class parameter specified in: " + mimeType); } } try { representationClass = (Class)is.readObject(); } catch (OptionalDataException ode) { if (!ode.eof || ode.length != 0) { throw ode; } // Ensure backward compatibility. // Old versions didn't write the representation class to the stream. if (rcn != null) { representationClass = DataFlavor.tryToLoadClass(rcn, getClass().getClassLoader()); } } }
/** * Stores the given object in the Rangzen generic store, using Java's object * serialization and base64 coding. * * @param key The key under which to store the data. * @param value The object to store, which must be serializable. */ public void putObject(String key, Object value) throws IOException, StreamCorruptedException, OptionalDataException { ByteArrayOutputStream b = new ByteArrayOutputStream(); ObjectOutputStream o = new ObjectOutputStream(b); o.writeObject(value); o.close(); put(key, new String(Base64.encode(b.toByteArray(), Base64.DEFAULT))); }
/** * Retrieves the object associated with the given key. * * @param key The key under which to retrieve a object from the store. * @return The object requested or null if not found. */ public Object getObject(String key) throws IOException, ClassNotFoundException, StreamCorruptedException, OptionalDataException { String v = get(key); if (v == null) return null; ObjectInputStream o = new ObjectInputStream( new ByteArrayInputStream(Base64.decode(v, Base64.DEFAULT))); return o.readObject(); }
/** * Get a list of all locations stored on this device. * * @return A list of locations this device has been recorded to be at. * * TODO(lerner): Use a set instead. Hashcode is always 0 on my serializable * locations though, and they're not comparable, so we can't use HashSet or TreeSet. */ public List<SerializableLocation> getAllLocations() throws StreamCorruptedException, OptionalDataException, IOException, ClassNotFoundException { if (getMostRecentSequenceNumber() == NO_SEQUENCE_STORED) { return new ArrayList<SerializableLocation>(); } return getLocations(MIN_SEQUENCE_NUMBER, getMostRecentSequenceNumber()); }
/** * Get a list of locations stored on this device between the given indexes (inclusive). * Throws an IllegalArgumentException if either index value is out of bounds * or end is less than start. * * @return A list of locations this device has been recorded to be at. */ public List<SerializableLocation> getLocations(int start, int end) throws StreamCorruptedException, OptionalDataException, IOException, ClassNotFoundException { int lastSequenceNumber = getMostRecentSequenceNumber(); if (start < MIN_SEQUENCE_NUMBER || end < MIN_SEQUENCE_NUMBER || start > lastSequenceNumber || end > lastSequenceNumber || end < start) { throw new IllegalArgumentException("Indexes [" + start + "," + end + "] out of bounds."); } ArrayList<SerializableLocation> locations = new ArrayList<SerializableLocation>(); for (int i = start; i <= end; i++) { locations.add((SerializableLocation) store.getObject(getLocationKey(i))); } return locations; }
/** * Stores the given object in the Murmur generic store, using Java's object * serialization and base64 coding. * * @param key The key under which to store the data. * @param value The object to store, which must be serializable. */ public void putObject(String key, Object value) throws IOException, StreamCorruptedException, OptionalDataException { ByteArrayOutputStream b = new ByteArrayOutputStream(); ObjectOutputStream o = new ObjectOutputStream(b); o.writeObject(value); o.close(); put(key, new String(Base64.encode(b.toByteArray(), Base64.DEFAULT))); }
/** * Tests that we can store locations and get them all back. */ @Test public void storeLocations() throws StreamCorruptedException, OptionalDataException, IOException, ClassNotFoundException { assertEquals(0, store.getAllLocations().size()); assertTrue(store.addLocation(serialLoc1)); assertEquals(1, store.getAllLocations().size()); assertTrue(store.addLocation(serialLoc2)); assertEquals(2, store.getAllLocations().size()); assertEquals(store.MIN_SEQUENCE_NUMBER + 1, store.getMostRecentSequenceNumber()); List<SerializableLocation> locations = store.getAllLocations(); for (SerializableLocation location : locations) { if (PROVIDER1.equals(location.provider)) { assertEquals(lat1, location.latitude, 0.1); } else if (PROVIDER2.equals(location.provider)) { assertEquals(lat2, location.latitude, 0.1); } else { assertFalse("Provider isn't PROVIDER1 or PROVIDER2", true); } } locations = store.getLocations(store.MIN_SEQUENCE_NUMBER, store.MIN_SEQUENCE_NUMBER); assertEquals(1, locations.size()); assertEquals(lat1, locations.get(0).latitude, 0.1); locations = store.getLocations(store.MIN_SEQUENCE_NUMBER + 1, store.MIN_SEQUENCE_NUMBER + 1); assertEquals(1, locations.size()); assertEquals(lat2, locations.get(0).latitude, 0.1); }
/** * De-serialize this class. * * @param stream The <code>ObjectInput</code> stream to deserialize from. * * @exception IOException If an error ocurs. * @exception ClassNotFoundException If the class for an object being restored * cannot be found. */ public void readExternal(ObjectInput stream) throws IOException, ClassNotFoundException { mimeType = (MimeType) stream.readObject(); String className = null; if (mimeType != null) { humanPresentableName = mimeType.getParameter("humanPresentableName"); mimeType.removeParameter("humanPresentableName"); className = mimeType.getParameter("class"); if (className == null) throw new IOException("No class in mime type"); } try { representationClass = (Class) stream.readObject(); } catch (OptionalDataException ex) { if (ex.eof && ex.length == 0) { if (className != null) representationClass = tryToLoadClass(className, getClass().getClassLoader()); } else throw ex; } }