001/*
002 * Copyright (c) 2011+, HL7, Inc
003 * All rights reserved.
004 * 
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 * 
008 * Redistributions of source code must retain the above copyright notice, this
009 * list of conditions and the following disclaimer.
010 * Redistributions in binary form must reproduce the above copyright notice,
011 * this list of conditions and the following disclaimer in the documentation
012 * and/or other materials provided with the distribution.
013 * Neither the name of HL7 nor the names of its contributors may be used to
014 * endorse or promote products derived from this software without specific
015 * prior written permission.
016 * 
017 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
018 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
019 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
020 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
021 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
022 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
023 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
024 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
025 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
026 * POSSIBILITY OF SUCH DAMAGE.
027 * 
028 */
029package org.hl7.fhir.dstu3.model;
030
031import org.apache.commons.codec.binary.Base64;
032
033import ca.uhn.fhir.model.api.annotation.DatatypeDef;
034
035/**
036 * Primitive type "base64Binary" in FHIR: a sequence of bytes represented in base64
037 */
038@DatatypeDef(name = "base64binary")
039public class Base64BinaryType extends PrimitiveType<byte[]> {
040
041  private static final long serialVersionUID = 3L;
042
043  /**
044   * Constructor
045   */
046  public Base64BinaryType() {
047    super();
048  }
049
050  public Base64BinaryType(byte[] theBytes) {
051    super();
052    setValue(theBytes);
053  }
054
055  public Base64BinaryType(String theValue) {
056    super();
057    setValueAsString(theValue);
058  }
059
060  protected byte[] parse(String theValue) {
061    return Base64.decodeBase64(theValue.getBytes(ca.uhn.fhir.rest.api.Constants.CHARSET_UTF8));
062  }
063
064  protected String encode(byte[] theValue) {
065    if (theValue == null) {
066      return null;
067    }
068    return new String(Base64.encodeBase64(theValue), ca.uhn.fhir.rest.api.Constants.CHARSET_UTF8);
069  }
070
071  @Override
072  public Base64BinaryType copy() {
073    return new Base64BinaryType(getValue());
074  }
075
076  public String fhirType() {
077    return "base64Binary";
078  }
079}