@Override
  public void write(Bytes out, @NotNull CharSequence s) {
    if (s == null) {
      out.writeStopBit(NULL_LENGTH);
      return;
    } else if (s.length() == 0) {
      out.writeStopBit(0);
      return;
    }
    // write the total length.
    int length = s.length();
    out.writeStopBit(length);

    long position = out.writePosition();
    out.writeInt(0);
    DataOutputStream dos =
        new DataOutputStream(
            new BufferedOutputStream(new DeflaterOutputStream(out.outputStream())));
    try {
      for (int i = 0; i < s.length(); i++) {
        dos.write(s.charAt(i));
      }
      dos.close();
    } catch (IOException e) {
      throw new IORuntimeException(e);
    }
    out.writeInt(position, (int) (out.writePosition() - position - 4));
  }
Exemplo n.º 2
0
 public static void writeDataOnce(
     @NotNull WireOut wireOut, boolean metaData, @NotNull WriteMarshallable writer) {
   Bytes bytes = wireOut.bytes();
   long position = bytes.writePosition();
   int metaDataBit = metaData ? Wires.META_DATA : 0;
   int value = metaDataBit | Wires.NOT_READY | Wires.UNKNOWN_LENGTH;
   if (!bytes.compareAndSwapInt(position, 0, value)) return;
   bytes.writeSkip(4);
   writer.writeMarshallable(wireOut);
   int length =
       metaDataBit
           | toIntU30(
               bytes.writePosition() - position - 4,
               "Document length %,d out of 30-bit int range.");
   if (!bytes.compareAndSwapInt(position, value, length | Wires.META_DATA))
     throw new AssertionError();
 }
Exemplo n.º 3
0
 public static void writeData(
     @NotNull WireOut wireOut,
     boolean metaData,
     boolean notReady,
     @NotNull WriteMarshallable writer) {
   Bytes bytes = wireOut.bytes();
   long position = bytes.writePosition();
   int metaDataBit = metaData ? Wires.META_DATA : 0;
   bytes.writeOrderedInt(metaDataBit | Wires.NOT_READY | Wires.UNKNOWN_LENGTH);
   writer.writeMarshallable(wireOut);
   int length =
       metaDataBit
           | toIntU30(
               bytes.writePosition() - position - 4,
               "Document length %,d out of 30-bit int range.");
   bytes.writeOrderedInt(position, length | (notReady ? Wires.NOT_READY : 0));
 }
 public UncheckedNativeBytes(@NotNull Bytes<Underlying> underlyingBytes)
     throws IllegalStateException {
   underlyingBytes.reserve();
   this.bytesStore = (NativeBytesStore<Underlying>) underlyingBytes.bytesStore();
   assert bytesStore.start() == 0;
   writePosition = underlyingBytes.writePosition();
   writeLimit = underlyingBytes.writeLimit();
   readPosition = underlyingBytes.readPosition();
   capacity = bytesStore.capacity();
 }
        @Override
        public void readMarshallable(WireIn wire) throws IllegalStateException {
          @SuppressWarnings("ConstantConditions")
          final Bytes<?> outBytes = outWire.bytes();

          try {

            final StringBuilder eventName = Wires.acquireStringBuilder();
            @SuppressWarnings("ConstantConditions")
            final ValueIn valueIn = inWire.readEventName(eventName);

            outWire.writeDocument(
                true, w -> w.writeEventName(CoreFields.tid).int64(CollectionWireHandler.this.tid));

            outWire.writeDocument(
                false,
                out -> {

                  // note :  remove on the key-set returns a boolean and on the map returns the
                  // old value
                  if (EventId.remove.contentEquals(eventName)) {
                    outWire
                        .write(CoreFields.reply)
                        .bool(underlyingCollection.remove(fromWire.apply(valueIn)));
                    return;
                  }

                  // note :  remove on the key-set returns a boolean and on the map returns the
                  // old value
                  if (EventId.iterator.contentEquals(eventName)) {
                    final ValueOut valueOut = out.writeEventName(CoreFields.reply);
                    valueOut.sequence(v -> underlyingCollection.forEach(e -> toWire.accept(v, e)));
                    return;
                  }

                  if (EventId.numberOfSegments.contentEquals(eventName)) {
                    outWire.write(CoreFields.reply).int32(1);
                    return;
                  }

                  if (EventId.isEmpty.contentEquals(eventName)) {
                    outWire.write(CoreFields.reply).bool(underlyingCollection.isEmpty());
                    return;
                  }

                  if (EventId.size.contentEquals(eventName)) {
                    outWire.write(CoreFields.reply).int32(underlyingCollection.size());
                    return;
                  }

                  if (EventId.clear.contentEquals(eventName)) {
                    underlyingCollection.clear();
                    return;
                  }

                  if (EventId.contains.contentEquals(eventName)) {
                    outWire
                        .write(CoreFields.reply)
                        .bool(underlyingCollection.contains(fromWire.apply(valueIn)));
                    return;
                  }

                  if (EventId.add.contentEquals(eventName)) {
                    outWire
                        .write(CoreFields.reply)
                        .bool(underlyingCollection.add(fromWire.apply(valueIn)));
                    return;
                  }

                  if (EventId.remove.contentEquals(eventName)) {
                    outWire
                        .write(CoreFields.reply)
                        .bool(underlyingCollection.remove(fromWire.apply(valueIn)));
                    return;
                  }

                  if (EventId.containsAll.contentEquals(eventName)) {
                    outWire
                        .write(CoreFields.reply)
                        .bool(underlyingCollection.remove(collectionFromWire()));
                    return;
                  }

                  if (EventId.addAll.contentEquals(eventName)) {
                    outWire
                        .write(CoreFields.reply)
                        .bool(underlyingCollection.addAll(collectionFromWire()));
                    return;
                  }

                  if (EventId.removeAll.contentEquals(eventName)) {
                    outWire
                        .write(CoreFields.reply)
                        .bool(underlyingCollection.removeAll(collectionFromWire()));
                    return;
                  }

                  if (EventId.retainAll.contentEquals(eventName)) {
                    outWire
                        .write(CoreFields.reply)
                        .bool(underlyingCollection.retainAll(collectionFromWire()));
                    return;
                  }

                  throw new IllegalStateException("unsupported event=" + eventName);
                });
          } catch (Exception e) {
            LOG.error("", e);
          } finally {

            if (YamlLogging.showServerWrites) {
              long len = outBytes.writePosition();
              if (len >= SIZE_OF_SIZE) {
                String s = Wires.fromSizePrefixedBlobs(outBytes);

                LOG.info("server writes:\n\n" + s);
              }
            }
          }
        }