package serializers.colfer.media; // This file was generated by colf(1); DO NOT EDIT import static java.lang.String.format; import java.util.InputMismatchException; import java.nio.BufferOverflowException; import java.nio.BufferUnderflowException; /** * Data bean with built-in serialization support. * @author generated by colf(1) * @see <a href="https://github.com/pascaldekloe/colfer">Colfer's home</a> */ public class Image implements java.io.Serializable { /** The upper limit for serial byte sizes. */ public static int colferSizeMax = 16 * 1024 * 1024; /** The upper limit for the number of elements in a list. */ public static int colferListMax = 64 * 1024; private static final java.nio.charset.Charset _utf8 = java.nio.charset.Charset.forName("UTF-8"); public String uri = ""; public String title = ""; public int width; public int height; public boolean small; public boolean large; /** * Serializes the object. * @param buf the data destination. * @param offset the initial index for {@code buf}, inclusive. * @return the final index for {@code buf}, exclusive. * @throws BufferOverflowException when {@code buf} is too small. * @throws IllegalStateException on an upper limit breach defined by either {@link #colferSizeMax} or {@link #colferListMax}. */ public int marshal(byte[] buf, int offset) { int i = offset; try { if (! this.uri.isEmpty()) { buf[i++] = (byte) 0; int start = ++i; String s = this.uri; for (int sIndex = 0, sLength = s.length(); sIndex < sLength; sIndex++) { char c = s.charAt(sIndex); if (c < '\u0080') { buf[i++] = (byte) c; } else if (c < '\u0800') { buf[i++] = (byte) (192 | c >>> 6); buf[i++] = (byte) (128 | c & 63); } else if (c < '\ud800' || c > '\udfff') { buf[i++] = (byte) (224 | c >>> 12); buf[i++] = (byte) (128 | c >>> 6 & 63); buf[i++] = (byte) (128 | c & 63); } else { int cp = 0; if (++sIndex < sLength) cp = Character.toCodePoint(c, s.charAt(sIndex)); if ((cp >= 1 << 16) && (cp < 1 << 21)) { buf[i++] = (byte) (240 | cp >>> 18); buf[i++] = (byte) (128 | cp >>> 12 & 63); buf[i++] = (byte) (128 | cp >>> 6 & 63); buf[i++] = (byte) (128 | cp & 63); } else buf[i++] = (byte) '?'; } } int size = i - start; if (size > colferSizeMax) throw new IllegalStateException(format("colfer: field serializers/colfer/media.image.uri size %d exceeds %d UTF-8 bytes", size, colferSizeMax)); int ii = start - 1; if (size > 0x7f) { i++; for (int x = size; x >= 1 << 14; x >>>= 7) i++; System.arraycopy(buf, start, buf, i - size, size); do { buf[ii++] = (byte) (size | 0x80); size >>>= 7; } while (size > 0x7f); } buf[ii] = (byte) size; } if (! this.title.isEmpty()) { buf[i++] = (byte) 1; int start = ++i; String s = this.title; for (int sIndex = 0, sLength = s.length(); sIndex < sLength; sIndex++) { char c = s.charAt(sIndex); if (c < '\u0080') { buf[i++] = (byte) c; } else if (c < '\u0800') { buf[i++] = (byte) (192 | c >>> 6); buf[i++] = (byte) (128 | c & 63); } else if (c < '\ud800' || c > '\udfff') { buf[i++] = (byte) (224 | c >>> 12); buf[i++] = (byte) (128 | c >>> 6 & 63); buf[i++] = (byte) (128 | c & 63); } else { int cp = 0; if (++sIndex < sLength) cp = Character.toCodePoint(c, s.charAt(sIndex)); if ((cp >= 1 << 16) && (cp < 1 << 21)) { buf[i++] = (byte) (240 | cp >>> 18); buf[i++] = (byte) (128 | cp >>> 12 & 63); buf[i++] = (byte) (128 | cp >>> 6 & 63); buf[i++] = (byte) (128 | cp & 63); } else buf[i++] = (byte) '?'; } } int size = i - start; if (size > colferSizeMax) throw new IllegalStateException(format("colfer: field serializers/colfer/media.image.title size %d exceeds %d UTF-8 bytes", size, colferSizeMax)); int ii = start - 1; if (size > 0x7f) { i++; for (int x = size; x >= 1 << 14; x >>>= 7) i++; System.arraycopy(buf, start, buf, i - size, size); do { buf[ii++] = (byte) (size | 0x80); size >>>= 7; } while (size > 0x7f); } buf[ii] = (byte) size; } if (this.width != 0) { int x = this.width; if (x < 0) { x = -x; buf[i++] = (byte) (2 | 0x80); } else buf[i++] = (byte) 2; while ((x & ~0x7f) != 0) { buf[i++] = (byte) (x | 0x80); x >>>= 7; } buf[i++] = (byte) x; } if (this.height != 0) { int x = this.height; if (x < 0) { x = -x; buf[i++] = (byte) (3 | 0x80); } else buf[i++] = (byte) 3; while ((x & ~0x7f) != 0) { buf[i++] = (byte) (x | 0x80); x >>>= 7; } buf[i++] = (byte) x; } if (this.small) { buf[i++] = (byte) 4; } if (this.large) { buf[i++] = (byte) 5; } buf[i++] = (byte) 0x7f; return i; } catch (IndexOutOfBoundsException e) { if (i - offset > colferSizeMax) throw new IllegalStateException(format("colfer: serial exceeds %d bytes", colferSizeMax)); if (i >= buf.length) throw new BufferOverflowException(); throw e; } } /** * Deserializes the object. * @param buf the data source. * @param offset the initial index for {@code buf}, inclusive. * @return the final index for {@code buf}, exclusive. * @throws BufferUnderflowException when {@code buf} is incomplete. (EOF) * @throws SecurityException on an upper limit breach defined by either {@link #colferSizeMax} or {@link #colferListMax}. * @throws InputMismatchException when the data does not match this object's schema. */ public int unmarshal(byte[] buf, int offset) { int i = offset; try { byte header = buf[i++]; if (header == (byte) 0) { int size = 0; for (int shift = 0; true; shift += 7) { byte b = buf[i++]; size |= (b & 0x7f) << shift; if (shift == 28 || b >= 0) break; } if (size > colferSizeMax) throw new SecurityException(format("colfer: field serializers/colfer/media.image.uri size %d exceeds %d UTF-8 bytes", size, colferSizeMax)); int start = i; i += size; this.uri = new String(buf, start, size, this._utf8); header = buf[i++]; } if (header == (byte) 1) { int size = 0; for (int shift = 0; true; shift += 7) { byte b = buf[i++]; size |= (b & 0x7f) << shift; if (shift == 28 || b >= 0) break; } if (size > colferSizeMax) throw new SecurityException(format("colfer: field serializers/colfer/media.image.title size %d exceeds %d UTF-8 bytes", size, colferSizeMax)); int start = i; i += size; this.title = new String(buf, start, size, this._utf8); header = buf[i++]; } if (header == (byte) 2) { int x = 0; for (int shift = 0; true; shift += 7) { byte b = buf[i++]; x |= (b & 0x7f) << shift; if (shift == 28 || b >= 0) break; } this.width = x; header = buf[i++]; } else if (header == (byte) (2 | 0x80)) { int x = 0; for (int shift = 0; true; shift += 7) { byte b = buf[i++]; x |= (b & 0x7f) << shift; if (shift == 28 || b >= 0) break; } this.width = -x; header = buf[i++]; } if (header == (byte) 3) { int x = 0; for (int shift = 0; true; shift += 7) { byte b = buf[i++]; x |= (b & 0x7f) << shift; if (shift == 28 || b >= 0) break; } this.height = x; header = buf[i++]; } else if (header == (byte) (3 | 0x80)) { int x = 0; for (int shift = 0; true; shift += 7) { byte b = buf[i++]; x |= (b & 0x7f) << shift; if (shift == 28 || b >= 0) break; } this.height = -x; header = buf[i++]; } if (header == (byte) 4) { this.small = true; header = buf[i++]; } if (header == (byte) 5) { this.large = true; header = buf[i++]; } if (header != (byte) 0x7f) throw new InputMismatchException(format("colfer: unknown header at byte %d", i - 1)); } catch (IndexOutOfBoundsException e) { if (i - offset > colferSizeMax) throw new SecurityException(format("colfer: serial exceeds %d bytes", colferSizeMax)); if (i >= buf.length) throw new BufferUnderflowException(); throw new RuntimeException("colfer: bug", e); } if (i - offset > colferSizeMax) throw new SecurityException(format("colfer: serial exceeds %d bytes", colferSizeMax)); return i; } public String getUri() { return this.uri; } public void setUri(String value) { this.uri = value; } public String getTitle() { return this.title; } public void setTitle(String value) { this.title = value; } public int getWidth() { return this.width; } public void setWidth(int value) { this.width = value; } public int getHeight() { return this.height; } public void setHeight(int value) { this.height = value; } public boolean getSmall() { return this.small; } public void setSmall(boolean value) { this.small = value; } public boolean getLarge() { return this.large; } public void setLarge(boolean value) { this.large = value; } @Override public final int hashCode() { int h = 1; if (this.uri != null) h = 31 * h + this.uri.hashCode(); if (this.title != null) h = 31 * h + this.title.hashCode(); h = 31 * h + this.width; h = 31 * h + this.height; h = 31 * h + (this.small ? 1231 : 1237); h = 31 * h + (this.large ? 1231 : 1237); return h; } @Override public final boolean equals(Object o) { return o instanceof Image && equals((Image) o); } public final boolean equals(Image o) { return o != null && java.util.Objects.equals(this.uri, o.uri) && java.util.Objects.equals(this.title, o.title) && this.width == o.width && this.height == o.height && this.small == o.small && this.large == o.large; } }