Search in sources :

Example 61 with ByteString

use of com.google.protobuf.ByteString in project etcd-java by IBM.

the class RangeCache method offerUpdate.

/**
 * @param keyValue
 * @param watchThread if being called from background watch context
 * @return the provided value, <i>or a newer one</i>
 */
protected KeyValue offerUpdate(final KeyValue keyValue, boolean watchThread) {
    final long modRevision = keyValue.getModRevision();
    if (modRevision <= seenUpToRev)
        return kvOrNullIfDeleted(keyValue);
    final ByteString key = keyValue.getKey();
    final boolean isDeleted = isDeleted(keyValue);
    // a possible (but unlikely) race with deletions
    if (watchThread && !isDeleted) {
        // optimized non-deletion path
        KeyValue newKv = entries.merge(key, keyValue, (k, v) -> (modRevision > v.getModRevision() ? keyValue : v));
        if (newKv == keyValue)
            notifyListeners(EventType.UPDATED, keyValue, true);
        return kvOrNullIfDeleted(newKv);
    }
    KeyValue existKv = entries.get(key);
    while (true) {
        if (existKv != null) {
            long existModRevision = existKv.getModRevision();
            if (existModRevision >= modRevision)
                return kvOrNullIfDeleted(existKv);
            KeyValue newKv = entries.computeIfPresent(key, (k, v) -> (existModRevision == v.getModRevision() ? keyValue : v));
            if (newKv != keyValue) {
                existKv = newKv;
                // update failed
                continue;
            }
            // update succeeded
            if (isDeleted) {
                deletionQueue.add(keyValue);
                if (!isDeleted(existKv)) {
                    // previous value
                    notifyListeners(EventType.DELETED, existKv, watchThread);
                }
                return null;
            } else {
                // added or updated
                notifyListeners(EventType.UPDATED, keyValue, false);
                return keyValue;
            }
        }
        // here existKv == null
        if (modRevision <= seenUpToRev)
            return null;
        if ((existKv = entries.putIfAbsent(key, keyValue)) == null) {
            // update succeeded
            if (isDeleted) {
                deletionQueue.add(keyValue);
                return null;
            } else {
                notifyListeners(EventType.UPDATED, keyValue, false);
                return keyValue;
            }
        }
    }
}
Also used : KeyValue(com.ibm.etcd.api.KeyValue) ByteString(com.google.protobuf.ByteString)

Example 62 with ByteString

use of com.google.protobuf.ByteString in project etcd-java by IBM.

the class KeyUtils method plusOne.

public static ByteString plusOne(ByteString key) {
    int max = key.size() - 1;
    if (max < 0)
        return singleByte(1);
    int last = key.byteAt(max);
    ByteString excludeLast = key.substring(0, max);
    return last == 0xff ? plusOne(excludeLast) : excludeLast.concat(singleByte(last + 1));
}
Also used : ByteString(com.google.protobuf.ByteString)

Example 63 with ByteString

use of com.google.protobuf.ByteString in project java-docs-samples by GoogleCloudPlatform.

the class Redact method redactImage.

// [START dlp_redact_image]
/*
   * Redact sensitive data from an image using the Data Loss Prevention API.
   *
   * @param filePath The path to a local file to inspect. Can be a JPG or PNG image file.
   * @param minLikelihood The minimum likelihood required before redacting a match.
   * @param infoTypes The infoTypes of information to redact.
   * @param outputPath The local path to save the resulting image to.
   * @param projectId The project ID to run the API call under.
   */
private static void redactImage(String filePath, Likelihood minLikelihood, List<InfoType> infoTypes, String outputPath, String projectId) throws Exception {
    // Instantiate the DLP client
    try (DlpServiceClient dlpClient = DlpServiceClient.create()) {
        String mimeType = URLConnection.guessContentTypeFromName(filePath);
        if (mimeType == null) {
            mimeType = MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType(filePath);
        }
        ByteContentItem.BytesType bytesType;
        switch(mimeType) {
            case "image/jpeg":
                bytesType = ByteContentItem.BytesType.IMAGE_JPEG;
                break;
            case "image/bmp":
                bytesType = ByteContentItem.BytesType.IMAGE_BMP;
                break;
            case "image/png":
                bytesType = ByteContentItem.BytesType.IMAGE_PNG;
                break;
            case "image/svg":
                bytesType = ByteContentItem.BytesType.IMAGE_SVG;
                break;
            default:
                bytesType = ByteContentItem.BytesType.BYTES_TYPE_UNSPECIFIED;
                break;
        }
        byte[] data = Files.readAllBytes(Paths.get(filePath));
        InspectConfig inspectConfig = InspectConfig.newBuilder().addAllInfoTypes(infoTypes).setMinLikelihood(minLikelihood).build();
        ByteContentItem byteContentItem = ByteContentItem.newBuilder().setType(bytesType).setData(ByteString.copyFrom(data)).build();
        List<RedactImageRequest.ImageRedactionConfig> imageRedactionConfigs = infoTypes.stream().map(infoType -> RedactImageRequest.ImageRedactionConfig.newBuilder().setInfoType(infoType).build()).collect(Collectors.toList());
        RedactImageRequest redactImageRequest = RedactImageRequest.newBuilder().setParent(ProjectName.of(projectId).toString()).addAllImageRedactionConfigs(imageRedactionConfigs).setByteItem(byteContentItem).setInspectConfig(inspectConfig).build();
        RedactImageResponse redactImageResponse = dlpClient.redactImage(redactImageRequest);
        // redacted image data
        ByteString redactedImageData = redactImageResponse.getRedactedImage();
        FileOutputStream outputStream = new FileOutputStream(outputPath);
        outputStream.write(redactedImageData.toByteArray());
        outputStream.close();
    }
}
Also used : ByteContentItem(com.google.privacy.dlp.v2.ByteContentItem) Options(org.apache.commons.cli.Options) Likelihood(com.google.privacy.dlp.v2.Likelihood) HelpFormatter(org.apache.commons.cli.HelpFormatter) MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) RedactImageResponse(com.google.privacy.dlp.v2.RedactImageResponse) ArrayList(java.util.ArrayList) DefaultParser(org.apache.commons.cli.DefaultParser) ServiceOptions(com.google.cloud.ServiceOptions) URLConnection(java.net.URLConnection) CommandLine(org.apache.commons.cli.CommandLine) Option(org.apache.commons.cli.Option) DlpServiceClient(com.google.cloud.dlp.v2.DlpServiceClient) Files(java.nio.file.Files) CommandLineParser(org.apache.commons.cli.CommandLineParser) FileOutputStream(java.io.FileOutputStream) InfoType(com.google.privacy.dlp.v2.InfoType) Collectors(java.util.stream.Collectors) ByteString(com.google.protobuf.ByteString) InspectConfig(com.google.privacy.dlp.v2.InspectConfig) List(java.util.List) ProjectName(com.google.privacy.dlp.v2.ProjectName) RedactImageRequest(com.google.privacy.dlp.v2.RedactImageRequest) Paths(java.nio.file.Paths) ParseException(org.apache.commons.cli.ParseException) ByteString(com.google.protobuf.ByteString) ByteContentItem(com.google.privacy.dlp.v2.ByteContentItem) ByteString(com.google.protobuf.ByteString) InspectConfig(com.google.privacy.dlp.v2.InspectConfig) DlpServiceClient(com.google.cloud.dlp.v2.DlpServiceClient) RedactImageRequest(com.google.privacy.dlp.v2.RedactImageRequest) FileOutputStream(java.io.FileOutputStream) RedactImageResponse(com.google.privacy.dlp.v2.RedactImageResponse)

Example 64 with ByteString

use of com.google.protobuf.ByteString in project bookkeeper by apache.

the class TableStoreImpl method buildRangeOp.

private RangeOp<byte[], byte[]> buildRangeOp(RoutingHeader header, RangeRequest request) {
    ByteString rKey = header.getRKey();
    ByteString lKey = request.getKey();
    ByteString lEndKey = request.getRangeEnd();
    byte[] storeKey = newStoreKey(rKey, lKey);
    byte[] storeEndKey = null;
    if (null != lEndKey && lEndKey.size() > 0) {
        storeEndKey = newStoreKey(rKey, lEndKey);
    }
    RangeOptionBuilder<byte[]> optionBuilder = store.getOpFactory().optionFactory().newRangeOption();
    if (request.getLimit() > 0) {
        optionBuilder.limit(request.getLimit());
    }
    if (request.getMaxCreateRevision() > 0) {
        optionBuilder.maxCreateRev(request.getMaxCreateRevision());
    }
    if (request.getMaxModRevision() > 0) {
        optionBuilder.maxModRev(request.getMaxModRevision());
    }
    if (request.getMinCreateRevision() > 0) {
        optionBuilder.minCreateRev(request.getMinCreateRevision());
    }
    if (request.getMinModRevision() > 0) {
        optionBuilder.minModRev(request.getMinModRevision());
    }
    if (null != storeEndKey) {
        optionBuilder.endKey(storeEndKey);
    }
    return store.getOpFactory().newRange(storeKey, optionBuilder.build());
}
Also used : ByteString(com.google.protobuf.ByteString)

Example 65 with ByteString

use of com.google.protobuf.ByteString in project bookkeeper by apache.

the class TableStoreImpl method buildPutOp.

private PutOp<byte[], byte[]> buildPutOp(RoutingHeader header, PutRequest request) {
    ByteString rKey = header.getRKey();
    ByteString lKey = request.getKey();
    byte[] storeKey = newStoreKey(rKey, lKey);
    return store.getOpFactory().newPut(storeKey, request.getValue().toByteArray(), store.getOpFactory().optionFactory().newPutOption().prevKv(request.getPrevKv()).build());
}
Also used : ByteString(com.google.protobuf.ByteString)

Aggregations

ByteString (com.google.protobuf.ByteString)406 Test (org.junit.Test)143 ArrayList (java.util.ArrayList)65 ByteString (org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.ByteString)63 HashMap (java.util.HashMap)41 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)40 IOException (java.io.IOException)37 List (java.util.List)33 Map (java.util.Map)33 ServerRequest (com.pokegoapi.main.ServerRequest)17 ExecutionException (java.util.concurrent.ExecutionException)16 AnnotateImageRequest (com.google.cloud.vision.v1.AnnotateImageRequest)14 AnnotateImageResponse (com.google.cloud.vision.v1.AnnotateImageResponse)14 BatchAnnotateImagesResponse (com.google.cloud.vision.v1.BatchAnnotateImagesResponse)14 Feature (com.google.cloud.vision.v1.Feature)14 Image (com.google.cloud.vision.v1.Image)14 ImageAnnotatorClient (com.google.cloud.vision.v1.ImageAnnotatorClient)14 FileInputStream (java.io.FileInputStream)13 ByteBuffer (java.nio.ByteBuffer)13 WebImage (com.google.cloud.vision.v1.WebDetection.WebImage)12