|
| 1 | +/* |
| 2 | + * eXist Open Source Native XML Database |
| 3 | + * Copyright (C) 2001-2018 The eXist Project |
| 4 | + * http://exist-db.org |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU Lesser General Public License |
| 8 | + * as published by the Free Software Foundation; either version 2 |
| 9 | + * of the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public |
| 17 | + * License along with this library; if not, write to the Free Software |
| 18 | + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | + */ |
| 20 | + |
| 21 | +package org.exist.storage; |
| 22 | + |
| 23 | +import org.apache.logging.log4j.LogManager; |
| 24 | +import org.apache.logging.log4j.Logger; |
| 25 | +import org.exist.storage.journal.AbstractLoggable; |
| 26 | + |
| 27 | +import javax.annotation.Nullable; |
| 28 | +import java.nio.file.Path; |
| 29 | +import java.nio.file.Paths; |
| 30 | + |
| 31 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 32 | + |
| 33 | +/** |
| 34 | + * @author Adam Retter <[email protected]> |
| 35 | + */ |
| 36 | +public abstract class AbstractBinaryLoggable extends AbstractLoggable { |
| 37 | + private static final Logger LOG = LogManager.getLogger(AbstractBinaryLoggable.class); |
| 38 | + |
| 39 | + public AbstractBinaryLoggable(final byte type, final long transactionId) { |
| 40 | + super(type, transactionId); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Get's the absolute path as a byte array. |
| 45 | + * |
| 46 | + * @param path the path to encode. |
| 47 | + * |
| 48 | + * @return the absolute path, UTF-8 encoded as bytes |
| 49 | + */ |
| 50 | + @Nullable protected static byte[] getPathData(@Nullable final Path path) { |
| 51 | + if (path == null) { |
| 52 | + return null; |
| 53 | + } |
| 54 | + return path.toAbsolutePath().toString().getBytes(UTF_8); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Get's the path from a byte array encoded by {@link #getPathData(Path)}. |
| 59 | + * |
| 60 | + * @param pathData the path data to decode. |
| 61 | + * |
| 62 | + * @return the path |
| 63 | + */ |
| 64 | + @Nullable protected static Path getPath(@Nullable final byte[] pathData) { |
| 65 | + if (pathData == null) { |
| 66 | + return null; |
| 67 | + } |
| 68 | + return Paths.get(new String(pathData, UTF_8)); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Converts the first two bytes of an integer into |
| 73 | + * an unsigned short and stores the result into a short. |
| 74 | + * |
| 75 | + * @param i the integer |
| 76 | + * |
| 77 | + * @return the unsigned short stored in a short. |
| 78 | + */ |
| 79 | + protected static short asUnsignedShort(final int i) { |
| 80 | + return (short)(i & 0xFFFF); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Converts an unsigned short stored in a short back |
| 85 | + * into an integer. Inverse of {@link #asUnsignedShort(int)}. |
| 86 | + * |
| 87 | + * @param s the unsigned short as a short. |
| 88 | + * |
| 89 | + * @return the integer. |
| 90 | + */ |
| 91 | + protected static int asSignedInt(final short s) { |
| 92 | + return s & 0xFFFF; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Check that the length of a path does not need more storage than we have available (i.e. 2 bytes). |
| 97 | + * |
| 98 | + * @param loggableName The name of the loggable (for formatting error messages). |
| 99 | + * @param pathName The name of the path (for formatting error messages). |
| 100 | + * @param path The path to check the length of. |
| 101 | + */ |
| 102 | + protected static void checkPathLen(final String loggableName, final String pathName, @Nullable final byte path[]) { |
| 103 | + if (path == null) { |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + final int len = path.length; |
| 108 | + if (len <= 0) { |
| 109 | + LOG.error(loggableName + ": " + pathName + " path has a zero length"); |
| 110 | + } else if(len > 0xFFFF) { |
| 111 | + LOG.error(loggableName + ": " + pathName + " path needs more than 65,535 bytes. Path will be truncated: " + new String(path, UTF_8)); |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments