Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright 2013 SAP AG Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. /
 
 package com.sap.core.odata.core.edm;
 
 
 
Implementation of the EDM simple type Binary.

Author(s):
SAP AG
 
 public class EdmBinary extends AbstractSimpleType {
 
   private static final EdmBinary instance = new EdmBinary();
 
   public static EdmBinary getInstance() {
     return ;
   }
 
   @Override
   public Class<?> getDefaultType() {
     return byte[].class;
   }
 
   @Override
   public boolean validate(final String valuefinal EdmLiteralKind literalKindfinal EdmFacets facets) {
     if (value == null) {
       return facets == null || facets.isNullable() == null || facets.isNullable();
     }
 
     if (literalKind == null) {
       return false;
     }
 
     return validateLiteral(valueliteralKind) && validateMaxLength(valueliteralKindfacets);
   }
 
   private static boolean validateLiteral(final String valuefinal EdmLiteralKind literalKind) {
     return literalKind == . ?
         value.matches("(?:X|binary)'(?:\\p{XDigit}{2})*'") : Base64.isBase64(value);
   }
 
   private static boolean validateMaxLength(final String valuefinal EdmLiteralKind literalKindfinal EdmFacets facets) {
     return facets == null || facets.getMaxLength() == null ? true :
         literalKind == . ?
             // In URI representation, each byte is represented as two hexadecimal digits;
             // additionally, we have to account for the prefix and the surrounding "'"s.
             facets.getMaxLength() >= (value.length() - (value.startsWith("X") ? 3 : 8)) / 2
             :
             // In default representation, every three bytes are represented as four base-64 characters.
             facets.getMaxLength() >= value.length() * 3 / 4;
   }
 
   @Override
   protected <T> T internalValueOfString(final String valuefinal EdmLiteralKind literalKindfinal EdmFacets facetsfinal Class<T> returnTypethrows EdmSimpleTypeException {
     if (!validateLiteral(valueliteralKind)) {
     }
     if (!validateMaxLength(valueliteralKindfacets)) {
     }
 
     byte[] result;
     if (literalKind == .) {
       try {
         result = Hex.decodeHex(value.substring(value.startsWith("X") ? 2 : 7, value.length() - 1).toCharArray());
       } catch (final DecoderException e) {
       }
     } else {
       result = Base64.decodeBase64(value);
     }
 
     if (returnType.isAssignableFrom(byte[].class)) {
       return returnType.cast(result);
     } else if (returnType.isAssignableFrom(Byte[].class)) {
       Byte[] byteArray = new Byte[result.length];
       for (int i = 0; i < result.lengthi++) {
         byteArray[i] = result[i];
      }
      return returnType.cast(byteArray);
    } else {
    }
  }
  protected <T> String internalValueToString(final T valuefinal EdmLiteralKind literalKindfinal EdmFacets facetsthrows EdmSimpleTypeException {
    byte[] byteArrayValue;
    if (value instanceof byte[]) {
      byteArrayValue = (byte[]) value;
    } else if (value instanceof Byte[]) {
      final int length = ((Byte[]) value).length;
      byteArrayValue = new byte[length];
      for (int i = 0; i < lengthi++) {
        byteArrayValue[i] = ((Byte[]) value)[i].byteValue();
      }
    } else {
    }
    if (facets != null && facets.getMaxLength() != null && byteArrayValue.length > facets.getMaxLength()) {
    }
    return Base64.encodeBase64String(byteArrayValue);
  }
  public String toUriLiteral(final String literalthrows EdmSimpleTypeException {
    return "binary'" + Hex.encodeHexString(Base64.decodeBase64(literal)).toUpperCase(.) + "'";
  }
New to GrepCode? Check out our FAQ X