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;
034import ca.uhn.fhir.rest.server.Constants;
035
036/**
037 * Primitive type "base64Binary" in FHIR: a sequence of bytes represented in base64
038 */
039@DatatypeDef(name = "base64binary")
040public class Base64BinaryType extends PrimitiveType<byte[]> {
041
042  private static final long serialVersionUID = 3L;
043
044  /**
045   * Constructor
046   */
047  public Base64BinaryType() {
048    super();
049  }
050
051  public Base64BinaryType(byte[] theBytes) {
052    super();
053    setValue(theBytes);
054  }
055
056  public Base64BinaryType(String theValue) {
057    super();
058    setValueAsString(theValue);
059  }
060
061  protected byte[] parse(String theValue) {
062    return Base64.decodeBase64(theValue.getBytes(Constants.CHARSET_UTF8));
063  }
064
065  protected String encode(byte[] theValue) {
066    if (theValue == null) {
067      return null;
068    }
069    return new String(Base64.encodeBase64(theValue), Constants.CHARSET_UTF8);
070  }
071
072  @Override
073  public Base64BinaryType copy() {
074    return new Base64BinaryType(getValue());
075  }
076
077  public String fhirType() {
078    return "base64Binary";
079  }
080}